Simple example to show how to use Date Formatting in Java

The following is a question from "SCJP Sun Certified Programmer for Java 6 Study Guide Exam" by Kathy Sierra and Bert Bates. I personally don't think this is a good question, but it is good to know how to use DateFormat class.

Given that 1119280000000L is roughly the number of milliseconds from Jan 1, 1970, to June 20, 2005, and that you want to print that date in German, using LONG style such that "June" will be displayed as "Juni".

import java.text.*;
import java.util.*;
//import java.date.*;// There is such package defined in Java!!
public class DateFormatExample {
    public static void main(String[] args){
        Date d = new Date(1119280000000L);
        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.GERMANY);
        System.out.println(df.format(d));
    }
}
Category >> Basics  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. 939347507 on 2013-11-1

    Date d=new Date();
    SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);
    sdf.format(d)
    这个可以

  2. […] Date formatting (add more) […]

Leave a comment

*