Format time
In this chapter you will learn:
How to use Java DateFormat to format time
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
.
The argument locale is one of the static references defined by Locale
.
If the style and/or locale is not specified, defaults are used.
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
/*from j av a 2 s .c o m*/
public class Main {
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.US);
System.out.println("United States: " + df.format(date));
}
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Date, Time