New Date and Time API: Session: 13
New Date and Time API: Session: 13
New Date-Time API overcomes issues faced by earlier version of date and time library such
as:
Time-zone
Thread- Poor
handling
safe issue design
issue
Lo calDate OffsetTime
Clock
Time
Du ZonedDate
LocalTime Period
ration Time
Clock
Clock class in Java 8 version is used to get the date and time using
current time-zone.
Duration
Following Code Snippet shows the usage of plusDays() and minusDays()
methods:
Instant
(java.time.instant)
Instant class helps in time stamp
creation.
Generating an Instant:
An instance of an Instant can be generated using one of the Instant class factory methods.
Code Snippet shows an Instant object representing the exact moment of now, using method Instant.now().
Instant Calculations:
Code Snippet displays the use of Instant in nanoseconds and
milliseconds.
getDayOfYear ()
getDayOfWeek
getYear ()
()
getMonth getDayOfMonth
() ()
Notice how getMonth() and getDayOfWeek() methods return an enum instead of an int. These
enums can provide their data as int values by calling their getValue() methods.
LocalDate Calculations:
LocalDate A set of date calculations can be achieved with the LocalDate
class
using one or more of following methods:
minusYears
()
LocalTime LocalTime class in Date-Time API signifies exact time of day without
Class any
time-zone data.
Creating a LocalTime Class:
Fixed offsets
Geographical regions
Enum
Enum
Enum
Type Safe Has its Used inside New constants
own switch can be added
name statements without
space breaking the
existing code
Parsing dates from strings and formatting dates to strings is possible with the
java.text.SimpleDateFormat class.