One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man

Wednesday, March 27, 2013

Java DateTime Formatting

You can use following mechanism to get the date and time as a format you preferred. You need to use the Calendar class in the java.util package. It is possible to filter the date time using SimpleDateFormat class in the java.text package by passing the format to the constructor.

LetterDate or Time ComponentPresentationExamples
GEra designatorTextAD
MMonth in yearMonthJulyJul07
wWeek in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay in weekTextTuesdayTue
aAm/pm markerTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
KHour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
sSecond in minuteNumber55
zTime zoneGeneral time zonePacific Standard TimePSTGMT-08:00
ZTime zoneRFC 822 time zone-0800

public String getTimeStamp(){
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_hhmmss");

String timeStamp = "";

try {
timeStamp = format.format(date);
} catch (Exception e1) {

return timeStamp;

No comments:

Post a Comment