One of the new and noteworthy introduction in the Java 8 version is update of the Date Time API. Since the initial version of the Java packages, there is no standard API for calculating the date and time data. In the previous versions, there are two packages java.util and java.sql defines the date and time API with different features. Which makes cumbersome for the developers to understand when to use the suitable classes for manipulating date and time data. Java 8 introduces a new package java.time with lots of data time classes for the date manipulation. This would replace the legacy classes.
This tutorial is helpful for preparing the date and time classes as part of the OCAJP 8 certification exam. I have covered all the classes mentioned in the exam topics for date and time API. Objective for certification is “Create and manipulate calendar data using classes from java.time.LocalDateTime?, java.time.LocalDate?, java.time.LocalTime?, java.time.format.DateTimeFormatter?, java.time.Period”.
This tutorial explains the following APIs with simple examples.
- java.time.LocalDate
- java.time.LocalTime
- java.time.LocalDateTime
- java.time.Period
- java.time.format.DateTimeFormatter
java.time.LocalDate
This is a immutable class with the default date format of yyyy-MM-dd. The functionality provided by this class is similar to the one that is provided by java.sql.Date API. java.util.Date provides method now() to get the current date. Note that this method has the overloaded verion to get the current date in the specific zone by passing the ZoneId as the argument to method now().
Let’s look at an example:
package javabeat.net; import java.time.LocalDate; import java.time.ZoneId; /** * LocalDate Examples * @author krishna (www.javabeat.net) * */ public class LocalDateExample { public static void main(String[] args) { LocalDate localDateToday = LocalDate.now(); System.out.println("Today's Date : "+localDateToday); LocalDate localDateZone = LocalDate.now(ZoneId.of("America/Los_Angeles")); System.out.println("Today's Date at Zone America/Los_Angeles : "+localDateZone); } }
Output for the above program:
Today's Date : 2015-04-06 Today's Date at Zone America/Los_Angeles : 2015-04-06
java.time.LocalTime
java.time.LocalTime class is similar to LocalDate, it povides human readable time in the format of hh:mm:ss.zzz. This class also provides the ZoneId support for getting the time for the given ZoneId.
Let’s look at an example for LocalTime class.
package javabeat.net; import java.time.LocalTime; import java.time.ZoneId; /** * LocalTime Example * @author krishna (www.javabeat.net) * */ public class LocalTimeExample { public static void main(String[] args) { LocalTime currentTime = LocalTime.now(); System.out.println("Current Time : " + currentTime); LocalTime localTimeZone = LocalTime.now(ZoneId.of("America/Los_Angeles")); System.out.println("Current Time at America/Los_Angeles : " + localTimeZone); } }
Output for the above program:
Current Time : 20:47:00.725 Current Time at America/Los_Angeles : 08:17:00.729
java.time.LocalDateTime
LocalDateTime is an immutable object that presents a date-time. The default format for the date-time value is yyyy-MM-dd-HH-mm-ss.zzz. LocalDateTime class provides a factory method that takes LocalDate and LocalTime arguments to create LocalDateTime instance.
Let’s look at a simple example.
package javabeat.net; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; public class LocalDateTimeExample { public static void main(String[] args) { LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("Current Date Time : " + localDateTime); LocalDateTime today = LocalDateTime.of(LocalDate.now(), LocalTime.now()); System.out.println("Current DateTime="+today); LocalDateTime localDateTimeZone = LocalDateTime.now(ZoneId.of("America/Los_Angeles")); System.out.println("Current Date Time at America/Los_Angeles : " + localDateTimeZone); } }
Out for the above program:
Current Date Time : 2015-04-06T21:52:55.996 Current DateTime=2015-04-06T21:52:55.997 Current Date Time at America/Los_Angeles : 2015-04-06T09:22:56.004
java.time.format.DateTimeFormatter
Before Java 8, the standard Java approach for formatting the dates was using DateFormat and SimpleDateFormat. With Java 8, the preferred date/time classes are not longer maintained in the java.util package and the new date/time handling classes are part of java.time package. In the same way, preferred date/time formatting classes are no longer in the java.text package and the new formatting classes are found in java.time.format package.
With Java 8, DateTimeFormatter class will be primarily used for formatting date/time data for Java application. This class also part of OCAJP 8 exam topics. The DateTimeFormatter class provides ofPattern methods to provide an instance of DateTimeFormatter based on the given date/time pattern as String.
Let’s look at a simple example.
package javabeat.net; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class DateTimeFormatterExample { public static void main(String[] args) { DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter .ofPattern("yyyy/MM/dd HH:mm:ss z"); DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter .ofPattern("yyyy/MM/dd"); DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter .ofPattern("dd/MMM/YYYY"); ZonedDateTime zonedDateTime = ZonedDateTime.now(); String formatter1 = dateTimeFormatter1.format(zonedDateTime); String formatter2 = dateTimeFormatter2.format(zonedDateTime); String formatter3 = dateTimeFormatter3.format(zonedDateTime); System.out.println(formatter1); System.out.println(formatter2); System.out.println(formatter3); } }
Output for the above program will be:
2015/04/07 07:14:51 IST 2015/04/07 07/Apr/2015
java.time.Period
Period provides the quantity or amount of time in terms of years, months and days. If you want to find the exact period between two dates, Period class would provide you the details with the in-built methods. This class is immutable and thread-safe.
Let’s look at a simple example that demonstrates how to use Period class.
package javabeat.net; import java.time.LocalDate; import java.time.Period; public class PeriodExample { public static void main(String[] args) { LocalDate localDate1 = LocalDate.of(2014, 03, 03); LocalDate localDate2 = LocalDate.of(2015, 07, 01); Period period = Period.between(localDate1, localDate2); System.out .println("03-March-2014 to 01-July-2015 :Years (" + period.getYears() + "), Months(" + period.getMonths() + "), Days(" + period.getDays() + ")"); } }
Output for the above program will be:
03-March-2014 to 01-July-2015 :Years (1), Months(3), Days(28)
Sample Mock Exams – OCAJP 8 Date Time Classes
1. Which of the following package declares the class DateTimeFormatter?
- java.util
- java.sql
- java.lang
- java.time.format
Correct Answer : 4. java.time.format
Explanation: java.time.format is the new package introduced in Java 8 for declaring the date/time formatting APIs. The other classes declared inside this package is DateTimeFormatterBuilder and DecimalStyle.
2. Please choose the correct class name(s) which is declared inside the package java.time.
- Date
- LocalTime
- Period
- DateTime
- LocalDateTime
Correct Answer : 2,3 & 5 (LocalTime, Period and LocalDateTime)
Explanation : Date is defined under the package java.util and DateTime is wrong class name.
3. Read the below syntax and choose the correct option that is valid data for “arg3”
LocalDate localDate = LocalDate.of(arg1, arg2, arg3);
- Month
- Year
- Day
- Syntax is not correct
Correct Answer : 3. Day
Explanation : LocalDate.of(YEAR,MONTH,DAY) returns the LocalDate object based on the parameters passed to this method.