Format date and time value
In this chapter you will learn:
- What specifiers to use to format time and date
- How to show month by name and number
- How to display standard 12-hour time format
- How to display complete time and date information
- How to just display hour and minute
- How to display day of month as a decimal
Specifiers for formatting time and date
%t
formats time and date information.
%t
specifier requires the use of a suffix to describe the portion and
precise format of the time or date desired.
The time and date suffixes are shown in the following table.
Suffix | Replaced By |
---|---|
a | Abbreviated weekday name |
A | Full weekday name |
b | Abbreviated month name |
B | Full month name |
c | Standard date and time string formatted as day month date hh::mm:ss tzone year |
C | First two digits of year |
d | Day of month as a decimal (01-31) |
D | month/day/year |
e | Day of month as a decimal (1-31) |
F | year-month-day |
h | Abbreviated month name |
H | Hour (00 to 23) |
I | Hour (01 to 12) |
j | Day of year as a decimal (001 to 366) |
k | Hour (0 to 23) |
l | Hour (1 to 12) |
L | Millisecond (000 to 999) |
m | Month as decimal (01 to 13) |
M | Minute as decimal (00 to 59) |
N | Nanosecond (000000000 to 999999999) |
p | Locale's equivalent of AM or PM in lowercase |
Q | Milliseconds from 1/1/1970 |
r | hh:mm:ss (12-hour format) |
R | hh:mm (24-hour format) |
S | Seconds (00 to 60) |
s | Seconds from 1/1/1970 UTC |
T | hh:mm:ss (24-hour format) |
y | Year in decimal without century (00 to 99) |
Y | Year in decimal including century (0001 to 9999) |
z | Offset from UTC |
Z | Time zone name |
To display minutes, you would use %tM
, and
M
indicates minutes in a two-character field.
The argument corresponding to the %t specifier must be of type Calendar, Date, Long, or long.
Display month by name and number
The following code shows how to use format('%tB %tb %tm', cal, cal, cal) to display month name and month number.
import java.util.Calendar;
import java.util.Formatter;
/*j a v a2 s . c o m*/
public class MainClass {
public static void main(String args[]) {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
// Display month by name and number.
fmt = new Formatter();
fmt.format("%tB %tb %tm", cal, cal, cal);
System.out.println(fmt);
}
}
The output:
Display standard 12-hour time format
The following code displays standard 12-hour time format.
import java.util.Calendar;
import java.util.Formatter;
/*from j a va2 s.c o m*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
// Display standard 12-hour time format.
fmt.format("%tr", cal);
System.out.println(fmt);//
}
}
The output:
Display complete time and date information
The following code uses the %tc
to display time and date information
in a complete form.
import java.util.Calendar;
import java.util.Formatter;
// ja v a 2 s . co m
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
// Display complete time and date information.
fmt = new Formatter();
fmt.format("%tc", cal);
System.out.println(fmt);
}
}
The output:
Display just hour and minute
The following code only shows the hour and minute information.
import java.util.Calendar;
import java.util.Formatter;
//java2s. co m
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
// Display just hour and minute.
fmt = new Formatter();
fmt.format("%tl:%tM", cal, cal);
System.out.println(fmt);
}
}
The output:
Display Day of month as a decimal
The following code displays day of month as a decimal.
import java.util.Calendar;
import java.util.Formatter;
/* ja v a2 s.co m*/
public class MainClass {
public static void main(String args[]) {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
fmt.format("Today is day %te of %<tB, %<tY", cal);
System.out.println(fmt);
}
}
The output:
Next chapter...
What you will learn in the next chapter: