java SimpleDateFormat

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java SimpleDateFormat

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.

Java SimpleDateFormat Example: String to Date

  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.   
  12.     formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");  
  13.     strDate = formatter.format(date);  
  14.     System.out.println("Date Format with dd-M-yyyy hh:mm:ss : "+strDate);  
  15.   
  16.     formatter = new SimpleDateFormat("dd MMMM yyyy");  
  17.     strDate = formatter.format(date);  
  18.     System.out.println("Date Format with dd MMMM yyyy : "+strDate);  
  19.   
  20.     formatter = new SimpleDateFormat("dd MMMM yyyy zzzz");  
  21.     strDate = formatter.format(date);  
  22.     System.out.println("Date Format with dd MMMM yyyy zzzz : "+strDate);  
  23.   
  24.     formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");  
  25.     strDate = formatter.format(date);  
  26.     System.out.println("Date Format with E, dd MMM yyyy HH:mm:ss z : "+strDate);  
  27. }  
  28. }  

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