Tuesday, 17 May 2016

Formatting date in Java using SimpleDateFormat

If you want to create your own customized formats for formatting the date, you can use 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.

Java Code

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

public class FormatDate {

 public static void main(String[] args) {
  FormatDate fd = new FormatDate();
  
  // For date like Wed, Jun 8, '16
  fd.getFormattedDate("EEE, MMM d, ''yy");
  // For date like Wednesday, June 08, 2016
  fd.getFormattedDate("EEEE, MMMM dd, yyyy");
  // For date like 05/08/2016
  fd.getFormattedDate("MM/dd/yyyy");
  // For date like 08/05/2016
  fd.getFormattedDate("dd/MM/yyyy");
  // For date like 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 like 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 the pattern

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 How to format date in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. How to find last modified date of a file in Java
  2. Reading file in Java using BufferedReader
  3. How to sort arraylist of custom objects in Java
  4. How to Sort elements in different order in TreeSet using Comparator
  5. How to read file from the last line in Java

You may also like -

>>>Go to Java Programs page

No comments:

Post a Comment