Introduction to java.time - Part 1
This article will provide introduction to the java.time package. This package is introduced in Java 8 version and it contains many classes and interfaces to represent and process dates and times. Below are the five most basic classes you need to know in this package.
LocalDate: a date without a time. You can store year, month and day. If you want to work on only dates with out any time information, then this is the class. For example, you can store birthday dates.
LocalTime: a time without a date and time-zone. You can store hour, minutes and seconds. Stop-watch is the example, where only hours, minutes and seconds are needed.
LocalDateTime: a date and time with out time-zone. This class can represent all date and time fields like year, month, day, hour, minute and second. Date and time of the local foot ball match can be saved in this instance.
ZonedDateTime: a date, time and time-zone. Meeting invite in a calendar may require all these fields. It becomes complex to process time with zone information so its better to use this class only if required.
Instant: a timestamp. This is the long number represents epoch-seconds. For example, a network ping application needs epoch seconds to calculate the timing of the network packets.
All classes in java.time package are thread-safe and all are immutable. Every change to the object results in the new instance of it. This package uses a consistent method prefixes for all standard operations and which made this API to understand easily.
Creating new instance
Use static method now(), to create the instances based on the current time, this method creates instance based on the system clock. In the below example, I created instances using now().
If you want to create instance based on particular time, then use static method of(). Methods with prefix ‘of’ will create instance based on the provided arguments.
There are other overloaded methods for ‘of’ and ‘now’, read the documentation for all available methods. If you want to create instances based on the string then you can use parse method. Parse method takes CharSequence and returns time object. Below example creates LocalDate based on the string.
Note that we need to pass string in ISO format. This method throws DateTimeParseException if you pass different format. ISO formats are the standard, when you want to parse any string then check if the string is a ISO format, if it is, then you can pass it to parse method directly. Below are the list of parse methods in all the classes.
Some times we need to parse different format then we can use another version of the parse method which also takes DateTimeFormatter.
Above example shows the basic usage of parsing with DateTimeFormatter. In this article I am not providing much information on DateTimeFormatter because it is a huge class with lot of methods.
This article provided information about the basic classes in java.time package and the different ways to create there instances. Go through the second part of this post to learn about accessing time fields.