eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Introduction

Java 8 introduced a new set of date and time classes. Knowing which ones to use and when can be confusing. In this tutorial, we’ll look at the difference between the Instant and LocalDateTime classes.

2. The Instant Class

The easiest way to think of the Instant class is as a single moment in time in the UTC time zone. If we think of time as a line, Instant just represents a single point on the line.

Under the hood, the Instant class is really just counting the number of seconds and nanoseconds relative to the standard Unix epoch time of January 1, 1970, at 00:00:00. This point in time is denoted by 0 seconds and 0 nanoseconds, and everything else is just an offset from it.

By storing the number of seconds and nanoseconds relative to this specific point in time, it allows the class to store both negative and positive offsets. In other words, the Instant class can represent times both before and after the epoch time.

2.1. Using Instant

Let’s look at how we can work with the Instant class. First, it provides various static methods for quickly computing an instant along the timeline.

For example, the Instant.now() method gives us an Instant object that represents the current UTC time. We can also do some basic arithmetic with Instant:

Instant twelveHoursFromNow = Instant.now().plus(12, ChronoUnit.HOURS);
Instant oneWeekAgo = Instant.now().minus(7, ChronoUnit.DAYS);

Additionally, we can compare two Instants:

Instant instant1 = Instant.now();
Instant instant2 = instant1.plus(1, ChronoUnit.SECONDS);
assertTrue(instant1.isBefore(instant2));
assertFalse(instant1.isAfter(instant2));

2.2. Limitations

The Instant class is straightforward but does have some drawbacks. First, it doesn’t exactly match other standards that account for leap seconds. Instead of adding or removing a single second at the end of a day, it uses its own time scale that spreads out the second across the final 1000 seconds of that day.

In doing so, it can essentially treat every day as having exactly 86,400 seconds. However, this isn’t how other time standards work, so Instant isn’t as precise.

Also, because Instant simply stores the number of seconds and nanoseconds from a fixed epoch time, it’s limited in how much time it can represent. Specifically, the number of seconds in an Instant is stored using the long data type. This means there’s a limit to how far before and after the Unix epoch time we can represent with an Instant.

Luckily, the min and max are roughly 1 billion years before and after, so this limitation may not impact most applications.

3. The LocalDateTime Class

Now let’s look at the LocalDateTime class. The first thing to know is that, despite its name, it’s not tied to any time zone. Essentially the Local prefix means the date and time are in whatever locality we happen to be in.

We can think of it as simply a calendar set to a specific day of the year, along with a clock at a certain time of day. In fact, the underlying date and time values are stored using LocalDate and LocalTime types, respectively. Both of these values exist independently of any locality. They aren’t part of any particular timeline like the Instant class.

The LocalDateTime class is great at representing a possible event that occurs regardless of time zone. For example, New Year’s Day is always on January 1 at midnight. This exact time happens in every time zone eventually, but clearly not at the same time. While people in London countdown to a new year, people in Los Angeles are happily going about their afternoons.

Thus, on its own, the LocalDateTime class isn’t practical for scenarios that require time zones. Imagine asking a colleague in a different time zone to meet at 3:00 pm on June 15th, 2023.  Or imagine setting a reminder to call a friend at 5:00 pm and then flying across the country. Without any knowledge of the time zone, there’s a good chance we’ll miss the meeting with our colleague and forget to call our friend.

3.1. Using LocalDateTime

Let’s look at some examples of using the LocalDateTime class. We can easily calculate the current date and time for the default time zone using the LocalDateTime.now() method.

Just like Instant, there are a number of static methods that let us supply various combinations of the date and time values:

LocalDateTime.of(2023, 6, 1, 12, 0);
LocalDateTime.of(2023, 6, 1, 12, 0, 0);
LocalDateTime.of(2023, 6, 1, 12, 0, 0, 0);

We can also do basic arithmetic with LocalDateTime:

LocalDateTime tomorrow = LocalDateTime.now().plus(1, ChronoUnit.DAYS);
LocalDateTime oneYearAgo = LocalDateTime.now().minus(1, ChronoUnit.YEARS);

Finally, we can compare two LocalDateTime objects:

LocalDateTime now = LocalDateTime.now();
LocalDateTime y2k = LocalDateTime.of(2000, 1, 1, 0, 0);
assertTrue(now.isAfter(y2k));
assertTrue(y2k.isBefore(now));

4. Other Java Date & Time Classes

So what happens if we need to deal with time zones? We saw above that both Instant and LocalDateTime aren’t equipped for this, but luckily Java provides a number of other classes that handle time zones. Below we’ll briefly look at a few of them.

4.1. ZoneOffset

The ZoneOffset class represents an offset in hours, minutes, and seconds before or after the standard UTC zone. It’s only the offset information only and nothing more. There’s no name, knowledge of Daylight Savings, etc.

4.2. ZoneId

The ZoneId class is much more detailed than the ZoneOffset class. While it also defines the hour, minutes, and seconds from the UTC zone, it contains additional information such as a name, unique ID, Daylight Savings rules, and more.

The rules for specific time zones are set by local governments and thus change somewhat frequently. Therefore the ZoneId class encapsulates all the specific rules of each zone, as well as a history of any changes.

4.3. ZonedDateTime

Finally, we get to the ZoneDateTime class. We can think of this class as an Instant with ZoneId information. While we should always store date and time values using a consistent zone for all users, the ZoneDateTime class is useful for displaying those values in a specific zone for individual users.

5. Conclusion

Java 8 provides a rich set of APIs that are far superior to the legacy Date class for handling date and time. However, knowing which one to use for specific use cases isn’t always obvious.

In this article, we looked at two of the new classes, Instant and LocalDateTime. While they have similar APIs, they are quite different. We saw the Instant is just a negative or positive offset from the Unix epoch time and is always tied to the UTC time zone. We also saw that LocalDateTime is just a calendar and clock without any time zone information.

Both can be useful for different things, but if we require time zone information, neither will be sufficient by itself.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)