Back to Java Course

Java Course

New date time API is introduced in Java 8 to overcome the following drawbacks of old date-time API :

  • Not thread safe : Unlike old java.util.Date which is not thread safe the new date-time API is immutable and doesn't have setter methods.
  • Less operations : In old API there are only few date operations but the new API provides us with many date operations.

Java 8 under the package java.time introduced a new date-time API, most important classes among them are

  • Local : Simplified date-time API with no complexity of timezone handling.
  • Zoned : Specialized date-time API to deal with various timezones.
  • LocalDate/LocalTime: These classes are used to represent date and time respectively, where timezones are not required.
  • ZonedDateTime: Used when the time zone needs to be taken into account alongside the date and time.
  • ChronoUnit Enum: The ChronoUnit enum, added in the new Java 8 API, is used to represent units like day, month, etc., and is available in the java.time.temporal package.
public class NewDateTimeApiExamples { public static void main(String args[]) { // current date and time LocalDateTime currentTime = LocalDateTime.now(); System.out.println("Current DateTime: " + currentTime); LocalDate date1 = currentTime.toLocalDate(); System.out.println("date1: " + date1); Month month = currentTime.getMonth(); int day = currentTime.getDayOfMonth(); int seconds = currentTime.getSecond(); System.out.println("Month: " + month + " day: " + day + " seconds: " + seconds); // current date and time ZonedDateTime date = ZonedDateTime.parse("2019-07-28T10:14:20+05:30[Asia/Kolkata]"); System.out.println("date: " + date); // Get the current date LocalDate currentDate = LocalDate.now(); System.out.println("Current date: " + currentDate); // add 1 week to the current date LocalDate nextWeek = currentDate.plus(1, ChronoUnit.WEEKS); System.out.println("Next week: " + nextWeek); // add 2 month to the current date LocalDate nextMonth = currentDate.plus(2, ChronoUnit.MONTHS); System.out.println("Next month: " + nextMonth); // add 3 year to the current date LocalDate nextYear = currentDate.plus(3, ChronoUnit.YEARS); System.out.println("Next year: " + nextYear); // add 10 years to the current date LocalDate nextDecade = currentDate.plus(1, ChronoUnit.DECADES); System.out.println("Next ten year: " + nextDecade); // comparing dates LocalDate date2 = LocalDate.of(2014, 1, 15); LocalDate date3 = LocalDate.of(2019, 7, 28); if (date2.isAfter(date3)) { System.out.println("date2 comes after date3"); } else { System.out.println("date2 comes before date3"); } // check Leap year if (date1.isLeapYear()) { System.out.println("This year is Leap year"); } else { System.out.println(date1.getYear() + " is not a Leap year"); } //How many days, month between two dates LocalDate newDate = LocalDate.of(2019, Month.DECEMBER, 14); Period periodTonewDate = Period.between(date1, newDate); // Note: getMonths() returns only the "months" component of the Period (0-11), // not the total number of months between the dates. It matches here because // the gap is under a year; for a true total use ChronoUnit.MONTHS.between(date1, newDate). System.out.println("Months left between today and newDate : " + periodTonewDate.getMonths() ); } }

Output

Current DateTime: 2019-07-28T02:26:31.367 date1: 2019-07-28 Month: JULY day: 28 seconds: 31 date: 2019-07-28T10:14:20+05:30[Asia/Kolkata] Current date: 2019-07-28 Next week: 2019-08-04 Next month: 2019-09-28 Next year: 2022-07-28 Next ten year: 2029-07-28 date2 comes before date3 2019 is not a Leap year Months left between today and newDate : 4