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

Comments

18 Feb 2010 - 3:20pm by Anonymous (not verified)

thanks is good jejejej XD very good

10 Nov 2010 - 2:14am by Anonymous

@Anonymous: kupal ka jejemon kahihiyan ka ng lahi natin

19 Dec 2010 - 5:10am by Anonymous (not verified)

what about PM/AM how can i specify it ?

21 Jan 2011 - 12:11am by noobdstryker (not verified)

specify PM/AM by using a:

6 Feb 2011 - 8:49pm by Anonymous (not verified)

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);

9 Feb 2011 - 7:09am by jan.palencar (not verified)

It looks like you miss ',' after MMM in the string.
it should be "E, dd MMM, yyyy hh:mm:ss a Z".

22 Apr 2011 - 11:46am by Anonymous (not verified)

What does this display?

Date today;
SimpleDateFormat formatter;

formatter = new SimpleDateFormat( );

System.out.println("Today is " + formatter.format(today));

4 Jul 2011 - 3:44am by Anonymous (not verified)

How to convert date/time to EST?

9 Aug 2011 - 6:23am by vignesh (not verified)

Very good and very nice

9 Aug 2011 - 6:24am by vignesh (not verified)

Very useful

16 Oct 2011 - 9:31am by Anonymous (not verified)

thank U for that, Very nice :)

22 Oct 2011 - 3:31am by Irshad (not verified)

how to format date like this..,

12th January

5 Jan 2012 - 9:58am by Guest (not verified)

> specify PM/AM by using a:

Just use "a", NOT "a colon".

26 Jan 2012 - 1:18am by http://buzzbit.net (not verified)

What format does a string need to be in for Java to interpret the date correctly?

16 Feb 2012 - 2:04pm by Anonymous (not verified)

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?

28 Apr 2012 - 1:23pm by Anonymous (not verified)

this comments has been

4 May 2012 - 8:27am by Anonymous (not verified)

// 01/09/02
formatter = new SimpleDateFormat("MMMM dd,yyyy");
s = formatter.format(date);
s = "Today's Date: " + s;
System.out.println(s);

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.