All for Joomla All for Webmasters

Java Simple Date Format

The java.text.SimpleDateFormat class provides methods to format and parse date and time in java. The SimpleDateFormat is a concrete class for formatting and parsing date which inherits java.text.DateFormat class.

Notice that formatting means converting date to string and parsing means converting string to date.

Java SimpleDateFormat Example: Date to String

Let’s see the simple example to format date in java using java.text.SimpleDateFormat class.

  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. public class SimpleDateFormatExample {
  4. public static void main(String[] args) {
  5.     Date date = new Date();
  6.     SimpleDateFormat formatter = new SimpleDateFormat(“dd/MM/yyyy”);
  7.     String strDate= formatter.format(date);
  8.     System.out.println(strDate);
  9. }
  10. }

Output:

13/04/2015

Note: M (capital M) represents month and m (small m) represents minute in java.

Let’s see the full example to format date and time in java using java.text.SimpleDateFormat class.

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.Locale;
  5. public class SimpleDateFormatExample2 {
  6. public static void main(String[] args) {
  7.     Date date = new Date();
  8.     SimpleDateFormat formatter = new SimpleDateFormat(“MM/dd/yyyy”);
  9.     String strDate = formatter.format(date);
  10.     System.out.println(“Date Format with MM/dd/yyyy : “+strDate);
  11.     formatter = new SimpleDateFormat(“dd-M-yyyy hh:mm:ss”);
  12.     strDate = formatter.format(date);
  13.     System.out.println(“Date Format with dd-M-yyyy hh:mm:ss : “+strDate);
  14.     formatter = new SimpleDateFormat(“dd MMMM yyyy”);
  15.     strDate = formatter.format(date);
  16.     System.out.println(“Date Format with dd MMMM yyyy : “+strDate);
  17.     formatter = new SimpleDateFormat(“dd MMMM yyyy zzzz”);
  18.     strDate = formatter.format(date);
  19.     System.out.println(“Date Format with dd MMMM yyyy zzzz : “+strDate);
  20.     formatter = new SimpleDateFormat(“E, dd MMM yyyy HH:mm:ss z”);
  21.     strDate = formatter.format(date);
  22.     System.out.println(“Date Format with E, dd MMM yyyy HH:mm:ss z : “+strDate);
  23. }
  24. }

Output:

Date Format with MM/dd/yyyy : 04/13/2015
Date Format with dd-M-yyyy hh:mm:ss : 13-4-2015 10:59:26
Date Format with dd MMMM yyyy : 13 April 2015
Date Format with dd MMMM yyyy zzzz : 13 April 2015 India Standard Time
Date Format with E, dd MMM yyyy HH:mm:ss z : Mon, 13 Apr 2015 22:59:26 IST

Java SimpleDateFormat Example: String to Date

Let’s see the simple example to convert string into date using java.text.SimpleDateFormat class.

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. public class SimpleDateFormatExample3 {
  5. public static void main(String[] args) {
  6.     SimpleDateFormat formatter = new SimpleDateFormat(“dd/MM/yyyy”);
  7.     try {
  8.         Date date = formatter.parse(“31/03/2015”);
  9.         System.out.println(“Date is: “+date);
  10.     } catch (ParseException e) {e.printStackTrace();}
  11. }
  12. }

Output:

Date is: Tue Mar 31 00:00:00 IST 2015
PinIt
submit to reddit

Leave a Reply

Top