Java Reference
In-Depth Information
6.
At the end of the main method, add the following to define date and time formatter
objects:
DateFormat dfShort = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat dfMedium = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat dfLong = DateFormat.getDateInstance(DateFormat.LONG);
DateFormat dfFull = DateFormat.getDateInstance(DateFormat.FULL);
DateFormat tfShort = DateFormat.getTimeInstance(DateFormat.SHORT);
DateFormat tfMedium = DateFormat.getTimeInstance(DateFormat.MEDIUM);
DateFormat tfLong = DateFormat.getTimeInstance(DateFormat.LONG);
7.
At the end of the main method, add the following statements to display the date and time
in the various formats:
System.out.println(dfShort.format(d));
System.out.println(dfMedium.format(d));
System.out.println(dfLong.format(d));
System.out.println(dfFull.format(d));
System.out.println(tfShort.format(d));
System.out.println(tfMedium.format(d));
System.out.println(tfLong.format(d));
The current date and time will appear in the following formats:
Thu Feb 17 14:32:50 EST 2011
2/17/11
Feb 17, 2011
February 17, 2011
Thursday, February 17, 2011
2:32 PM
2:32:50 PM
2:32:50 PM EST
8. Comment out the statements added in steps 6 and 7.
We are going to create a SimpleDateFormat object and define a unique pattern (format). We will then modify the
pattern.
9.
Add the following statements (after the println statement, in the main method), to create
two SimpleDateFormat objects:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat stf = new SimpleDateFormat("hh:mm:ss a");
A pattern is defined with “date format symbols” and constant text. The following is a partial list of the various date
format symbols and the data each symbol represents. As always, upper- and lowercase letters make a difference.
Search WWH ::




Custom Search