DateTime API
DateTime API
By
Ravi Kant Sahu
Asst. Professor, LPU
Contents…
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
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Why is Date and Time important?
Need to represent time, used to perform calculation.
The current date and time. (locally)
A date and time in future and past.
Difference between two dates/times in seconds, minutes, hours,
years.
Date and time in other country.
Current time after if day light saving is applied.
Number of days in February(Leap Year).
A time duration(hours, minutes and seconds) and period(Days,
Month and Years).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Disadvantages of java.util.Date(Calendar,
TimeZone and DateFormat)
Does not support fluent API approach.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Java Date and Time API goals
Classes and methods should be straight forward.
The API should support fluent API approach.
Instances of Date and Time objects should be immutable.
Should be thread safe.
Use ISO standard to define Date and Time.
API should support strong type checks.
Allows developers to extend API.
toString() always returns human readable format.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Working with Local Date and Time
Java.time package provides two classes for working with local
Date and Time.
LocalDate
Does not include time
A year-month-day representation
toString – ISO 8601 format(YYYY-MM-DD)
LocalTime
Does not include date
Stores hours:minutes:seconds.nanoseconds
toString- (HH:mm:ss.SSS)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.
When converting from another type, the factory is called
from.
There are also parse methods that take strings as parameters:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
LocalDate Class
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
LocalDate Class
A date without a time-zone in the ISO-8601 calendar system,
such as 2007-12-03.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalDate
Methods
public static LocalDate now()
public static LocalDate now(ZoneId zone)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalDate
Methods
public int getYear()
public int getMonthValue()
public Month getMonth()
public int getDayOfMonth()
public int getDayOfYear()
public DayOfWeek getDayOfWeek()
public boolean isLeapYear()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalDate
Methods
public LocalDate with(TemporalAdjuster adjuster)
public LocalDate withYear(int year)
public LocalDate withMonth(int month)
public LocalDate withDayOfMonth(int dayOfMonth)
public LocalDate withDayOfYear(int dayOfYear)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalDate
Methods
public LocalDate plusYears(long yearsToAdd)
public LocalDate plusMonths(long monthsToAdd)
public LocalDate plusWeeks(long weeksToAdd)
public LocalDate plusDays(long daysToAdd)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Exercise
WAP which prompts the user to enter the date of birth and now
display the Day on which the user was born?
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
TemporalAdjusters
TemporalAdjuster object can be obtained using the methods of
TemporalAdjusters class.
Methods:
public static TemporalAdjuster firstDayOfNextYear()
public static TemporalAdjuster firstInMonth(DayOfWeek
dayOfWeek)
public static TemporalAdjuster lastInMonth(DayOfWeek
dayOfWeek)
public static TemporalAdjuster next(DayOfWeek dayOfWeek)
public static TemporalAdjuster previous(DayOfWeek
dayOfWeek)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Let’s Do Something….
Now find out the Day after that many Day, year and month
from the entered Date.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
LocalTime Class
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
LocalTime Class
A time without a time-zone in the ISO-8601 calendar system,
such as 10:15:30. 13
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalTime
Methods
public static LocalTime now()
public static LocalTime now(ZoneId zone)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalTime
Methods
public int getHour()
public int getMinute()
public int getSecond()
public int getNano()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalTime
Methods
public LocalTime with(TemporalAdjuster adjuster)
public LocalTime withHour(int hour)
public LocalTime withMinute(int min)
public LocalTime withSecond(int sec)
public LocalTime withNano(int ns)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalTime
Methods
public LocalTime plusHours(long hours)
public LocalTime plusMinutes(long min)
public LocalTime plusSeconds(long sec)
public LocalTime plusNanos(long ns)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
LocalDateTime Class
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
LocalDateTime Class
A date-time without a time-zone in the ISO-8601 calendar
system, such as 2007-12-03T10:15:30.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalDateTime
Methods
public static LocalDateTime now()
public static LocalDateTime now(ZoneId zone)
public static LocalDateTime of(int year, int mnth, int day, int
hour, int mint)
public static LocalDateTime of(int year, int mnth, int day, int
hour, int mint, int sec)
public static LocalDateTime of(int year, int mnth, int day, int
hour, int mint, int sec, int nsec)
public static LocalDateTime of(LocalDate d, LocalTime t)
public static LocalDateTime parse(CharSequence text)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of LocalDateTime
Methods
public int getHour()
public int getMinute()
public int getSecond()
All other getter methods of LocalDate and LocalTime class
Methods
public LocalDate toLocalDate()
public LocalTime toLocalTime()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Instant
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Using Instant
Instant n = Instant.now();
Instant epochNow =
Instant.ofEpochSecond(1000000);
long s = n.getEpochSecond();
int ns = n.getNano();
Instant twoSecondsAfter = n.plusSeconds(2);
Instant twoSecondsBefore = n.minusSeconds(2);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Period
A period represents a value such as "1 months and 15
days", in contrast to the other classes that represent a point
in time.
NOTE:
We can modify the values of dates using periods:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Duration
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Duration
A duration is similar to a period but its precision is based on
hours, minutes, seconds, milliseconds.
Duration d2 = Duration.between(LocalTime.NOON,
LocalTime.MIDNIGHT);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Working with dates and
times across time zones
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
TImezones and DayLight
Saving
In any given timezone, as you get further away from the equator,
there are more hours of daylight per day in summer than there
are in winter. So, the goal of DST is to maximize the daylight
hours available during typical waking hours.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
DayLight Saving
By adjusting clocks forward on a specific date by a standard
amount, usually one hour, people can take better advantage of
daylight during their typical working day.
For example, suppose you wake daily at 7:00 AM. In the spring,
the sun rises earlier each day. So, instead of waking up at 6:00
AM to take advantage of the extra daylight, DST observers
move their clocks ahead by one hour.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
DayLight Saving
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
DateTimeFormatter
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
DateTimeFormatter
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern(“dd/MM/yyyy");
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
DateTimeFormatter
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern(“dd-MM-yyyy");
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Date/Time Formatting
The date-time classes provide two methods - one for
formatting and one for Parsing.
Formatting Example:
LocalDateTime dt =
LocalDateTime.of( 2010, Month.JULY, 03, 09, 0, 30 );
String isoDateTime =
dt.format(DateTimeFormatter.ISO_DATE_TIME);
Parsing Example:
LocalDate dt = LocalDate.parse("2014/09/19 14:05:12",
DateTimeFormatter.ofPattern( "yyyy/MM/dd kk:mm:ss" ) );
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
ZonedDateTime
2022-03-31T10:15:30+01:00 Europe/Paris
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods
public static ZonedDateTime now()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)