Header Ads

ad

Java 8 New Features

Date and Time API in Java 8

Until Java 1.7 there are several classes to handle date and time values such as : 
  • Date
  • Calendar
  • TimeStamp etc. 
These are not recommended to use because these classes are not that much convenient to handle date and time and most of the methods have already been deprecated.

So, to handle date and time effectively, a new Date and Time API is introduced in Java 8 also known as Joda Time API because It is developed by joda.org

If you want to use this API, you need to import java.time package.

Display current system date and time using Joda Time API

  1. LocalDate date=LocalDate.now();
  2. System.out.println(date);

  3. Output : 2019-11-16

Note : The output is in the form yyyy-MM-dd

  1. LocalTime time=LocalTime.now();
  2. System.out.println(time);

  3. Output : 23:00:02.330

Note : The output is in the form HH:MM:SS

Java 8 Date and Time API provides several classes which are present in java.time package. 
  • java.time.LocalDate class
  • java.time.LocalTime class
  • java.time.LocalDateTime class
  • java.time.MonthDay class
  • java.time.OffsetTime class
  • java.time.OffsetDateTime class
  • java.time.Clock class
  • java.time.ZonedDateTime class
  • java.time.ZoneId class
  • java.time.ZoneOffset class
  • java.time.Year class
  • java.time.YearMonth class
  • java.time.Period class
  • java.time.Duration class
  • java.time.Instant class
  • java.time.DayOfWeek enum
  • java.time.Month enum








No comments