DateTime API
DateTime API
By
Arvind Kumar
Asst. Professor, LPU
Contents
Objective of DateTime API
LocalDate
LocalTime
LocalDateTime
Instant
Period
Duration
Time zones
Objectives of DateTime API
Creating and manage date-based events, Creating and manage
time-based events.
Combining date and time into a single object.
Working with dates and times across time zones.
Managing changes resulting from daylight savings.
Defining and create timestamps, periods and durations.
Applying formatting to local and zoned dates and times
LocalDate Class
Working with LocalDate
LocalDate is a class that holds an event date: a birth date,
anniversary, meeting date and so on.
A date is label for a day.
LocalDate uses ISO calendar by default.
LocalDate does not uses time so its portable across time zones.
LocalDate, LocalTime and LocalDateTime
They are local in the sense that they represent date and time
from the context of one observer, in contrast to time zones.
All the core classes in the new API are constructed by
factory methods.
When constructing a value through its fields, the factory is
called of.
There are also parse methods that take strings as parameters:
LocalDate
Methods
public static LocalDate now()
public static LocalDate now(ZoneID id)
Methods:
public static DateTimeFormatter ofPattern(String pattern)
public static DateTimeFormatter ofLocalizedDate(FormatStyle
dateStyle)
DateTimeFormatter
Pattern Symbols
Symbol Meaning Presentation Examples
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
y year-of-era year 2004; 04
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
E day-of-week text Tue; Tuesday;
V time-zone ID zone-id America/Los_Angeles; Z;
-08:30
a am-pm-of-day text PM
DateTimeFormatter
Constants:
public static final DateTimeFormatter ISO_LOCAL_DATE;
public static final DateTimeFormatter ISO_ORDINAL_DATE;
DateTimeFormatter
ZonedDateTime now = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
System.out.println(now.format(formatter));
formatter = DateTimeFormatter.ISO_ORDINAL_DATE;
System.out.println(now.format(formatter));
FormatStyle values:
SHORT
MEDIUM
LONG
FULL
DateTimeFormatter
ZonedDateTime now = ZonedDateTime.now();
DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(now.format(formatter));