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

Open Compiler

importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{String pattern ="dd-MM-yyyy";SimpleDateFormat simpleDateFormat =newSimpleDateFormat(pattern);Date date =newDate();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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *