Sunday, July 27, 2008

Java: Date Format

Code Sample:

import java.text.SimpleDateFormat;

java.util.Date today = new java.util.Date();
SimpleDateFormat sdfOutput = new SimpleDateFormat ( "yyyyMMdd" );
String strToday = sdfOutput.format( today );

Date and Time Patterns as below:

Letter: G
Date or Time Component: Era designator
Presentation: Text
Examples: AD

Letter: y
Date or Time Component: Year
Presentation: Year
Examples: 1996; 96

Letter: M
Date or Time Component: Month in year
Presentation: Month
Examples: July; Jul; 07

Letter: w
Date or Time Component: Week in year
Presentation: Number
Examples: 27

Letter: W
Date or Time Component: Week in month
Presentation: Number
Examples: 2

Letter: D
Date or Time Component: Day in year
Presentation: Number
Examples: 189

Letter: d
Date or Time Component: Day in month
Presentation: Number
Examples: 10

Letter: F
Date or Time Component: Day of week in month
Presentation: Number
Examples: 2

Letter: E
Date or Time Component: Day in week
Presentation: Text
Examples: Tuesday; Tue

Letter: a
Date or Time Component: Am/pm marker
Presentation: Text
Examples: PM

Letter: H
Date or Time Component: Hour in day (0-23)
Presentation: Number
Examples: 0

Letter: k
Date or Time Component: Hour in day (1-24)
Presentation: Number
Examples: 24

Letter: K
Date or Time Component: Hour in am/pm (0-11)
Presentation: Number
Examples: 0

Letter: h
Date or Time Component: Hour in am/pm (1-12)
Presentation: Number
Examples: 12

Letter: m
Date or Time Component: Minute in hour
Presentation: Number
Examples: 30

Letter: s
Date or Time Component: Second in minute
Presentation: Number
Examples: 55

Letter: S
Date or Time Component: Millisecond
Presentation: Number
Examples: 978

Letter: z
Date or Time Component: Time zone
Presentation: General time zone
Examples: Pacific Standard Time; PST; GMT-08:00

Letter: Z
Date or Time Component: Time zone
Presentation: RFC 822 time zone
Examples: -0800

Examples
Date and Time Pattern: "yyyy.MM.dd G 'at' HH:mm:ss z"
Result: 2001.07.04 AD at 12:08:56 PDT

Date and Time Pattern: "yyyy.MM.dd G 'at' HH:mm:ss z"
Result: Wed, Jul 4, '01

Date and Time Pattern: "h:mm a"
Result: 12:08 PM

Date and Time Pattern: "hh 'o''clock' a, zzzz"
Result: 12 o'clock PM, Pacific Daylight Time

Date and Time Pattern: "K:mm a, z"
Result: 0:08 PM, PDT

Date and Time Pattern: "yyyyy.MMMMM.dd GGG hh:mm aaa"
Result: 02001.July.04 AD 12:08 PM

Date and Time Pattern: "EEE, d MMM yyyy HH:mm:ss Z"
Result: Wed, 4 Jul 2001 12:08:56 -0700

Date and Time Pattern: "yyMMddHHmmssZ"
Result: 010704120856-0700

Date and Time Pattern: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Result: 2001-07-04T12:08:56.235-0700

No comments:


Since 26 July 2008: