Sub topic 1
Why Java Needed a New Date and Time API
Java has always had classes for dates and times. For many years those classes caused bugs that were hard to find. In 2014, Java 8 added a new set of classes called java.time. This chapter explains what went wrong with the old classes, so you can see why the new ones work the way they do.
The Old Classes
An API is a set of classes and methods that a library gives you to use. Before Java 8, the date and time API had two main classes:
java.util.Dateis a class that holds a single moment in time, stored as milliseconds since January 1, 1970.java.util.Calendaris a class that splits a moment into parts such as year, month, and day, and lets you change those parts.
What Went Wrong
The old classes have several design flaws.
| Problem | What it means | Why it causes bugs |
|---|---|---|
| Mutable objects | A mutable object can change after you create it | Code you share an object with can change it without you knowing |
| Zero-based months | In `Calendar`, January is 0 and December is 11 | Writing 2 for February actually gives you March |
| Misleading name | `Date` holds a moment in time, with no calendar date in it | Its output depends on the computer's time zone |
| Not thread-safe | `SimpleDateFormat` breaks when several threads use one object | Programs show wrong dates only some of the time |
A thread is one path of work running inside a program. Many programs run several threads at once, which is why the last row matters.
Seeing the Bugs in Code
This program shows two of the problems. Run it with any JDK.
import java.util.Calendar;
import java.util.Date;
public class OldWay {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(2024, 1, 15); // looks like January, but 1 means February
System.out.println(cal.get(Calendar.MONTH)); // 1
Date start = cal.getTime();
Date copy = start; // both names point to the same object
copy.setTime(0); // changing copy also changes start
System.out.println(start.getTime()); // 0
}
}The first bug is easy to make. Most people read 1 as January, but Calendar counts months from 0.
The second bug is harder to spot. copy and start point to the same object. When setTime changes copy, start changes too, because a Date can be modified after it is made.
How java.time Fixes This
The java.time package was designed under JSR 310, a formal proposal for changes to Java. It took many ideas from a popular library called Joda-Time. It fixes the problems above:
- Its objects are immutable, which means they never change after you create them.
- Months are numbered 1 to 12, as on a normal calendar.
- Each kind of value has its own class, such as one for a date only and one for a moment in time.
- Its formatters are safe to share between threads.
Here is February 15, 2024 written with java.time:
import java.time.LocalDate;
public class NewWay {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2024, 2, 15); // 2 means February
System.out.println(date); // 2024-02-15
}
}The next chapter explains immutability in more detail.
Exercise
A programmer writes cal.set(2024, 12, 25) with a Calendar and expects December 25, 2024. What date do they get, and why?
Show answer
They get January 25, 2025. Calendar months start at 0, so December is 11. Month 12 is one past December, and Calendar rolls it over into January of the next year.
Exercise
Name two problems with java.util.Date or Calendar that java.time fixes.
Show answer
Any two of these: Date objects are mutable, Calendar months start at 0, the name Date is misleading because the class holds a moment in time, and SimpleDateFormat is not thread-safe. java.time objects are immutable, its months run from 1 to 12, it has a separate class for each kind of value, and its formatters are thread-safe.