Lesson 14 - Date and Time in Java - Parsing and comparing

In the previous lesson, Date and Time in Java - Modifying and intervals, we learned to convert between
LocalDateTime, LocalDate, and LocalTime, modify the inner value, and introduced
time intervals. In today's Java course tutorial, we're going to finish up with
the topic of date and time. We'll get inner values, parse using custom formats,
and compare.

Notice the use of the getMonthValue() to retrieve the month number. We had to
use it, in this case, because getMonth() returns the value of the enumerated
Month type.

Note: If you ever encounter the older Calendar class, beware that months
were zero-based back then (January was 0, not 1 like it is now in
LocalDate/Local­DateTime).

Parsing date and time

As you may already know, date and time often comes as a String, e.g. from the
user through the console, a file or the database. We then create a LocalDateTime
from the string value using the parse() method right on the data type as we're
used to in Java.

The default parse() method expects dates to be in the "yyyy-mm-dd" format,
date and times in the "yyyy-mm-ddThh:mm:ss" format and times in the "hh:mm:ss"
format. All of the numbers have to have leading zeros if they’re less than 10.
The "T" in the example below isn't a typo. It’s more of a separator for date
and time:

Other classes

Aside from LocalDateTime, LocalDate, and LocalTime, you may also encounter
several other classes which you'll use rather than the aforementioned ones in
applications whose main focus is date and time manipulation. Don’t worry,
you'll get by with LocalDateTime in most applications. However, you should be
aware of the existence of the following classes.

Instant

Instant represents a date and time that is not related to the calendar or to
daylight saving. It's stored as a number of nanoseconds since 1/1/1970, which
gives it a certain point in the UTC (universal time) timeline. The following
code will always print the same date and time no matter where you’re
located:

Instant instantNow = Instant.now();
System.out.println(instantNow);

Instant is only aware of universal time, so it'll differ from a particular
area’s local time.

OffsetDateTime and ZonedDateTime

Now you know that Instant is used for universal time and LocalDateTime is
used for a particular area’s local time. We wouldn't be able to get a point in
a timeline from LocalDateTime since it doesn't carry any area information.

Wouldn’t it be great if there was a class where date and time would be
local and also carry area information (timezone)? This way, we would be able to
convert between various time zones. Well, that's exactly what the ZonedDateTime
class is there for.

You may also encounter the OffsetDateTime class in Java, which is an
intermediate Instant with the timezone offset. However, it comes without full
timezone support.

ZoneId

In Java, time zones are represented by the ZoneId class. Here’s an example
of its use (creating an instance based on a time zone):

You may now be thinking, that's a lot of classes. I suggest that you treat it
rather as information to which you may return to when you need it. There are
more classes in Java than in many other programming languages. The best way to
become a solid Java programmer is to be patient and develop some endurance to
the fact. On the other hand, this is why we’re better paid than others We'll get to some more practical
programming in the next lesson so as to take a break from the theoretical
aspects of it all.

Epochas

To top it all off, we’ll get acquainted with some more LocalDateTime
methods.

ofEpochSecond() - A static method allowing us to create a
LocalDateTime instance from a Linux timestamp which was used to store dates in
the past. In returns the number of seconds since 1/1/1970 (the beginning of the
Linux epoch), which is a huge number, and we have to specify the nanoseconds as
well (mostly 0) as well as a timezome (most often ZoneOffset.UTC). The method is
also available on LocalDate as ofEpochDay() where it receives the number of days
rather than seconds.

toEpochSecond() and toEpochDay() - These
methods do the exact opposite of the ones mentioned above. They convert the
instance to the number of seconds/days since 1970.

That is all for date and time in Java 8. We'll code a practical application
in the next lesson, an electronic diary.

The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.

The author learned IT at the Unicorn College - a prestigious college providing education on IT and economics.