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:
- LocalDate represents just a date(year, month, day) — not time.
- LocalTime represents only a time(hours, minutes, seconds, nanoseconds).
- 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 useuuuu
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
andLocalTime
- 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!
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET