Home | | Web Programming | Formatting Date and Time with java.text

Chapter: Java The Complete Reference : The Java Library : Regular Expressions and Other Packages

Formatting Date and Time with java.text

The package java.text allows you to format, parse, search, and manipulate text.

Formatting Date and Time with java.text

 

The package java.text allows you to format, parse, search, and manipulate text. This section examines two of its most commonly used classes: those that format date and time information. However, it is important to state at the outset that the new date and time API described later in this chapter offers a modern approach to handling date and time that also supports formatting. Of course, legacy code will continue to use the classes shown here for some time.

 

DateFormat Class

 

DateFormat is an abstract class that provides the ability to format and parse dates and times. The getDateInstance( ) method returns an instance of DateFormat that can format date information. It is available in these forms:

 

static final DateFormat getDateInstance( )

 

static final DateFormat getDateInstance(int style)

 

static final DateFormat getDateInstance(int style, Locale locale)

 

The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. They cause different details about the date to be presented. The argument locale is one of the static references defined by Locale (refer to Chapter 19 for details). If the style and/or locale is not specified, defaults are used.

 

One of the most commonly used methods in this class is format( ). It has several overloaded forms, one of which is shown here:

final String format(Date d)

The argument is a Date object that is to be displayed. The method returns a string containing the formatted information.

The following listing illustrates how to format date information. It begins by creating a Date object. This captures the current date and time information. Then it outputs the date information by using different styles and locales.

 

// Demonstrate date formats.

import java.text.*;

 

import java.util.*;

 

public class DateFormatDemo {

 

public static void main(String args[]) { Date date = new Date();

 

DateFormat df;

 

df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN); System.out.println("Japan: " + df.format(date));

 

df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA); System.out.println("Korea: " + df.format(date));

 

df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK); System.out.println("United Kingdom: " + df.format(date));

 

df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); System.out.println("United States: " + df.format(date));

}

 

}

 

Sample output from this program is shown here:

 

Japan: 14/01/01

 

Korea: 2014. 1. 1

 

United Kingdom: 01 January 2014

 

United States: Wednesday, January 1, 2014

 

The getTimeInstance( ) method returns an instance of DateFormat that can format time information. It is available in these versions:

 

static final DateFormat getTimeInstance( )

 

static final DateFormat getTimeInstance(int style)

 

static final DateFormat getTimeInstance(int style, Locale locale)

 

The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. They cause different details about the time to be presented. The argument locale is one of the static references defined by Locale. If the style and/or locale is not specified, defaults are used.

 

The following listing illustrates how to format time information. It begins by creating a Date object. This captures the current date and time information. Then it outputs the time information by using different styles and locales.

// Demonstrate time formats.

import java.text.*;

 

import java.util.*;

public class TimeFormatDemo {

 

public static void main(String args[]) { Date date = new Date();

 

DateFormat df;

 

df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.JAPAN); System.out.println("Japan: " + df.format(date));

 

df = DateFormat.getTimeInstance(DateFormat.LONG, Locale.UK); System.out.println("United Kingdom: " + df.format(date));

 

df = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CANADA); System.out.println("Canada: " + df.format(date));

 

}

 

}

 

Sample output from this program is shown here:

 

Japan: 13:06

 

United Kingdom: 13:06:53 CST

 

Canada: 1:06:53 o’clock PM CST

 

The DateFormat class also has a getDateTimeInstance( ) method that can format both date and time information. You may wish to experiment with it on your own.

 

SimpleDateFormat Class

 

SimpleDateFormat is a concrete subclass of DateFormat. It allows you to define your own formatting patterns that are used to display date and time information.

One of its constructors is shown here: SimpleDateFormat(String formatString)

 

The argument formatString describes how date and time information is displayed. An example of its use is given here:

 

SimpleDateFormat sdf = SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");

 

The symbols used in the formatting string determine the information that is displayed. Table 30-4 lists these symbols and gives a description of each.

In most cases, the number of times a symbol is repeated determines how that data is presented. Text information is displayed in an abbreviated form if the pattern letter is repeated less than four times. Otherwise, the unabbreviated form is used. For example, a zzzz pattern can display Pacific Daylight Time, and a zzz pattern can display PDT.

For numbers, the number of times a pattern letter is repeated determines how many digits are presented. For example, hh:mm:ss can present 01:51:15, but h:m:s displays the same time value as 1:51:15.

Finally, M or MM causes the month to be displayed as one or two digits. However, three or more repetitions of M cause the month to be displayed as a text string.

Symbol : - Description

 : -

a : - AM or PM

 : -

d : - Day of month (1–31)

 : -

h : - Hour in AM/PM (1–12)

 : -

k : - Hour in day (1–24)

 : -

m : - Minute in hour (0–59)

 : -

s : - Second in minute (0–59)

 : -

u : - Day of week, with Monday being 1

 : -

w : - Week of year (1–52)

 : -

y : - Year

 : -

z : - Time zone

 : -

D : - Day of year (1–366)

 : -

E : - Day of week (for example, Thursday)

 : -

F : - Day of week in month

 : -

G : - Era (for example, AD or BC)

 : -

H : - Hour in day (0–23)

 : -

K : - Hour in AM/PM (0–11)

 : -

L : - Month

 : -

M : - Month

 : -

S : - Millisecond in second

 : -

W : - Week of month (1–5)

 : -

X : - Time zone in ISO 8601 format

 : -

Y : - Week year

 : -

Z : - Time zone in RFC 822 format

Table 30-4  : - Formatting String Symbols for SimpleDateFormat

The following program shows how this class is used:

 

// Demonstrate SimpleDateFormat. import java.text.*;

import java.util.*;

 

public class SimpleDateFormatDemo {

 

public static void main(String args[]) { Date date = new Date(); SimpleDateFormat sdf;

 

sdf = new SimpleDateFormat("hh:mm:ss"); System.out.println(sdf.format(date));

 

sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz"); System.out.println(sdf.format(date));

sdf = new SimpleDateFormat("E MMM dd yyyy");

 

System.out.println(sdf.format(date));

 

}

 

}

 

Sample output from this program is shown here:

 

01:30:51

 

01 Jan 2014 01:30:51 CST Wed Jan 01 2014

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : Regular Expressions and Other Packages : Formatting Date and Time with java.text |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.