Programming in Java
Topic: Date Time API
Contents…
 Introduction
 Local Date
 Local Time
 Local Date Time
Introduction
 New DateTime API is introduced in jdk8.
 LocalDate, LocalTime and LocalDateTime classes are provided
in java.time package.
 The Date class encapsulates the current date and time.
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.
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)
LocalDate Class
LocalDate Class
 A date without a time-zone in the ISO-8601 calendar system,
such as 2021-02-03(YYYY-MM-DD).
 LocalDate is an immutable date-time object that represents a
date, often viewed as year-month-day.
 Other date fields, such as day-of-year, day-of-week and week-
of-year, can also be accessed.
 This class does not store or represent a time or time-zone so its
portable across time zones.
Methods of LocalDate
 public static LocalDate now()
 public static LocalDate now(ZoneId zone)
 public static LocalDate of(int year, Month month, int
dayOfMonth)
 public static LocalDate of(int year, int month, int dayOfMonth)
Note: DateTimeException can be thrown.
 public static LocalDate parse(CharSequence text)
Note: DateTimeParseException can be thrown.
Example
LocalDate ldt = LocalDate.now();
ldt = LocalDate.of(2015, Month.FEBRUARY, 28);
ldt = LocalDate.of(2015, 2, 13);
ldt = LocalDate.parse("2017-02-28”);
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()
Methods of LocalDate
 public LocalDate withYear(int year)
 public LocalDate withMonth(int month)
 public LocalDate withDayOfMonth(int dayOfMonth)
 public LocalDate withDayOfYear(int dayOfYear)
Methods of LocalDate
 public LocalDate plusYears(long yearsToAdd)
 public LocalDate plusMonths(long monthsToAdd)
 public LocalDate plusWeeks(long weeksToAdd)
 public LocalDate plusDays(long daysToAdd)
 public LocalDate minusYears(long yearsToSubtract)
 public LocalDate minusMonths(long monthsToSubtract)
 public LocalDate minusWeeks(long weeksToSubtract)
 public LocalDate minusDays(long daysToSubtract)
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args){
LocalDate date=LocalDate.now();
LocalDate yesterday=date.minusDays(1);
LocalDate tomorrow=yesterday.plusDays(2);
System.out.println("Today date: "+date);
System.out.println("Yesterday date:"+yesterday);
System.out.println("Tommorow date: "+tomorrow);
}
}
import java.time.LocalDate;
import java.time.Month;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println("Present date : " +now);
int year = now.getYear();
Month month = now.getMonth();
int day = now.getDayOfMonth();
System.out.println("year : " + year + " , month : " + month + ",
day : " + day);
int monthInt = now.getMonthValue();
System.out.println("today date : " + year + "-" + monthInt +
"-" + day);
LocalDate oldDate = LocalDate.of(2015, 2,2);
System.out.println("Old date : " + oldDate);
}
}
LocalTime Class
LocalTime Class
 A time without a time-zone in the ISO-8601 calendar system,
such as 10:15:30. 13
 LocalTime is an immutable date-time object that represents a
time, often viewed as hour-minute-second.
 Time is represented to nanosecond precision.
 For example, the value "13:45:30.123" can be stored in a
LocalTime.
 This class does not store or represent a date or time-zone.
Methods of 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 min, int sec, int nsec)
 public static LocalTime parse(CharSequence text)
Methods of LocalTime
Methods
 public int getHour()
 public int getMinute()
 public int getSecond()
 public int getNano()
 public boolean isAfter(LocalTime l)
 public boolean isBefore(LocalTime l)
 public LocalDateTime atDate(LocalDate d)
Methods of LocalTime
Methods
 public LocalTime withHour(int hour)
 public LocalTime withMinute(int min)
 public LocalTime withSecond(int sec)
 public LocalTime withNano(int ns)
Methods of LocalTime
Methods
 public LocalTime plusHours(long hours)
 public LocalTime plusMinutes(long min)
 public LocalTime plusSeconds(long sec)
 public LocalTime plusNanos(long ns)
 public LocalTime minusHours(long hours)
 public LocalTime minusMinutes(long min)
 public LocalTime minusSeconds(long sec)
 public LocalTime minusNanos(long ns)
import java.time.LocalTime;
public class LocalTimeExample {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println("Present time : " +now);
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println("Hour : " + hour + " ,Minute : " + minute + ",
Second : " + second);
int nanoSec = now.getNano();
System.out.println("time now in format : " + hour + ":" + minute
+ ":0" + second + ":" + nanoSec);
LocalTime oldTime = LocalTime.of(13, 20,25);
System.out.println("Old time : " + oldTime);
}
}
LocalDateTime Class
LocalDateTime Class
 A date-time without a time-zone in the ISO-8601 calendar
system, such as 2007-12-03T10:15:30.
 LocalDateTime is an immutable date-time object that
represents a date-time, often viewed as year-month-day-hour-
minute-second.
 Other date and time fields, such as day-of-year, day-of-week
