English 中文(简体)
JAVA I18N - SimpleDateFormat Class
  • 时间:2024-09-17

Java Internapzation - SimpleDateFormat Class


Previous Page Next Page  

java.text.SimpleDateFormat class formats dates as per the given pattern. It is also used to parse dates from string where string contains date in mentioned format. See the following example of using SimpleDateFormat class.

IOTester.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

pubpc class I18NTester {
   pubpc static void main(String[] args) throws ParseException {
   
      String pattern = "dd-MM-yyyy";

      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

      Date date = new Date();

      System.out.println(date);
      System.out.println(simpleDateFormat.format(date));

      String dateText = "29-11-2017";

      date = simpleDateFormat.parse(dateText);

      System.out.println(simpleDateFormat.format(date));
   }
}

Output

It will print the following result.

Wed Nov 29 17:01:22 IST 2017
29-11-2017
29-11-2017
Print