Formatting a Date Using a Custom Format
A pattern of special characters is used to specify the format of the
date. This example demonstrates some of the characters. For a
complete listing, see the javadoc documentation for the
SimpleDateFormat class.
Note: This example formats dates using the default locale
(which, in the author's case, is Locale.ENGLISH). If the example is
run in a different locale, the text (e.g., month names) will not be
the same.
Format formatter;
// The year
formatter = new SimpleDateFormat("yy"); // 02
formatter = new SimpleDateFormat("yyyy"); // 2002
// The month
formatter = new SimpleDateFormat("M"); // 1
formatter = new SimpleDateFormat("MM"); // 01
formatter = new SimpleDateFormat("MMM"); // Jan
formatter = new SimpleDateFormat("MMMM"); // January
// The day
formatter = new SimpleDateFormat("d"); // 9
formatter = new SimpleDateFormat("dd"); // 09
// The day in week
formatter = new SimpleDateFormat("E"); // Wed
formatter = new SimpleDateFormat("EEEE"); // Wednesday
// Get today's date
Date date = new Date();
// Some examples
formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);
// 01/09/02
formatter = new SimpleDateFormat("dd-MMM-yy");
s = formatter.format(date);
// 29-Jan-02
// Examples with date and time; see also
// Formatting the Time Using a Custom Format
formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
s = formatter.format(date);
// 2002.01.29.08.36.33
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
s = formatter.format(date);
// Tue, 09 Jan 2002 22:14:02 -0500
thanks is good jejejej XD very good
@Anonymous: kupal ka jejemon kahihiyan ka ng lahi natin
what about PM/AM how can i specify it ?
specify PM/AM by using a:
I am getting Parse Exception with this code, do you see any problem here?
String stringDate = "Friday, December 31, 2010 11:06:42 AM WST";
DateFormat formatter ;
Date dateGMT ;
formatter = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss a Z");
dateGMT = (Date)formatter.parse(stringDate);
It looks like you miss ',' after MMM in the string.
it should be "E, dd MMM, yyyy hh:mm:ss a Z".
What does this display?
Date today;
SimpleDateFormat formatter;
formatter = new SimpleDateFormat( );
System.out.println("Today is " + formatter.format(today));
How to convert date/time to EST?
Very good and very nice
Very useful
thank U for that, Very nice :)
how to format date like this..,
12th January
> specify PM/AM by using a:
Just use "a", NOT "a colon".
What format does a string need to be in for Java to interpret the date correctly?
I am trying to use your examples above to get a date to display in the correct format that I need and I am not having any luck. Here is my code:
SimpleDateFormat formatter = new SimpleDateFormat("MMMM/dd/yyyy");
Date myDate = new Date();
String = formatter.format(myDate);
label1.setText("Today's Date: " + myDate);
I am trying to add a date to a tabbed JPane so it displays the date like the following:
Today's Date: February 16, 2012.
All I keep getting is: Today's Date: Thu Feb 16 13:56:52 CST 2012
I am so frustrated. Please help. All I want is the month, day and year. How do I code the date format so it will do that?
this comments has been
// 01/09/02
formatter = new SimpleDateFormat("MMMM dd,yyyy");
s = formatter.format(date);
s = "Today's Date: " + s;
System.out.println(s);