Working with Dates in Java: Overview of Modern Libraries and Tools
Working with dates and times is one of the most common tasks in Java development. For a long time, the standard classes java.util.Date and java.util.Calendar were inconvenient, mutable, and error-prone. Fortunately, the situation has changed. In this overview, we will look at three main approaches: the built-in java.time package, the Joda-Time library, and Apache Commons Lang utilities.
1. java.time (Java 8+) — Modern Standard
Starting with Java 8, the java.time package, based on the ideas of Joda-Time, was included in the standard library. This is the primary and recommended way to work with dates in new projects.
Key classes:
LocalDate— date without time (year, month, day).LocalTime— time without date (hours, minutes, seconds, nanoseconds).LocalDateTime— combination of date and time.ZonedDateTime— date and time with time zone.Instant— moment in time in UTC (machine-readable).DurationandPeriod— for working with time intervals.
Usage examples:
import java.time.*;import java.time.format.DateTimeFormatter;import java.time.temporal.ChronoUnit;
public class JavaTimeExample { public static void main(String[] args) { // Current date and time LocalDate today = LocalDate.now(); LocalTime now = LocalTime.now(); System.out.println("Today: " + today); System.out.println("Now: " + now);
// Creating a specific date LocalDate birthDate = LocalDate.of(1990, Month.MAY, 15); System.out.println("Birth date: " + birthDate);
// Working with time zones ZonedDateTime berlinTime = ZonedDateTime.now(ZoneId.of("Europe/Berlin")); ZonedDateTime tokyoTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo")); System.out.println("In Berlin: " + berlinTime); System.out.println("In Tokyo: " + tokyoTime);
// Difference between dates long daysBetween = ChronoUnit.DAYS.between(birthDate, today); System.out.println("Days since birthday: " + daysBetween);
// Formatting DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"); String formatted = LocalDateTime.now().format(formatter); System.out.println("Formatted: " + formatted);
// Parsing a string LocalDate parsedDate = LocalDate.parse("2023-12-31"); System.out.println("Parsed: " + parsedDate); }}2. Joda-Time — Time-Tested Alternative
Before the release of Java 8, Joda-Time was the de facto standard for working with dates. Currently, its use is justified only in legacy projects or when support for older Java versions (below 8) is needed.
Main features:
- Immutable objects — thread safety.
- Support for multiple calendar systems (Islamic, Buddhist, etc.).
- Simple and intuitive API.
Code example with Joda-Time:
import org.joda.time.*;import org.joda.time.format.DateTimeFormat;import org.joda.time.format.DateTimeFormatter;
public class JodaTimeExample { public static void main(String[] args) { // Current date and time DateTime now = DateTime.now(); System.out.println("Now: " + now);
// Creating a date DateTime specificDate = new DateTime(2020, 6, 15, 10, 30, 0); System.out.println("Specific date: " + specificDate);
// Adding days DateTime nextW Also in library
Working with CSV in Rust: the csv library and data processing
CLI Frameworks for Kotlin: Clikt, kotlinx-cli, picocli — Overview and Comparison
WebSocket Libraries for Node.js: ws, Socket.IO, uWS — Complete Overview
Kotlin Task Scheduler: kotlinx-coroutines-core and korGE Library
Caching in Node.js: Redis and Alternatives for Accelerating Applications