CodeGym /Java Course /Module 1. Java Syntax /Helpful nuances to be aware of when working with the Date...

Helpful nuances to be aware of when working with the DateTime API

Module 1. Java Syntax
Level 27 , Lesson 4
Available

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
LocalDateTime time = LocalDateTime.now();
System.out.println("Now = " + time);

Now = 2019-02-22T09:49:19.275039200

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
of (int year, int month, int day, int hour, int minute)
of (int year, int month, int day, int hour, int minute, int second)
of (int year, int month, int day, int hour, int minute, int second, int nano)
of (int year, Month month, int day, int hour, int minute)
of (int year, Month month, int day, int hour, int minute, int second)
of (int year, Month month, int day, int hour, int minute, int second, int nano)
of (LocalDate date, LocalTime time)

You can set the date directly or set it indirectly through LocalDate and LocalTime objects:

Code
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime current = LocalDateTime.of(date, time);
System.out.println("Now = " + current);

LocalDateTime date = LocalDateTime.of(2019, Month.MAY, 15, 12, 15, 00);
System.out.println("Now = " + date);
Console output
Now = 2019-02-22T10:05:38.465675100
Now = 2019-05-15T12:15

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
Instant timestamp = Instant.now();
System.out.println(timestamp);

2019-02-22T08:42:42.234945300Z

You can also create a new object using variations of the of() method by passing in the time elapsed since January 1, 1970:

ofEpochMilli(long milliseconds)
You need to pass the number of milliseconds
ofEpochSecond(long seconds)
You need to pass the number of seconds
ofEpochSecond(long seconds, long nanos)
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:

long getEpochSecond()
Number of seconds that have elapsed since January 1, 1970
int getNano()
Nanoseconds.
long toEpochMilli()
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:

Instant plusSeconds(long)
Adds seconds to the current time
Instant plusMillis(long)
Adds milliseconds
Instant plusNanos(long)
Adds nanoseconds
Instant minusSeconds(long)
Subtracts seconds
Instant minusMillis(long)
Subtracts milliseconds
Instant minusNanos(long)
Subtracts nanoseconds

Examples:

Code Console output
Instant timestamp = Instant.now();
System.out.println(timestamp);

long n = timestamp.toEpochMilli();
Instant time = Instant.ofEpochMilli(n);
System.out.println(time);

2019-02-22T09:01:20.535344Z



2019-02-22T09:01:20.535Z

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION