Formatting the Time Using a Custom Format

A pattern of special characters is used to specify the format of the time. 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 hour (1-12)
formatter = new SimpleDateFormat("h");     // 8
formatter = new SimpleDateFormat("hh");    // 08

// The hour (0-23)
formatter = new SimpleDateFormat("H");     // 8
formatter = new SimpleDateFormat("HH");    // 08

// The minutes
formatter = new SimpleDateFormat("m");     // 7
formatter = new SimpleDateFormat("mm");    // 07

// The seconds
formatter = new SimpleDateFormat("s");     // 3
formatter = new SimpleDateFormat("ss");    // 03

// The am/pm marker
formatter = new SimpleDateFormat("a");     // AM

// The time zone
formatter = new SimpleDateFormat("z");     // PST
formatter = new SimpleDateFormat("zzzz");  // Pacific Standard Time
formatter = new SimpleDateFormat("Z");     // -0800

// Get today's date
Date date = new Date();

// Some examples
formatter = new SimpleDateFormat("hh:mm:ss a");
String s = formatter.format(date);
// 01:12:53 AM

formatter = new SimpleDateFormat("HH.mm.ss");
s = formatter.format(date);
// 14.36.33

Comments

9 Oct 2011 - 9:12am by Janese (not verified)

Way to go on this essay, hepeld a ton.

9 Oct 2011 - 9:13am by Janese (not verified)

Way to go on this essay, hepeld a ton.

5 Jan 2012 - 8:54pm by home loans (not verified)

People in the world get the mortgage loans from different creditors, because this is simple and comfortable.

15 May 2012 - 6:14pm by adidas jeremy scott shoes (not verified)

Adidas Blue Challenge has a pleasant and light fragrance that is perfect Adidas Porsche Design Golf for daily use.It is known for its sporty fresh fragrance ideal for those who have an active lifestyle.If you use Adidas cologne, women will definitely go nuts over you.Adi and his brother Rudi started Adidas Porsche Design Bounce producing shoes Adidas Porsche Bounce S in their mother's laundry area.In 1996, Adidas decided to Adidas Porsche Bounce S2 start offering its loyal following a wide range of perfumes and colognes including a number of excellent men's cologne products.They are a perfect complement for your Adidas shoes and apparel to complete your athletic and powerful Adidas Porsche Bounce SL image.

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.