Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 862

Formatting date in Java using SimpleDateFormat

$
0
0

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

SymbolMeaningPresentationExample
Gera designatorTextAD
yyearNumber2009
Mmonth in yearText & NumberJuly & 07
dday in monthNumber10
hhour in am/pm (1-12)Number12
Hhour in day (0-23)Number0
mminute in hourNumber30
ssecond in minuteNumber55
SmillisecondNumber978
Eday in weekTextTuesday
Dday in yearNumber189
Fday of week in monthNumber2 (2nd Wed in July)
wweek in yearNumber27
Wweek in monthNumber2
aam/pm markerTextPM
khour in day (1-24)Number24
Khour in am/pm (0-11)Number0
ztime zoneTextPacific Standard Time
'escape for textDelimiter(none)
'single quoteLiteral'

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


Viewing all articles
Browse latest Browse all 862

Trending Articles