Home » Java DateFormat

Java Date Format

There are two classes for formatting dates in Java: DateFormat and SimpleDateFormat.

The java.text.DateFormat class provides various methods to format and parse date and time in java in language-independent manner. The DateFormat class is an abstract class. java.text. The Format is the parent class and java.text.SimpleDateFormat is the subclass of java.text.DateFormat class.

In Java, converting the date into the string is called formatting and vice-versa parsing. In other words, formatting means date to string, and parsing means string to date.

java.text.DateFormat Fields

java.text.DateFormat Methods

No. Public Method Description
1) final String format(Date date) converts given Date object into string.
2) Date parse(String source)throws ParseException converts string into Date object.
3) static final DateFormat getTimeInstance() returns time formatter with default formatting style for the default locale.
4) static final DateFormat getTimeInstance(int style) returns time formatter with the given formatting style for the default locale.
5) static final DateFormat getTimeInstance(int style, Locale locale) returns time formatter with the given formatting style for the given locale.
6) static final DateFormat getDateInstance() returns date formatter with default formatting style for the default locale.
7) static final DateFormat getDateInstance(int style) returns date formatter with the given formatting style for the default locale.
8) static final DateFormat getDateInstance(int style, Locale locale) returns date formatter with the given formatting style for the given locale.
9) static final DateFormat getDateTimeInstance() returns date/time formatter with default formatting style for the default locale.
10) static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle) returns date/time formatter with the given date formatting style and time formatting style for the default locale.
11) static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale) returns date/time formatter with the given date formatting style and time formatting style for the given locale.
12) static final DateFormat getInstance() returns date/time formatter with short formatting style for date and time.
13) static Locale[] getAvailableLocales() returns an array of available locales.
14) Calendar getCalendar() returns an instance of Calendar for this DateFormat instance.
15) NumberFormat getNumberFormat() returns an instance of NumberFormat for this DateFormat instance.
16) TimeZone getTimeZone() returns an instance of TimeZone for this DateFormat instance.

Java DateFormat Example: Date to String

Let’s see the simple example to format date and time in Java using java.text.DateFormat class.

FileName: DateFormatExample.java

Output:

Current Date: Tue Mar 31 14:37:23 IST 2015  Date Format using getInstance(): 31/3/15 2:37 PM  

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

FileName: DateFormatExample2.java

Output:

Current Date: Tue Mar 31 14:37:23 IST 2015  Date Format using getInstance(): 31/3/15 2:37 PM  Date Format using getDateInstance(): 31 Mar, 2015  Date Format using getTimeInstance(): 2:37:23 PM  Date Format using getDateTimeInstance(): 31 Mar, 2015 2:37:23 PM  Date Format using getTimeInstance(DateFormat.SHORT): 2:37 PM  Date Format using getTimeInstance(DateFormat.MEDIUM): 2:37:23 PM  Date Format using getTimeInstance(DateFormat.LONG): 2:37:23 PM IST  Date Format using getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT): 31 March, 2015 2:37 PM  

Java DateFormat Example: String to Date

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

FileName: DateFormatExample3.java

Output:

Date is: Tue Mar 31 00:00:00 IST 2015  

Java DateFormat Example: getTimeInstance(int style, Locale locale)

Let’s see the working of the getTimeInstance(int style, Locale locale) method.

FileName: GetTimeInstanceExample.java

Output:

13:12  

Java DateFormat Example: getDateInstance(int style)

Let’s see the working of the getDateInstance(int style) method.

FileName: GetDateInstanceExample.java

Output:

12/2/21  

Java DateFormat Example: getDateInstance(int style, Locale locale)

Let’s see the working of the getDateInstance(int style, Locale locale) method.

FileName: GetDateInstanceExample1.java

Output:

02/12/2021  

Java DateFormat Example: getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)

Let’s see the working of the getDateTimeInstance(int dateStyle, int timeStyle, Locale locale) method.

FileName: GetDateTimeInstanceExample.java

Output:

02/12/2021 14:16:34 GMT  

Java DateFormat Example: getCalender()

Let’s see the working of the getCalender() method.

FileName: GetCalenderExample.java

Output:

java.util.GregorianCalendar[time = -886152493222, areFieldsSet = true, areAllFieldsSet = true, lenient = true, zone = sun.util.calendar.ZoneInfo[id = "GMT", offset=0, dstSavings = 0, useDaylight = false, transitions = 0, lastRule = null], firstDayOfWeek = 1, minimalDaysInFirstWeek = 1,ERA = 1, YEAR = 1941, MONTH = 11, WEEK_OF_YEAR = 49, WEEK_OF_MONTH = 1, DAY_OF_MONTH = 2, DAY_OF_YEAR = 336, DAY_OF_WEEK = 3, DAY_OF_WEEK_IN_MONTH = 1, AM_PM = 1, HOUR = 2, HOUR_OF_DAY = 14, MINUTE = 31, SECOND = 46, MILLISECOND = 778, ZONE_OFFSET = 0, DST_OFFSET = 0]  

Java DateFormat Example: getNumberFormat()

Let’s see the working of the getNumberFormat() method.

FileName: GetNumberFormatExample.java

Output:

The format is: [email protected]  

You may also like