1. LocalDateTime class
The LocalDateTime class combines the capabilities of the LocalDate and LocalTime classes: it stores both a date and a time. Its objects are also immutable, and its methods are similar to those of the LocalDate and LocalTime classes.
Getting the current date and time
Everything is as you would expect here: we use the now() method. Example:
| Code | Console output |
|---|---|
|
|
When displayed on the screen, the date and time are separated by the letter T.
Getting a specific date and time
Unsurprisingly, everything is similar to the LocalDate and LocalTime classes — we use the of() method:
... = LocalDateTime.of(year, month, day, hours, minutes, seconds);
First, there are parameters that specify the date in the same formats as in the LocalDate class. Then there are parameters that specify the time in the same formats as in the LocalTime class. A list of all variations of the of() method is given below:
| Methods |
|---|
|
|
|
|
|
|
|
You can set the date directly or set it indirectly through LocalDate and LocalTime objects:
| Code |
|---|
|
| Console output |
|
The LocalDateTime class has methods for getting elements of a date and/or time. They exactly mirror the methods of the LocalDate and LocalTime classes. We will not repeat them here.
2. Instant class
Java's creators also haven't forgotten about old school ways.
The Date Time API includes an Instant class for working with time intended for processes that occur in computers. Instead of hours, minutes, and seconds, it deals with seconds, milliseconds, and nanoseconds.
This class has two fields that represent:
- the number of seconds that have elapsed since January 1, 1970
- a number of nanoseconds
Was the class made for developers? Yes. That's why it calculates time in Unix-time, which starts at the beginning of 1970.
One could even say that the Instant class is a simplified version of the Date class, retaining only what programmers need.
You can get an Instant object in exactly the same way as a LocalTime object:
Instant timestamp = Instant.now();
Where timestamp is a Instant variable, and Instant.now() is a call to the static now() method of the Instant class.
Example:
| Code | Console output |
|---|---|
|
|
You can also create a new object using variations of the of() method by passing in the time elapsed since January 1, 1970:
|
You need to pass the number of milliseconds |
|
You need to pass the number of seconds |
|
You need pass the seconds and nanoseconds |
Methods available on Instant objects
The Instant class has two methods that return the values of its fields:
|
Number of seconds that have elapsed since January 1, 1970 |
|
Nanoseconds. |
|
Number of milliseconds that have elapsed since January 1, 1970 |
There are also methods to create a new Instant objects based on an existing one:
|
Adds seconds to the current time |
|
Adds milliseconds |
|
Adds nanoseconds |
|
Subtracts seconds |
|
Subtracts milliseconds |
|
Subtracts nanoseconds |
Examples:
| Code | Console output |
|---|---|
|
|
GO TO FULL VERSION