and week-of-year, can also be accessed.
 Time is represented to nanosecond precision.
 For example, the value "2nd October 2007 at
13:45.30.123456789" can be stored in a LocalDateTime.
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)
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
class LocalDateTimeExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("Present date : " +now);
int year = now.getYear();
Month month = now.getMonth();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println("year : " + year + " ,month : " + month + ", day : " + day + "Hour :
" + hour+ " , Minute : " + minute + ", Second :" + second);
LocalDateTime oldDate = LocalDateTime.of(2000, 12, 10, 10, 10, 10);
System.out.println("Old date : " + oldDate);
LocalDate newlocalDate = now.toLocalDate();
System.out.println("newlocalDate:" +newlocalDate);
LocalTime newlocalTime = now.toLocalTime();
System.out.println("newlocalTime:"+ newlocalTime);
LocalDateTime strToDate =LocalDateTime.parse("2020-11-10T10:10:10");
System.out.println("String to Date : " +strToDate);
} }
Working with dates and
times across time zones
Date/Time Formatting
 java.time.format.DateTimeFormatter class is used for
printing and parsing date-time objects.
 This class works by using:
Predefined constants, such as ISO_LOCAL_DATE
Pattern letters, such as yyyy-MMM-dd
Localized styles, such as long or medium
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" ) );
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
class ZonedDateTimeExample {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("India current DateTime : " + currentDateTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy
HH:mm:ss");
ZonedDateTime zonedDateTime =
currentDateTime.atZone(ZoneId.of("Australia/Sydney"));
System.out.println("Aus Zoned time : " + formatter.format(zonedDateTime));
ZoneId zoneId = zonedDateTime.getZone();
System.out.println("zone id : " + zoneId);
ZonedDateTime pstZoneTime = zonedDateTime.withZoneSameInstant(ZoneId.of
("America/Los_Angeles"));
System.out.println("PST time now : " + formatter.format(pstZoneTime));
}
}
ABSTRACT CLASS
vs
INTERFACE
METHOD
vs
CONSTRUCTOR
ENUM
vs
CLASS
METHOD OVERLOADING
vs
METHOD OVERRIDDING
IN JAVA
STATIC BLOCK
vs
ANONYMOUS BLOCK
STATIC VARIABLE
vs
LOCAL VARIABLE
Vs
INSTANCE VARIABLE

