Better Programming

Advice for programmers.

Follow publication

Essentials of Java’s Time API

How JSR310 changed the way we handle date and time

Ben Weidig
Better Programming
Published in
10 min readJan 7, 2021
hourglass with sand running
Clear Glass with Red Sand — Nile (Pixabay)

Dealing with date and time is a cumbersome task in many programming languages. But with Java 8, the JDK provides us with a comprehensive and completely new API, changing how we deal with time-related concepts.

Even though JSR310 was introduced with Java 8, the code example will use a Java 10 feature, local variable type inference, to improve readability.
The code examples themselves will all be Java 8 compatible, though.
The // => part of code examples will show the toString() output of the previous line/variable.

Table of ContentsPre-JSR310
The Java Time API (JSR310)
Local Types
Time Zones and Offsets
Other Date and Time Types
General API Design
Android Support
Java Time API for Java 6 and 7
Resources

Pre-JSR310

Before the new API, the JDK provided only a few classes to handle date and time.

Here are the most commonly known ones:

At first glance, these four seem to cover the most common scenarios.

But are they really able to handle all delicate specialties of date and time?

Milliseconds

With java.util.Date being based on milliseconds since Unix timestamp "0", we tend to think of date and time in sums of milliseconds:

Using milliseconds might seem intuitive, but it will lead to bugs eventually.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Ben Weidig
Ben Weidig

Written by Ben Weidig

Software developer, entrepreneur, blogger. Mostly Java, sometimes Swift, Golang, Bash and all the other fun stuff.

No responses yet

Write a response