Java Internationalization - SimpleDateFormat Class



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.

Example

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

public class I18NTester {
   public 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.

Fri Jun 07 15:19:00 IST 2024
07-06-2024
29-11-2017
Advertisements