15. DateTime API.ppt

  • 1.
  • 2.
    Contents…  Introduction  LocalDate  Local Time  Local Date Time
  • 3.
    Introduction  New DateTimeAPI is introduced in jdk8.  LocalDate, LocalTime and LocalDateTime classes are provided in java.time package.  The Date class encapsulates the current date and time.
  • 4.
    Java Date andTime 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.
  • 5.
    Working with LocalDate 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)
  • 6.
  • 7.
    LocalDate Class  Adate without a time-zone in the ISO-8601 calendar system, such as 2021-02-03(YYYY-MM-DD).  LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day.  Other date fields, such as day-of-year, day-of-week and week- of-year, can also be accessed.  This class does not store or represent a time or time-zone so its portable across time zones.
  • 8.
    Methods of LocalDate public static LocalDate now()  public static LocalDate now(ZoneId zone)  public static LocalDate of(int year, Month month, int dayOfMonth)  public static LocalDate of(int year, int month, int dayOfMonth) Note: DateTimeException can be thrown.  public static LocalDate parse(CharSequence text) Note: DateTimeParseException can be thrown.
  • 9.
    Example LocalDate ldt =LocalDate.now(); ldt = LocalDate.of(2015, Month.FEBRUARY, 28); ldt = LocalDate.of(2015, 2, 13); ldt = LocalDate.parse("2017-02-28”);
  • 10.
    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()
  • 11.
    Methods of LocalDate public LocalDate withYear(int year)  public LocalDate withMonth(int month)  public LocalDate withDayOfMonth(int dayOfMonth)  public LocalDate withDayOfYear(int dayOfYear)
  • 12.
    Methods of LocalDate public LocalDate plusYears(long yearsToAdd)  public LocalDate plusMonths(long monthsToAdd)  public LocalDate plusWeeks(long weeksToAdd)  public LocalDate plusDays(long daysToAdd)  public LocalDate minusYears(long yearsToSubtract)  public LocalDate minusMonths(long monthsToSubtract)  public LocalDate minusWeeks(long weeksToSubtract)  public LocalDate minusDays(long daysToSubtract)
  • 13.
    import java.time.LocalDate; public classLocalDateExample { public static void main(String[] args){ LocalDate date=LocalDate.now(); LocalDate yesterday=date.minusDays(1); LocalDate tomorrow=yesterday.plusDays(2); System.out.println("Today date: "+date); System.out.println("Yesterday date:"+yesterday); System.out.println("Tommorow date: "+tomorrow); } }
  • 14.
    import java.time.LocalDate; import java.time.Month; publicclass LocalDateExample { public static void main(String[] args) { LocalDate now = LocalDate.now(); System.out.println("Present date : " +now); int year = now.getYear(); Month month = now.getMonth(); int day = now.getDayOfMonth(); System.out.println("year : " + year + " , month : " + month + ", day : " + day); int monthInt = now.getMonthValue(); System.out.println("today date : " + year + "-" + monthInt + "-" + day); LocalDate oldDate = LocalDate.of(2015, 2,2); System.out.println("Old date : " + oldDate); } }
  • 15.
  • 16.
    LocalTime Class  Atime without a time-zone in the ISO-8601 calendar system, such as 10:15:30. 13  LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second.  Time is represented to nanosecond precision.  For example, the value "13:45:30.123" can be stored in a LocalTime.  This class does not store or represent a date or time-zone.
  • 17.
    Methods of 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 min, int sec, int nsec)  public static LocalTime parse(CharSequence text)
  • 18.
    Methods of LocalTime Methods public int getHour()  public int getMinute()  public int getSecond()  public int getNano()  public boolean isAfter(LocalTime l)  public boolean isBefore(LocalTime l)  public LocalDateTime atDate(LocalDate d)
  • 19.
    Methods of LocalTime Methods public LocalTime withHour(int hour)  public LocalTime withMinute(int min)  public LocalTime withSecond(int sec)  public LocalTime withNano(int ns)
  • 20.
    Methods of LocalTime Methods public LocalTime plusHours(long hours)  public LocalTime plusMinutes(long min)  public LocalTime plusSeconds(long sec)  public LocalTime plusNanos(long ns)  public LocalTime minusHours(long hours)  public LocalTime minusMinutes(long min)  public LocalTime minusSeconds(long sec)  public LocalTime minusNanos(long ns)
  • 21.
    import java.time.LocalTime; public classLocalTimeExample { public static void main(String[] args) { LocalTime now = LocalTime.now(); System.out.println("Present time : " +now); int hour = now.getHour(); int minute = now.getMinute(); int second = now.getSecond(); System.out.println("Hour : " + hour + " ,Minute : " + minute + ", Second : " + second); int nanoSec = now.getNano(); System.out.println("time now in format : " + hour + ":" + minute + ":0" + second + ":" + nanoSec); LocalTime oldTime = LocalTime.of(13, 20,25); System.out.println("Old time : " + oldTime); } }
  • 22.
  • 23.
    LocalDateTime Class  Adate-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.  LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour- minute-second.  Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed.  Time is represented to nanosecond precision.  For example, the value "2nd October 2007 at 13:45.30.123456789" can be stored in a LocalDateTime.
  • 24.
    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)
  • 25.
    import java.time.LocalDate; import java.time.LocalDateTime; importjava.time.LocalTime; import java.time.Month; class LocalDateTimeExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println("Present date : " +now); int year = now.getYear(); Month month = now.getMonth(); int day = now.getDayOfMonth(); int hour = now.getHour(); int minute = now.getMinute(); int second = now.getSecond(); System.out.println("year : " + year + " ,month : " + month + ", day : " + day + "Hour : " + hour+ " , Minute : " + minute + ", Second :" + second); LocalDateTime oldDate = LocalDateTime.of(2000, 12, 10, 10, 10, 10); System.out.println("Old date : " + oldDate); LocalDate newlocalDate = now.toLocalDate(); System.out.println("newlocalDate:" +newlocalDate); LocalTime newlocalTime = now.toLocalTime(); System.out.println("newlocalTime:"+ newlocalTime); LocalDateTime strToDate =LocalDateTime.parse("2020-11-10T10:10:10"); System.out.println("String to Date : " +strToDate); } }
  • 26.
    Working with datesand times across time zones
  • 27.
    Date/Time Formatting  java.time.format.DateTimeFormatterclass is used for printing and parsing date-time objects.  This class works by using: Predefined constants, such as ISO_LOCAL_DATE Pattern letters, such as yyyy-MMM-dd Localized styles, such as long or medium
  • 28.
    Date/Time Formatting  Thedate-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" ) );
  • 29.
    import java.time.LocalDateTime; import java.time.ZoneId; importjava.time.ZonedDateTime; import java.time.format.DateTimeFormatter; class ZonedDateTimeExample { public static void main(String[] args) { LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("India current DateTime : " + currentDateTime); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm:ss"); ZonedDateTime zonedDateTime = currentDateTime.atZone(ZoneId.of("Australia/Sydney")); System.out.println("Aus Zoned time : " + formatter.format(zonedDateTime)); ZoneId zoneId = zonedDateTime.getZone(); System.out.println("zone id : " + zoneId); ZonedDateTime pstZoneTime = zonedDateTime.withZoneSameInstant(ZoneId.of ("America/Los_Angeles")); System.out.println("PST time now : " + formatter.format(pstZoneTime)); } }
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.