19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

DateFormat formatter = DateFormat.getDateTimeInstance(<br />

DateFormat.FULL, DateFormat.FULL, Locale.US);<br />

TimeZone timeZone = TimeZone.getTimeZone("CST");<br />

formatter.setTimeZone(timeZone);<br />

System.out.println("The local time is " +<br />

formatter.format(calendar.getTime()));<br />

35.3.3 The SimpleDateFormat Class<br />

<br />

The date and time formatting subclass, SimpleDateFormat, enables you <strong>to</strong> choose<br />

any user-defined pattern for date and time formatting. The construc<strong>to</strong>r shown<br />

below can be used <strong>to</strong> create a SimpleDateFormat object, and the object can be<br />

used <strong>to</strong> convert a Date object in<strong>to</strong> a string with the desired format.<br />

public SimpleDateFormat(String pattern)<br />

The parameter pattern is a string consisting of characters with special<br />

meanings. For example, y means year, M means month, d means day of the month,<br />

G is for era designa<strong>to</strong>r, h means hour, m means minute of the hour, s means<br />

second of the minute, and z means time zone. Therefore, the following code<br />

will display a string like "Current time is 1997.11.12 AD at 04:10:18 PST"<br />

because the pattern is "yyyy.MM.dd G 'at' hh:mm:ss z".<br />

SimpleDateFormat formatter<br />

= new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");<br />

date currentTime = new Date();<br />

String dateString = formatter.format(currentTime);<br />

System.out.println("Current time is " + dateString);<br />

35.3.4 The DateFormatSymbols Class<br />

<br />

The DateFormatSymbols class encapsulates localizable date-time formatting<br />

data, such as the names of the months and the names of the days of the week,<br />

as shown in Figure 35.3.<br />

java.text.DateFormatSymbols<br />

+DateFormatSymbols()<br />

Constructs a DateFormatSymbols object for the default locale.<br />

+DateFormatSymbols(Locale locale) Constructs a DateFormatSymbols object by for the given locale.<br />

+getAmPmStrings(): String[]<br />

Gets AM/PM strings. For example: "AM" and "PM".<br />

+getEras(): String[]<br />

Gets era strings. For example: "AD" and "BC".<br />

+getMonths(): String[]<br />

Gets month strings. For example: "January", "February", etc.<br />

+setMonths(newMonths: String[]): void Sets month strings for this locale.<br />

+getShortMonths(): String[]<br />

Gets short month strings. For example: "Jan", "Feb", etc.<br />

+setShortMonths(newShortMonths: String[]): Sets short month strings for this locale.<br />

void<br />

+getWeekdays(): String[]<br />

Gets weekday strings. For example: "Sunday", "Monday", etc.<br />

+setWeekdays(newWeekdays: String[]): void Sets weekday strings.<br />

+getShotWeekdays(): String[]<br />

Gets short weekday strings. For example: "Sun", "Mon", etc.<br />

+setShortWeekdays(newWeekdays: String[]): Sets short weekday strings. For example: "Sun", "Mon", etc.<br />

void<br />

Figure 35.3<br />

The DateFormatSymbols class encapsulates localizable date-time formatting<br />

data.<br />

For example, the following statement displays the month names and weekday<br />

names for the default locale.<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!