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

Programming in Java: Topic: Date Time API

The document discusses the Java Date Time API and classes for working with dates and times in Java, including LocalDate, LocalTime, and LocalDateTime. LocalDate represents a date without time and allows accessing date fields like year, month, day. LocalTime represents a time without date with nanosecond precision. LocalDateTime combines both date and time. The classes are immutable and thread-safe. Methods like now(), of(), parse() are used to obtain instances.

Uploaded by

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

Programming in Java: Topic: Date Time API

The document discusses the Java Date Time API and classes for working with dates and times in Java, including LocalDate, LocalTime, and LocalDateTime. LocalDate represents a date without time and allows accessing date fields like year, month, day. LocalTime represents a time without date with nanosecond precision. LocalDateTime combines both date and time. The classes are immutable and thread-safe. Methods like now(), of(), parse() are used to obtain instances.

Uploaded by

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

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.
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, 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.
LocalDate Class
LocalDate Class
 A date without a time-zone in the ISO-8601 calendar system,
such as 2007-12-03.

 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”);
Java LocalDate Example

import java.time.LocalDate;  
public class LocalDateExample1
 {  
  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);  
  }  
}  
Java LocalDate Example: isLeapYear()

import java.time.LocalDate;  
public class LocalDateExample2 
{  
 public static void main(String[] args)
 {  
 LocalDate date1 = LocalDate.of(2017, 1, 13);  
System.out.println (date1.isLeapYear());  
 LocalDate date2 = LocalDate.of(2016, 9, 23);  
System.out.println (date2.isLeapYear());  
 }  
}  
Java LocalDate Example: atTime()

import java.time.*;  
public class LocalDateExample3 
{  
public static void main(String[] args)
 {  
LocalDate date = LocalDate.of(2017, 1, 13);  
LocalDateTime datetime = date.atTime(1,50,9);      
System.out.println(datetime);   
}  
}  
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

 static LocalTime now(): It is used to obtain the current time


from the system clock in the default time-zone.
Java LocalTime Example: now()

import java.time.LocalTime;  
public class LocalTimeExample1
 {  
public static void main(String[] args) 
{  
LocalTime time = LocalTime.now();  
System.out.println(time);  
 }  
}  
 static LocalTime of(int hour, int minute, int second) :It is used
to obtain an instance of LocalTime from an hour, minute and
second.
 EXAMPLE:

import java.time.LocalTime;  
public class LocalTimeExample2 
{  
public static void main(String[] args)
 {  
LocalTime time = LocalTime.of(10,43,12);  
System.out.println(time);  
}  
}  
 LocalTime minusHours(long hoursToSubtract): It is used
to return a copy of this LocalTime with the specified
number of hours subtracted.

 LocalTime minusMinutes(long minutesToSubtract): It is


used to return a copy of this LocalTime with the specified
number of minutes subtracted.
import java.time.LocalTime;  
public class LocalTimeExample3 
{  
public static void main(String[] args)
 {  
LocalTime time1 = LocalTime.of(10,43,12);  
System.out.println(time1);  
LocalTime time2= time1.minusHours(2);  
LocalTime time3= time2.minusMinutes(34);  
System.out.println(time3);  
}  
}  
 LocalTime plusHours(long hoursToAdd) :It is used to
return a copy of this LocalTime with the specified
number of hours added.

 LocalTime plusMinutes(long minutesToAdd) :It is


used to return a copy of this LocalTime with the
specified number of minutes added.
import java.time.LocalTime;  
public class LocalTimeExample4 
{  
public static void main(String[] args)
 {  
LocalTime time1 = LocalTime.of(10,43,12);  
System.out.println(time1);  
LocalTime time2= time1.plusHours(4);  
LocalTime time3= time2.plusMinutes(18);  
System.out.println(time3);  
}  
}  
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.
 Java LocalDateTime class is an immutable date-time object that
represents a date-time, with the default format as yyyy-MM-dd-
HH-mm-ss.zzz.

 It inherits object class and implements the


ChronoLocalDateTime interface.

public final class LocalDateTime extends Object  implement
Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate
>, Serializable  
Methods of LocalDateTime

 static LocalDateTime now() :It is used to obtain the current


date-time from the system clock in the default time-zone.

 static LocalDateTime of(LocalDate date, LocalTime time) :It is


used to obtain an instance of LocalDateTime from a date and
time.
import java.time.LocalDateTime;  
import java.time.format.DateTimeFormatter;  
public class LocalDateTimeExample1
{  
public static void main(String[] args)
{  
 LocalDateTime now = LocalDateTime.now();  
System.out.println("Before Formatting: " + now);  
DateTimeFormatter format = DateTimeFormatter.ofPattern("
dd-MM-yyyy HH:mm:ss");  
String formatDateTime = now.format(format);  
System.out.println("After Formatting: " + formatDateTime); 
}  
}  
 import java.time.LocalDateTime;  
 import java.time.format.DateTimeFormatter;  
 public class LocalDateTimeExample2 
 {    
 public static void main(String[] args)
  {     
  LocalDateTime datetime1 = LocalDateTime.now(); 
 DateTimeFormatter format = DateTimeFormatter.ofPattern
("dd-MMyyy HH:mm:ss");   
 String formatDateTime = datetime1.format(format); 
 System.out.println(formatDateTime);   
    }
   } 
Java LocalDateTime Example: minusDays()
import java.time.LocalDateTime;  
import java.time.format.DateTimeFormatter;  
public class LocalDateTimeExample4
 {  
public static void main(String[] args)
 {  
LocalDateTime datetime1 = LocalDateTime.of(2017, 1, 14, 10, 34);  
LocalDateTime datetime2 = datetime1.minusDays(100);  
System.out.println("Before Formatting: " + datetime2);  
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-
yyyy HH:mm");  
String formatDateTime = datetime2.format(format);   
System.out.println("After Formatting: " + formatDateTime );  
}  
}  
Java LocalDateTime Example: plusDays()
import java.time.LocalDateTime; 
 import java.time.format.DateTimeFormatter;  
public class LocalDateTimeExample5
 { 
 public static void main(String[] args)
 {   
 LocalDateTime datetime1 = LocalDateTime.of(2017, 1, 14, 10, 34);  
  LocalDateTime datetime2 = datetime1.plusDays(120);   
 System.out.println("Before Formatting: " + datetime2);   
 DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-
yyyy HH:mm");  
  String formatDateTime = datetime2.format(format);  
  System.out.println("After Formatting: " + formatDateTime );  

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

You might also like