Java Reference
In-Depth Information
Symbol Meaning
Presentation Examples
Milli-of-day
Number
1234
A
Nano-of-second
Number
987654321
n
Nano-of-day
Number
1234000000
N
Time-zone ID
Zone-id
America/Los_Angeles; Z; -08:30
V
Time-zone name
Zone-name
Pacific Standard Time; PST
z
Zone-offset
Z
for zero
Offset-X
Z; -08; -0830; -08:30; -083015; -08:30:15;
X
Zone-offset
Offset-x
+0000; -08; -0830; -08:30; -083015; -08:30:15;
x
Zone-offset
Offset-Z
+0000; -0800; -08:00;
Z
Localized zone-offset
Offset-O
GMT+8; GMT+08:00; UTC-08:00;
O
Pad next
Pad modifier
1
p
NOTE
y
and
u
work the same for A.D. years; however, for a year of 3 B.C., y pattern returns 3,
whereas u pattern returns -2 (aka proleptic year).
Example 6-1
contains some examples of converting in both directions between strings and
dates.
Example 6-1. DateFormatter.java—Example date formatting and parsing
public
public class
class
DateFormatter
DateFormatter
{
public
public static
static
void
void
main
(
String
[]
args
) {
// Format a date ISO8601-like but with slashes instead of dashes
DateTimeFormatter df
=
DateTimeFormatter
.
ofPattern
(
"yyyy/LL/dd"
);
System
.
out
.
println
(
df
.
format
(
LocalDate
.
now
()));
// Parse a String to a date using the same formatter
System
.
out
.
println
(
LocalDate
.
parse
(
"2014/04/01"
,
df
));