Date Time API

 

Issues with Old Date API

  • Thread Safety – The Date and Calendar classes are not thread safe, leaving developers to deal with the headache of hard to debug concurrency issues and to write additional code to handle thread safety. On the contrary the new Date and Time APIs introduced in Java 8 are immutable and thread safe, thus taking that concurrency headache away from developers.
  • APIs Design and Ease of Understanding – The Date and Calendar APIs are poorly designed with inadequate methods to perform day-to-day operations. The new Date/Time APIs is ISO centric and follows consistent domain models for date, time, duration and periods. There are a wide variety of utility methods that support the commonest operations.
  • ZonedDate and Time – Developers had to write additional logic to handle timezone logic with the old APIs, whereas with the new APIs, handling of timezone can be done withLocal and ZonedDate/Time APIs.

Date and Time official documentation.

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. For example, the value “2nd October 2007” can be stored in a LocalDate. This class does not store or represent a time or time-zone.

How to create a LocalDate

// use now() method
LocalDate now = LocalDate.now(ZoneId.systemDefault());//2019-09-10
// use of() method
LocalDate date1 = LocalDate.of(2019, 6, 20);//2019-06-20
// use parse() method
LocalDate date2 = LocalDate.parse("2016-08-16").plusDays(2);//2016-08-18

How to manipulate LocalDate

// add days
LocalDate date2 = LocalDate.now().plusDays(2);//2016-08-18
// minus
date2 = date2.minus(1, ChronoUnit.MONTHS);//2016-07-18
// plus
//date2 = date2.plus(3, ChronoUnit.MONTHS);//2016-10-18

 

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. This class does not store or represent a time-zone.

How to create a LocalDateTime

// use now() method
LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
// use of() method
 LocalDateTime date1 = LocalDateTime.of(2019, 6, 20, 0, 0).plusDays(2);// 2019-06-22T00:00

How to manipulate LocalDateTime

// add days
LocalDateTime date1 = LocalDateTime.of(2019, 6, 20, 0, 0).plusDays(2);//2019-06-22T00:00
// minus
date1 = date1.minus(1, ChronoUnit.MONTHS);//2019-05-22T00:00
// plus
date1 = date1.plus(3, ChronoUnit.MONTHS);//2019-08-22T00:00

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.123456789” can be stored in a LocalTime.

LocalTime now = LocalTime.now(Clock.systemDefaultZone());
System.out.println("now = " + now);
    	
LocalTime time1 = LocalTime.of(12, 6).plusHours(2);
System.out.println("time1 = " + time1);
    	
time1 = time1.minus(1, ChronoUnit.MINUTES);
System.out.println("time1 = " + time1);
    	
time1 = time1.plus(3, ChronoUnit.HOURS);
System.out.println("time1 = " + time1);

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.