Thursday, 10 January 2013

Use SimpleDateFormat to convert format Date and Calender in java

Using SimpleDateFormat for custom date formatting and parsing

The default DateFormat instances returned by the static methods in the DateFormat class may be sufficient for many purposes, but clearly do not cover all possible valid or useful formats for dates. For example, notice that in Figure 2, none of the DateFormat-generated strings (numbers 2 – 9) match the format of the output of the Date class’s toString() method. This means that you cannot use the default DateFormat instances to parse the output of toString(), something that might be useful for things like parsing log data.

The SimpleDateFormat lets you build custom formats. Dates are constructed with a string that specifies a pattern for the dates to be formatted and/or parsed. From the SimpleDateFormatJavaDocs, the characters in Figure 7 can be used in date formats. Where appropriate, 4 or more of the character will be interpreted to mean that the long format of the element should be used, while fewer than 4 mean that a short format should be used.

































































































































SymbolMeaningTypeExample
GEraText“GG” -> “AD”
yYearNumber“yy” -> “03″
“yyyy” -> “2003″
MMonthText or Number“M” -> “7″
“M” -> “12″
“MM” -> “07″
“MMM” -> “Jul”
“MMMM” -> “December”
dDay in monthNumber“d” -> “3″
“dd” -> “03″
hHour (1-12, AM/PM)Number“h” -> “3″
“hh” -> “03″
HHour (0-23)Number“H” -> “15″
“HH” -> “15″
kHour (1-24)Number“k” -> “3″
“kk” -> “03″
KHour (0-11 AM/PM)Number“K” -> “15″
“KK” -> “15″
mMinuteNumber“m” -> “7″
“m” -> “15″
“mm” -> “15″
sSecondNumber“s” -> “15″
“ss” -> “15″
SMillisecond (0-999)Number“SSS” -> “007″
EDay in weekText“EEE” -> “Tue”
“EEEE” -> “Tuesday”
DDay in year (1-365 or 1-364)Number“D” -> “65″
“DDD” -> “065″
FDay of week in month (1-5)Number“F” -> “1″
wWeek in year (1-53)Number“w” -> “7″
WWeek in month (1-5)Number“W” -> “3″
aAM/PMText“a” -> “AM”
“aa” -> “AM”
zTime zoneText“z” -> “EST”
“zzz” -> “EST”
“zzzz” -> “Eastern Standard Time”
Excape for textDelimiter“‘hour’ h” -> “hour 9″
Single quoteLiteral“ss”SSS” -> “45’876″





1. Date() + SimpleDateFormat()




DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));



 2. Calender() + SimpleDateFormat()



DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));



Full example


Here is the full source code to show you the use of Date() and Calender() class to get and display the current date time.


package com.mkyong;

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

public class GetCurrentDateTime {
public static void main(String[] args) {

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));

//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));

}
}


No comments:

Post a Comment