Friday, June 18, 2021

Format Date in Java Using SimpleDateFormat

If you want to create your own customized formats to format a date in Java, you can do that using the SimpleDateFormat class.

When you create a SimpleDateFormat object, you specify a pattern String. The contents of the pattern String determine the format of the date and time.

For example-
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

Here the specified pattern is "MM/dd/yyyy" so the date will be formatted in that pattern.

Formatting date using SimpleDateFormat Java examples

In the example code several String patterns are used to create SimpleDateFormat object which are then used to format date. Comment along with the pattern shows how date is displayed using that pattern.

import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatDate {

 public static void main(String[] args) {
  FormatDate fd = new FormatDate();
  
  // For date in format Wed, Jun 8, '16
  fd.getFormattedDate("EEE, MMM d, ''yy");

  // For date in format Wednesday, June 08, 2016
  fd.getFormattedDate("EEEE, MMMM dd, yyyy");

  // For date in format 05/08/2016
  fd.getFormattedDate("MM/dd/yyyy");

  // For date in format 08/05/2016
  fd.getFormattedDate("dd/MM/yyyy");

  // For date in format 2016-05-08 AD at 09:42:54 IST
  // with era designator (AD in this case) and 
  // timezone info (IST in this case)
  fd.getFormattedDate("yyyy-MM-dd G 'at' hh:mm:ss z");

  //For date in format 08/May/2016 AD 21:47:28:889 PM
  //with AM/PM marker, time in 24 Hr fmt, miliseconds
  // also included
  fd.getFormattedDate("dd/MMMMM/yyyy GGG HH:mm:ss:SSS a");

  // Only time like 21:52:14:096 PM
  // in 24 hr format, with mili seconds and AM/PM marker
  fd.getFormattedDate("HH:mm:ss:SSS a");

 }
 
 public void getFormattedDate(String pattern){
  Date today;
  String result;
  SimpleDateFormat formatter;
  // Creating the date format using the given pattern
  formatter = new SimpleDateFormat(pattern);
  // Getting the date instance
  today = new Date();
  // formatting the date
  result = formatter.format(today);
  System.out.println("Pattern: " + pattern + 
    " Formatted Date - " + result);
 }

}

Output

Pattern: EEE, MMM d, ''yy Formatted Date - Sun, May 8, '16
Pattern: EEEE, MMMM dd, yyyy Formatted Date - Sunday, May 08, 2016
Pattern: MM/dd/yyyy Formatted Date - 05/08/2016
Pattern: dd/MM/yyyy Formatted Date - 08/05/2016
Pattern: yyyy-MM-dd G 'at' hh:mm:ss z Formatted Date - 2016-05-08 AD at 10:13:46 IST
Pattern: dd/MMMMM/yyyy GGG HH:mm:ss:SSS a Formatted Date - 08/May/2016 AD 22:13:46:090 PM
Pattern: HH:mm:ss:SSS a Formatted Date - 22:13:46:092 PM

Symbols used for creating date patterns in Java

Symbol Meaning Presentation Example
G era designator Text AD
y year Number 2009
M month in year Text & Number July & 07
d day in month Number 10
h hour in am/pm (1-12) Number 12
H hour in day (0-23) Number 0
m minute in hour Number 30
s second in minute Number 55
S millisecond Number 978
E day in week Text Tuesday
D day in year Number 189
F day of week in month Number 2 (2nd Wed in July)
w week in year Number 27
W week in month Number 2
a am/pm marker Text PM
k hour in day (1-24) Number 24
K hour in am/pm (0-11) Number 0
z time zone Text Pacific Standard Time
' escape for text Delimiter (none)
' single quote Literal '

Date Format Pattern Syntax

The number of symbol letters you specify also determines the format.

As exp. symbol for which the presentation style is text if length is 1-3 then abbreviated form is used if length is >= 4 then full form is used. In the above code it can be seen when 'EEE' is given it shows SUN as the day of the week, when 'EEEE' is given then Sunday is displayed.

Same way for month for which presentation style is text/number if length is 1-2 then number form is used when length is 3 (or more) then text form is used.

Source: https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html

That's all for this topic Format Date in Java Using SimpleDateFormat. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. How to Convert Date And Time Between Different Time-Zones in Java
  2. How to Display Time in AM-PM Format in Java
  3. Difference Between Two Dates in Java
  4. How to Find Last Modified Date of a File in Java
  5. How to Convert String to Date in Java

You may also like-

  1. How to Read File From The Last Line in Java
  2. How to Sort ArrayList of Custom Objects in Java
  3. How to Untar a File in Java
  4. Split a String Java Program
  5. Marker interface in Java
  6. Difference Between Checked And Unchecked Exceptions in Java
  7. Java ThreadLocal Class With Examples
  8. CopyOnWriteArrayList in Java With Examples

No comments:

Post a Comment