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));
    }
}

2 thoughts on “Simple example to show how to use Date Formatting in Java”

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

Leave a Comment