Printing Dates and Times

With the conversion character t or T, we can print dates and times in various formats. Conversion character t or T is always followed by a conversion suffix character that specifies the date and/or time format. When conversion character T is used, the output is displayed in uppercase letters. Figure 28.6 lists the common conversion suffix characters for formatting date and time compositions that display both the date and the time. Figure 28.7 lists the common conversion suffix characters for formatting dates. Figure 28.8 lists the common conversion suffix characters for formatting times. To view the complete list of conversion suffix characters, visit the Web site java.sun.com/j2se/5.0/docs/api/java/util/Formatter.html.

Figure 28.6. Date and time composition conversion suffix characters.

(This item is displayed on page 1332 in the print version)

Conversion

suffix character

Description

c

Display date and time formatted as

day month date hour:minute:second time-zone year

with three characters for day and month, two digits for date, hour, minute and second and four digits for yearfor example, Wed Mar 03 16:30:25 GMT-05:00 2004. The 24-hour clock is used. In this example, GMT-05:00 is the time zone.

F

Display date formatted as year-month-date with four digits for the year and two digits each for the month and the date (e.g., 2004-05-04).

D

Display date formatted as month/day/year with two digits each for the month, day and year (e.g., 03/03/04).

r

Display time formatted as hour:minute:second AM|PM with two digits each for the hour, minute and second (e.g., 04:30:25 PM). The 12-hour clock is used.

R

Display time formatted as hour:minute with two digits each for the hour and minute (e.g., 16:30). The 24-hour clock is used.

T

Display time formatted as hour:minute:second with two digits for the hour, minute and second (e.g., 16:30:25). The 24-hour clock is used.

Figure 28.7. Date formatting conversion suffix characters.

Conversion

suffix character

Description

A

Display full name of the day of the week (e.g., Wednesday).

a

Display the three-character short name of the day of the week (e.g., Wed).

B

Display full name of the month (e.g., March).

b

Display the three-character short name of the month (e.g., Mar).

d

Display the day of the month with two digits, padding with leading zeros as necessary (e.g., 03).

m

Display the month with two digits, padding with leading zeros as necessary (e.g., 07).

e

Display the day of month without leading zeros (e.g., 3).

Y

Display the year with four digits (e.g., 2004).

y

Display the last two digits of the year with leading zeros as necessary (e.g., 04).

j

Display the day of the year with three digits, padding with leading zeros as necessary (e.g., 016).

Figure 28.8. Time formatting conversion suffix characters.

Conversion

suffix character

Description

H

Display hour in 24-hour clock with a leading zero as necessary (e.g., 16).

I

Display hour in 12-hour clock with a leading zero as necessary (e.g., 04).

k

Display hour in 24-hour clock without leading zeros (e.g., 16).

l

Display hour in 12-hour clock without leading zeros (e.g., 4).

M

Display minute with a leading zero as necessary (e.g., 06).

S

Display second with a leading zero as necessary (e.g., 05).

Z

Display the abbreviation for the time zone (e.g., GMT-05:00, stands for Eastern Standard Time, which is 5 hours behind Greenwich Mean Time).

P

Display morning or afternoon marker in lower case (e.g., pm).

p

Display morning or afternoon marker in upper case (e.g., PM).

Figure 28.9 uses the conversion character t together with the conversion suffix characters to display dates and times in various formats. Conversion character t requires the corresponding argument to be of type long, Long, Calendar or Date (both in package java.util)objects of each of these classes can represent dates and times. Class Calendar is the preferred class for this purpose because some constructors and methods in class Date are replaced by those in class Calendar. Line 10 invokes static method getInstance of Calendar to obtain a calendar with the current date and time. Lines 1317, 2022 and 2526 use this Calendar object in printf statements as the value to be formatted with conversion character t. Note that lines 2022 and 2526 use the optional argument index ("1$") to indicate that all format specifiers in the format string use the first argument after the format string in the argument list. You will learn more about argument indices in Section 28.11. Using the argument index eliminates the need to repeatedly list the same argument.

Figure 28.9. Formatting dates and times with conversion character t.

1 // Fig. 28.9: DateTimeTest.java 2 // Formatting dates and times with conversion character t and T. 3 import java.util.Calendar; 4 5 public class DateTimeTest 6 { 7 public static void main( String args[] ) 8 { 9 // get current date and time 10 Calendar dateTime = Calendar.getInstance(); 11 12 // printing with conversion characters for date/time compositions 13 System.out.printf( "%tc ", dateTime ); 14 System.out.printf( "%tF ", dateTime ); 15 System.out.printf( "%tD ", dateTime ); 16 System.out.printf( "%tr ", dateTime ); 17 System.out.printf( "%tT ", dateTime ); 18 19 // printing with conversion characters for date 20 System.out.printf( "%1$tA, %1$tB %1$td, %1$tY ", dateTime ); 21 System.out.printf( "%1$TA, %1$TB %1$Td, %1$TY ", dateTime ); 22 System.out.printf( "%1$ta, %1$tb %1$te, %1$ty ", dateTime ); 23 24 // printing with conversion characters for time 25 System.out.printf( "%1$tH:%1$tM:%1$tS ", dateTime ); 26 System.out.printf( "%1$tZ %1$tI:%1$tM:%1$tS %tP", dateTime ); 27 } // end main 28 } // end class DateTimeTest  

Tue Jun 29 11:17:21 GMT-05:00 2004 2004-06-29 06/29/04 11:17:21 AM 11:17:21 Tuesday, June 29, 2004 TUESDAY, JUNE 29, 2004 Tue, Jun 29, 04 11:17:21 GMT-05:00 11:17:21 AM  

Категории