«Back to Home

Core Java

Topics

How To Format Date In Java

Format Date
 
In Java, there are two classes for formatting the date, which are,
  • DateFormat.

  • SimpleDateFormat.
DateFormat

In Java, java.text.DateFormat class provides many methods to format and parse the date and time in the language in an independent manner. The DateFormat is an abstract class, java.text.Format is the parent class and java.text.SimpleDateFormat is the child class of java.text.DateFormat class.
 
We can convert the date into the string, which is called as formatting and vice-versa parsing.
 
In other words, parsing means the string to the date and formatting means the date to the string in Java.
 
Fields of java.text.DateFormat
  • protected Calendar calendar
  • protected NumberFormat numberFormat
  • public static final int ERA_FIELD
  • public static final int YEAR_FIELD
  • public static final int MONTH_FIELD
  • public static final int DATE_FIELD
  • public static final int HOUR_OF_DAY1_FIELD
  • public static final int HOUR_OF_DAY0_FIELD
  • public static final int MINUTE_FIELD
  • public static final int SECOND_FIELD
  • public static final int MILLISECOND_FIELD
  • public static final int DAY_OF_WEEK_FIELD
  • public static final int DAY_OF_YEAR_FIELD
  • public static final int DAY_OF_WEEK_IN_MONTH_FIELD
  • public static final int WEEK_OF_YEAR_FIELD
  • public static final int WEEK_OF_MONTH_FIELD
  • public static final int AM_PM_FIELD
  • public static final int HOUR1_FIELD
  • public static final int HOUR0_FIELD
  • public static final int TIMEZONE_FIELD
  • public static final int FULL
  • public static final int LONG
  • public static final int MEDIUM
  • public static final int SHORT
  • public static final int DEFAULT
Methods of java.text.DateFormat

Public Method

final String format(Date date)

This method is used to convert the given date object into the string.
 
Date parse(String source)throws ParseException

This method is used to convert the string into the date object.
 
static final DateFormat getTimeInstance()

This method is used to return the time formatter with the default formatting style for the default locale.
 
static final DateFormat getTimeInstance(int style)

This method is used to return the time formatter with the given formatting style for the default locale.
 
static final DateFormat getTimeInstance(int style, Locale locale)

This method is used to return the time formatter with the particular formatting style for the given locale.
 
static final DateFormat getDateInstance()

This method is used to return the date formatter with the default formatting style for the default locale.
 
static final DateFormat getDateInstance(int style)

This method is used to return the date formatter with the given formatting style for the default locale.
 
static final DateFormat getDateInstance(int style, Locale locale)

This method is used to return date formatter with the given formatting style for the given locale.
 
static final DateFormat getDateTimeInstance()

This method is used to return the date/time formatter with the default formatting style for the default locale.
 
static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle)

This method is used to return the date/time formatter with the given date formatting style and time formatting style for the default locale.
 
static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)

This method is used to return the date/time formatter with the given date formatting style and time formatting style for the given locale.
 
static final DateFormat getInstance()

This method is used to return the date/time formatter with short formatting style for date and time.
 
static Locale[] getAvailableLocales()

This method is used to return an array of the available locales.
 
Calendar getCalendar()

This method is used to return an instance of the Calendar for this DateFormat instance.
 
NumberFormat getNumberFormat()

This method is used to return an instance of NumberFormat for this DateFormat instance.
 
TimeZone getTimeZone()

This method is used to return an instance of the TimeZone for this DateFormat instance.
 
Let’s see an example: Date to String, given below.
 
Code
  1. import java.text.DateFormat;  
  2. import java.util.Date;  
  3. public class DateExample {  
  4.     public static void main(String[] args) {  
  5.         Date d = new Date();  
  6.         System.out.println("Current Date: " + d);  
  7.         String s = DateFormat.getInstance().format(d);  
  8.         System.out.println("Date Format to String: " + s);  
  9.     }  
  10. }  
 33
 
Output

34

Let’s see another example: Date Format with Time, given below.
 
Code
  1. import java.text.DateFormat;  
  2. import java.util.Date;  
  3. public class DateExample {  
  4.     public static void main(String[] args) {  
  5.         Date d = new Date();  
  6.         System.out.println("Current Date: " + d);  
  7.         String s = DateFormat.getInstance().format(d);  
  8.         System.out.println("Date Format: " + s);  
  9.         s = DateFormat.getDateInstance().format(d);  
  10.         System.out.println("Date Format: " + s);  
  11.         s = DateFormat.getTimeInstance().format(d);  
  12.         System.out.println("Date Format: " + s);  
  13.         s = DateFormat.getDateTimeInstance().format(d);  
  14.         System.out.println("Date Format: " + s);  
  15.         s = DateFormat.getTimeInstance(DateFormat.SHORT).format(d);  
  16.         System.out.println("SHORT DateFormat: " + s);  
  17.         s = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(d);  
  18.         System.out.println("MEDIUM DateFormat: " + s);  
  19.         s = DateFormat.getTimeInstance(DateFormat.LONG).format(d);  
  20.         System.out.println("LONG DateFormat: " + s);  
  21.     }  
  22. }  
35

Output

37

SimpleDateFormat

In Java, java.text.SimpleDateFormat class provides many methods to format and parse the date and time. It is a concrete class to format and parse the date, which extends java.text.DateFormat class.
 
Let’s see an example: Date to String, given below.
 
Code
  1. import java.text.SimpleDateFormat;  
  2. import java.util.Date;  
  3. public class DateExample {  
  4.     public static void main(String[] args) {  
  5.         Date d = new Date();  
  6.         SimpleDateFormat fomt = new SimpleDateFormat("dd/MM/yyyy");  
  7.         String s = fomt.format(d);  
  8.         System.out.println(s);  
  9.     }  
  10. }  
38

Output

39

In Java, capital M represents month and small m represents minute.
 
Let’s see another example: Date Format with Time, given below.
 
Code
  1. import java.text.*;  
  2. import java.util.*;  
  3. public class DateExample {  
  4.     public static void main(String[] args) {  
  5.         Date d = new Date();  
  6.         SimpleDateFormat ft = new SimpleDateFormat("MM/dd/yyyy");  
  7.         String s = ft.format(d);  
  8.         System.out.println("Date Format: " + s);  
  9.         ft = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");  
  10.         s = ft.format(d);  
  11.         System.out.println("Date Format: " + s);  
  12.         ft = new SimpleDateFormat("dd MMMM yyyy");  
  13.         s = ft.format(d);  
  14.         System.out.println("Date Format: " + s);  
  15.         ft = new SimpleDateFormat("dd MMMM yyyy zzzz");  
  16.         s = ft.format(d);  
  17.         System.out.println("Date Format: " + s);  
  18.         ft = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");  
  19.         s = ft.format(d);  
  20.         System.out.println("Date Format: " + s);  
  21.     }  
  22. }  
40

Output

41

Now, let’s see an example: String to Date, given below.
 
Code
  1. import java.text.*;  
  2. import java.util.*;  
  3.   
  4. public class DateExample {  
  5.     public static void main(String[] args) {  
  6.         SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");  
  7.         try {  
  8.             Date d = f.parse("8/09/2016");  
  9.             System.out.println("Date: " + d);  
  10.         } catch (ParseException e) {  
  11.             e.printStackTrace();  
  12.         }  
  13.     }  
  14. }  
42

Output

43

Summary

Thus, we learnt, FormatDate and SimpleFormatDate classes to format the date and also learn, how we can change date format in Java.