Java Date format

Here i will provide list of Java Date formats that are used in Java frequently. There are different ways you can achieve the same result in Java.

1- Using Predefined Format

You can directly use DateFormat class to format date in predefined style. DateFormat has predefined 4 styles which can be used for quick formatting instead of using SimpleDateFormat.

  1. FULL
  2. LONG
  3. MEDIUM
  4. SHORT
  5. DEFAULT(Uses Medium as style)

Date Output

Predefined Style

Tuesday, June 30, 2016 FULL
June 30, 2009 LONG
Jun 30, 2009 MEDIUM
6/30/09 SHORT
Jun 30, 2009 MEDIUM

Example Code

Output

2- Using SimpleDateFormat

Date Output

Date Pattern

16.04.16 dd.MM.yy
16.04.2016 dd.MM.yyyy
16.Apr.2016 dd.MMM.yyyy
30.April.2009 dd.MMMM.yyyy
30.April.2009 23:23:23 (24 Hr format) dd.MM.yyyy HH:mm:ss
16.4.16 dd.M.yy
30.April.2009 23:23:23 (12 Hr format) dd.MM.yyyy hh:mm:ss
30.April.2009 23:23:23 PM dd.MM.yyyy HH:mm:ss a
30.April.2009 23:23:23 PM IST dd.MM.yyyy HH:mm:ss a z

Example Code

Output

3- Using Formatter Pattern

Formatter class can be used to support formatting of numeric, string and other variable types. Date can be formatted in different styles using this class. Some predefined conversion characters can be used to format date object.

Date Output

Date Pattern

04/17/16 %tm/%td/%ty
2016-04-17 %tY-%tm-%td
04/17/16 %tF
2016-04-17 %tD

Example Code

Output

 

References

  1. Simple Date Format Java Doc
  2. Formatter Java Doc
  3. Java Date Format Complete list

Leave A Comment

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