Java : Date Format Conversion

In below example for Date format conversion. Here are characters use for date format design.

LetterDate & Time ComponentPresentationExample
GEra designator Text AD
yYear Year1998; 98
YWeek year Year2007; 07
MMonth in year (context sensitive) MonthAugust; Aug; 08
LMonth in year (standalone form) MonthAugust; Aug; 08
wWeek in yearMonth28
WWeek in month Number3
DDay in year Number230
dDay in month Number18
FDay of week in month Number3
EDay name in week TextTuesday; Tue
uDay number of week (1 = Monday, ..., 7 = Sunday)Number2
aAm/pm marker TextPM
HHour in day (0-23) Number21
kHour in day (1-24) Number22
KHour in am/pm (0-11) Number10
hHour in am/pm (1-12) Number5
mMinute in HourNumber34
sSecond in minuteNumber55
SMillisecondNumber578
zTime zoneGeneral time zone Pacific Standard Time; PST; GMT-08:00
ZTime zoneRFC 822 time zone -0800
XTime zoneISO 8601 time zone-08; -0800; -08:00

 Example

package com.date;

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

public class DateFormatChange {

public static String LEGACY_FORMAT = "EEE MMM dd hh:mm:ss zzz yyyy";
public static String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz";
public static String SHORT_DATE = "yyyy-MM-dd";
private static final String MAIN_DATE_FORMAT = "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'";
private static final String DATE_FORMAT1 = "yyyyMMdd";
private static final String DATE_FORMAT2 = "yyyy/MM/dd";

private static final SimpleDateFormat legacyFormatter = new SimpleDateFormat(LEGACY_FORMAT);
private static final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
private static final SimpleDateFormat shortFormatter = new SimpleDateFormat(SHORT_DATE);

public static void main(String[] args) {
Date date = new Date();
System.out.println("Default Date Format:" + date);
System.out.println("Legacy Date Format:" + legacyFormatter.format(date));
System.out.println("ISO Date Format:" + isoFormatter.format(date));
System.out.println("Short Date Format:" + shortFormatter.format(date));
// Other Date Format
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT1);
System.out.println("Short Date Format1:" + formatter.format(date));
formatter = new SimpleDateFormat(DATE_FORMAT2);
System.out.println("Short Date Format2:" + formatter.format(date));
formatter = new SimpleDateFormat(MAIN_DATE_FORMAT);
System.out.println("Main Date Format:" + formatter.format(date));
	}
}

Output


Default Date Format:Sat Jul 28 13:35:28 PDT 2018
Legacy Date Format:Sat Jul 28 01:35:28 PDT 2018
ISO Date Format:2018-07-28T13:35:28.245 PDT
Short Date Format:2018-07-28
Short Date Format1:20180728
Short Date Format2:2018/07/28
Main Date Format:2018-07-28T01:35:28.245Z

Date pattern Examples

Date & Time Pattern Result
EEE MMM dd hh:mm:ss zzz yyyySat Jul 28 01:35:28 PDT 2018
yyyy-MM-dd'T'HH:mm:ss.SSS zzz2018-07-28T13:35:28.245 PDT
yyyy-MM-dd'T'hh:mm:ss.SSS'Z'2018-07-28T01:35:28.245Z
yyyy-MM-dd2018-07-28
yyyyMMdd20180728
yyyy/MM/dd2018/07/28
yyyy.MM.dd G 'at' HH:mm:ss z2001.07.04 AD at 12:08:56 PDT
EEE, MMM d, ''yyWed, Jul 4, '01
h:mm a12:08 PM
hh 'o''clock' a, zzzz12 o'clock PM, Pacific Daylight Time
K:mm a, z0:08 PM, PDT
yyyyy.MMMMM.dd GGG hh:mm aaa
02001.July.04 AD 12:08 PM
EEE, d MMM yyyy HH:mm:ss ZWed, 4 Jul 2001 12:08:56 -0700
yyMMddHHmmssZ010704120856-0700
yyyy-MM-dd'T'HH:mm:ss.SSSZ2001-07-04T12:08:56.235-0700
yyyy-MM-dd'T'HH:mm:ss.SSSXXX2001-07-04T12:08:56.235-07:00
YYYY-'W'ww-u2001-W27-3
Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.