0% found this document useful (0 votes)
303 views

Advanced Java Programming: Topic: Date Time API

The document discusses Java's Date Time API and its main classes for working with dates, times, and timestamps including LocalDate for dates, LocalTime for times of day, LocalDateTime for combining dates and times, and DateTimeFormatter for formatting and parsing date-time values. The classes allow creating and manipulating dates, times, periods, and durations while accounting for time zones and daylight savings time.

Uploaded by

Ajay
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
303 views

Advanced Java Programming: Topic: Date Time API

The document discusses Java's Date Time API and its main classes for working with dates, times, and timestamps including LocalDate for dates, LocalTime for times of day, LocalDateTime for combining dates and times, and DateTimeFormatter for formatting and parsing date-time values. The classes allow creating and manipulating dates, times, periods, and durations while accounting for time zones and daylight savings time.

Uploaded by

Ajay
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Advanced Java Programming

Topic: Date Time API


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)

 public static LocalDate of(int year, Month month, int


dayOfMonth)
 public static LocalDate of(int year, int month, int dayOfMonth)
 public static LocalDate ofEpochDay(long epochDay)

 public static LocalDate parse(CharSequence text)


LocalDate
Methods
 public int getYear()
 public int getMonthValue()
 public Month getMonth()
 public int getDayOfMonth()
 public int getDayOfYear()
 public DayOfWeek getDayOfWeek()
 public boolean isLeapYear()
 public boolean isAfter(ChronoLocalDate other)
 public boolean isBefore(ChronoLocalDate other)
 public boolean isEqual(ChronoLocalDate other)
Let’s Do It
WAP to perform following activities on the date enter by user in
format : YYYY-MM-DD
 Is it in future or past?
 Is it in a leap year?
 What day of the week is it?
 What is the day a month from now?
 What is the date on next Tuesday?
LocalTime Class
LocalTime
 LocalTime stores the time within a day
 Measured from midnight
 Based on a 24-hour clock (13:30 is 1:30 PM.)
LocalTime
Methods
 public static LocalTime now()
 public static LocalTime now(ZoneId zone)
 public static LocalTime of(int hour, int minute)
 public static LocalTime of(int hour, int minute, int second)
 public static LocalTime of(int hour, int minute, int second, int
nanoOfSecond)
 public static LocalTime parse(CharSequence text)
LocalTime
Methods
 public int getHour()
 public int getMinute()
 public int getSecond()
 public int getNano()
Let’s Do It
WAP to perform following activities on the Time enter by user in
format: HH:MM:SS
 When is my lunch time?
 Is lunch time in the future or past?
 What is the time 1 hour 15 minutes from now?
 How many minutes until lunch time?
 How many hours until bedtime?

Train departs Boston at 1:45PM and arrives New York 7:25PM


 How many minutes long is the train ride?
 If the train was delayed 1 hour 19 minutes, what is the actual
arrival time?
LocalDateTime Class
LocalDateTime
 LocalDateTime is a combination of LocalDate and LocalTime

 LocalDateTime is useful for narrowing events.


LocalDateTime
Methods
 public static LocalDateTime now()
 public static LocalDateTime of(int year, Month month, int
dayOfMonth, int hour, int minute)
 public static LocalDateTime of(int year, Month month, int
dayOfMonth, int hour, int minute, int second)
 public static LocalDateTime of(LocalDate date, LocalTime
time)
 public static LocalDateTime parse(CharSequence text)
LocalDateTime
Methods
 public int getYear()

 public LocalDateTime withYear(int year)

 public LocalDateTime plusYears(long years)

 public LocalDateTime minusYears(long years)


Let’s Do It
 A company XYZ have fixed the meeting with company ABC
on 15th December 2016 at 14:00 o’clock. WAP to answer
following queries:
 When is the meeting with corporate?
 When does my flight leave?
 If I move the meeting to Friday, What is the date?

 A training institute is going to start its new batch for Android


training for 15 days on 25th November 2016. WAP to answer
following queries:
 When does the course start?
 If the course start at 9 AM on Monday and ends at 5 PM on Friday,
how many hours am I in class.
DateTimeFormatter
 DateTimeFormatter produces formatted date/times Using
predefined constants, patterns letters, or a localized style

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));

formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy G,


hh:mm a VV");
System.out.println(now.format(formatter));
DateTimeFormatter
 DateTimeFormatter uses the FormatStyle enumeration to
determine how the data is formatted.

 FormatStyle values:
 SHORT
 MEDIUM
 LONG
 FULL
DateTimeFormatter
ZonedDateTime now = ZonedDateTime.now();
DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(now.format(formatter));

You might also like