Date and Time in Java

Beknazar
4 min readAug 17, 2021

--

In this article, we will go over how to work with dates and times in Java by using local date & time classes. These classes are available from Java 8.

We are going to discuss:

  1. LocalDate represents just a date(year, month, day) — not time.
  2. LocalTime represents only a time(hours, minutes, seconds, nanoseconds).
  3. LocalDateTime represents date and time.

LocalDate

We can use java.time.LocalDate.java to represent a date in our program as an object. In addition, it has many useful methods to manipulate its value.

In the above example, we are getting our current date and just printing it out.

Also, we can construct our date object manually for any specific date.

We can get each value separately

even day of the week.

We can add and subtract specific values from the date

  • We can add and subtract years, months, days, and weeks from our date object.
  • Note LocalDate is an immutable object so, in order to change it, we need to reassign it and most of the methods return an instance of it.

There are more useful methods in LocalDate. They are pretty intuitive by their names.

Let’s talk about formating our local dates.

  • We can get a local date in different string formats by using java.time.format.DateTimeFormatter.java class. The pattern we provide follows regular expression.

Now let’s see how to get a date from a string.

  • if our string is in default format (YYYY-MM-dd), we can convert to date object just in one step.
  • if you have your dates in a different format than the default one, you have to use DateTimeFormatter to provide your custom pattern.
  • YYYY pattern doesn’t work so we need to use uuuu for the year.

LocalTime

LocalTime is a class to work with time in java.

  • It’s similar to LocalDate, but it’s used only for a time(hours, minutes, seconds, and nanoseconds).
  • It is immutable as well.
  • LocalTime.now() that’s how we get the current time.

There are a lot more useful methods where we can add, substruct, and compare.

If we want to have time in a different format, we can use DateTimeFormatter as well.

LocalDateTime

The LocalDateTime is used if we need to work with dates and times at the same time.

  • LocalDateTime can work dates and times as well.
  • The methods are really similar to LocalDate and LocalTime
  • After T time goes

Let’s see more examples

Few more examples of formatting the LocalDateTime object.

Let’s see String to LocalDateTime

Summary

To work with dates only, you can use LocalDate, and to work with time only, you can use LocalTime. LocalDateTime can be used to represent a date and time with one object. You can get the current date & time by using LocalDateTime.now() method. DateTimeFormatter can be used to convert date & time objects to String in different formats. Similarly, it can be used to convert String to date & time objects by providing regular expression patterns.

Q&A

Q: How about time zones? Do local date & time classes handle it automatically?
A: No, they don’t. Java suggests to think we are all in the same time zone, but if you really want to have a time zone option, you will need to use ZonedDateTime class.

Q: Where does Java get current date & time information?
A: Simple, from your Operating System where code is running.

Q: What is Period in local date & time framework?
A: Period represents some period of time. It can have years, months, and days.

Q: What is Duration in the local date & time framework?
A: Duration is also some period of time, but can have smaller units. For example the minimum period for Period is 1 day, in contrast Duration can have seconds, milliseconds, and even nanoseconds.

Q: What’s Instant in the local date & time framework?
A: It represents a specific moment in time. For example, by using Instant object you could calculate how much it took to run your program:

Q: What is epoch time?
A: It’s the number of seconds elapsed from 00:00:00 UTC January 1, 1970. It’s a Unix system to describe points in time. LocalDateTime has build-in toEpochSeconds() method to get epoch time.

That’s all for today, thank you for reading!

Resources:
1.Oracle Java Local Date & Time
2.OCP Book

--

--

Beknazar
Beknazar

No responses yet