O'Reilly logo

PURE Java™ 2 by Kenneth Litwak

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Formatting Dates and Times

Just as it is possible to format numbers and currency, it is possible with classes from java.text to format dates and times.

Date and Time Using DateFormat: Code Example

The principles are essentially the same as formatting numbers, so I'll get right to the code. Listing 18.5 shows how to format dates and times using both predefined, locale-sensitive formats, and creating custom formats for locale-independent formats.

Code Listing 18.5. Using DateFormat and SimpleDateFormat (TextDriver3.java)
 // Program to format data with help from java.text import java.text.*; import java.util.*; class DateShaper { Locale usLocale; Locale deLocale; DateFormat usDateFormatter; DateFormat deDateFormatter; String formattedDate; Date ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required