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.

Symbol Meaning Type Example
G Era Text “GG” -> “AD”
y Year Number “yy” -> “03″
“yyyy” -> “2003″
M Month Text or Number “M” -> “7″
“M” -> “12″
“MM” -> “07″
“MMM” -> “Jul”
“MMMM” -> “December”
d Day in month Number “d” -> “3″
“dd” -> “03″
h Hour (1-12, AM/PM) Number “h” -> “3″
“hh” -> “03″
H Hour (0-23) Number “H” -> “15″
“HH” -> “15″
k Hour (1-24) Number “k” -> “3″
“kk” -> “03″
K Hour (0-11 AM/PM) Number “K” -> “15″
“KK” -> “15″
m Minute Number “m” -> “7″
“m” -> “15″
“mm” -> “15″
s Second Number “s” -> “15″
“ss” -> “15″
S Millisecond (0-999) Number “SSS” -> “007″
E Day in week Text “EEE” -> “Tue”
“EEEE” -> “Tuesday”
D Day in year (1-365 or 1-364) Number “D” -> “65″
“DDD” -> “065″
F Day of week in month (1-5) Number “F” -> “1″
w Week in year (1-53) Number “w” -> “7″
W Week in month (1-5) Number “W” -> “3″
a AM/PM Text “a” -> “AM”
“aa” -> “AM”
z Time zone Text “z” -> “EST”
“zzz” -> “EST”
“zzzz” -> “Eastern Standard Time”
Excape for text Delimiter “‘hour’ h” -> “hour 9″
Single quote Literal “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()));

  }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: