Java Reference
In-Depth Information
December 28 2013
12/28/13 10:44:06
10 o'clock AM, Central Standard Time
Solution #2
Use the java.util.Calendar class to obtain the date that you require and then
format that date using the java.text.SimpleDateFormat class. The following
example demonstrates the use of the SimpleDateFormat class:
// Create new calendar
Calendar cal = Calendar.getInstance();
// Create instance of SimpleDateFormat class using pattern
SimpleDateFormat dateFormatter1 = new
SimpleDateFormat("MMMMM dd yyyy");
String result = null;
result = dateFormatter1.format(cal.getTime());
System.out.println(result);
dateFormatter1.applyPattern("MM/dd/YY hh:mm:ss");
result = dateFormatter1.format(cal.getTime());
System.out.println(result);
dateFormatter1.applyPattern("hh 'o''clock' a, zzzz");
result = dateFormatter1.format(cal.getTime());
System.out.println(result);
Running this example would yield the following result:
June 22 2011
06/22/11 06:24:41
06 o'clock AM, Central Daylight Time
As you can see from the results, the DateTimeFormatter and Sim-
pleDateFormat classes make it easy to convert a date into just about any format.
Search WWH ::




Custom Search