DateTime API
DateTime API
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.
import java.time.*;
public class Test {
public static void main(String[] args)
{
// create an LocalDate object
LocalDate lt = LocalDate.now();
// print result
System.out.println("LocalDate : "+ lt);
}
}
Example (now(ZoneId zone))
// Java program to demonstrate LocalDate.now() method
import java.time.*;
public class Test {
public static void main(String[] args)
{
// create a clock
ZoneId zid = ZoneId.of(“Asia/Kolkata");
// create an LocalDate object using now(zoneId)
LocalDate lt = LocalDate.now(zid);
// print result
System.out.println("LocalDate : "+ lt);
}
}
Example ( of() method)
public static LocalDate of(int year,int month,int dayOfMonth)
// Java program to demonstrate LocalDate.of(int month) method
import java.time.*;
public class Test {
public static void main(String[] args)
{
// create LocalDate object
LocalDate localdate = LocalDate.of(2020, 5, 13);
// print full date
System.out.println("Date: " + localdate);
}
}
Output:
Date:2020-05-13
Example (of() method)
public static LocalDate of(int year,Month month,int dayOfMonth)
// Java program to demonstrate
// LocalDate.of(Month month) method
import java.time.*;
public class Test {
public static void main(String[] args)
{
// create LocalDate object
LocalDate localdate = LocalDate.of(2020, Month.MAY, 13);
// print full date
System.out.println("Date: "+ localdate);
}
}
Output:
Date: 2020-05-13
Example:parse() method
import java.time.*;
public class Test {
public static void main(String[] args)
{
// create an LocalDate object
LocalDate lt = LocalDate.parse("2020-05-13");
// print result
System.out.println("LocalDate : "+ lt);
}
}
Example
ldt = LocalDate.parse("2017-02-28”);
LocalTime Class
LocalTime Class
A time without a time-zone in the ISO-8601 calendar system,
such as 10:15:30. 13
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)
Example—now()
// Java program to demonstrate LocalDateTime.now() method
import java.time.*;
public class Test {
public static void main(String[] args)
{
// create an LocalDateTime object
LocalDateTime lt = LocalDateTime.now();
// print result
System.out.println("LocalDateTime : "+ lt);
}
}
Sample output:
LocalDateTime : 2021-02-19T10:03:55.356
Example—now()
// Java program to demonstrate LocalDateTime.now() method
import java.time.*;
public class Main {
public static void main(String[] args)
{
// create a clock
ZoneId zid = ZoneId.of(“Asia/Kolkata");
// create an LocalDateTime object using now(zoneId)
LocalDateTime lt = LocalDateTime.now(zid);
// print result
System.out.println("LocalDateTime : "+ lt);
}
}
Sample Output:
LocalDateTime : 2021-02-20T09:37:12.068
Example—of()
import java.time.*;
public class Main {
public static void main(String[] args)
{
// create LocalDateTime object
LocalDateTime localdatetime1 = LocalDateTime.of(2020, 5, 13, 6, 30);
// print full date and time
System.out.println("DateTime: "+ localdatetime1); //DateTime: 2020-05-13T06:30
LocalDateTime localdatetime2 = LocalDateTime.of(2020, 5, 13, 6, 30,45);
// print full date and time
System.out.println("DateTime: "+ localdatetime2); //DateTime: 2020-05-13T06:30:45
// create LocalDateTime object
LocalDateTime localdatetime3 = LocalDateTime.of(2020, 5, 13, 6, 30, 45, 20000);
// print full date and time
System.out.println("DateTime: "+ localdatetime3); //DateTime: 2020-05-13T06:30:45.000020
}
}
Example-of()
// Java program to demonstrate LocalDateTime.of(LocalDate date, LocalTime time) method
import java.time.*;
public class Main {
public static void main(String[] args)
{
// Create LocalDate object using LocalDate.of() method
LocalDate date = LocalDate.of(2020, 5, 13);
// Create LocalTime object using LocalTime.of() method
LocalTime time = LocalTime.of(6, 30);
// Create LocalDateTime object
LocalDateTime localdatetime = LocalDateTime.of(date, time);
// Print full date and time
System.out.println( "DateTime: " + localdatetime); //DateTime: 2020-05-13T06:30
}
}
Example-parse()
// Java program to demonstrate LocalDateTime.parse() method
import java.time.*;
public class Main {
public static void main(String[] args)
{
// create an LocalDateTime object
LocalDateTime lt = LocalDateTime .parse("2018-12-30T19:34:50.63");
// print result
System.out.println("LocalDateTime : "+ lt);
}
}
Output:
LocalDateTime : 2018-12-30T19:34:50.630