Working with Dates in Java: Libraries and Tools
Working with dates and times is one of the key tasks in any application. In Java, the standard java.util.Date and java.util.Calendar classes have long been criticized for their inconvenience, mutability, and poor timezone support. However, with the release of Java 8, the situation changed dramatically. In this article, we will review the main libraries and tools for working with dates in Java that will help you write clean and reliable code.
1. Standard java.time Library (Java 8+)
Starting with Java 8, developers have access to the powerful java.time package, based on the ISO-8601 standard. This is the recommended way to work with dates and times. Main 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 timezone.Instant— a moment in UTC (for machine time).DurationandPeriod— for working with intervals.
Example: creating and formatting a date
import java.time.LocalDate;import java.time.format.DateTimeFormatter;
public class DateExample { public static void main(String[] args) { // Current date LocalDate today = LocalDate.now(); System.out.println("Today: " + today);
// Creating a specific date LocalDate birthDate = LocalDate.of(1990, 5, 15); System.out.println("Birth date: " + birthDate);
// Formatting DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); String formatted = birthDate.format(formatter); System.out.println("Formatted date: " + formatted);
// Parsing a string String dateStr = "2023-12-31"; LocalDate parsedDate = LocalDate.parse(dateStr); System.out.println("Parsed: " + parsedDate); }}Example: calculating the difference between dates
import java.time.LocalDate;import java.time.Period;import java.time.temporal.ChronoUnit;
public class DateDifference { public static void main(String[] args) { LocalDate start = LocalDate.of(2020, 1, 1); LocalDate end = LocalDate.of(2024, 3, 15);
Period period = Period.between(start, end); System.out.println("Difference: " + period.getYears() + " years, " + period.getMonths() + " months, " + period.getDays() + " days");
long daysBetween = ChronoUnit.DAYS.between(start, end); System.out.println("Total days: " + daysBetween); }}2. Joda-Time (for older Java versions)
Before the advent of java.time, the de facto standard was the Joda-Time library. It provided convenient immutable classes and timezone support. If you are working with Java 7 or below, Joda-Time is an excellent choice.
Dependency (Maven):
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.12.7</version></dependency>Example: working with Joda-Time
import org.joda.time.DateTime;import org.joda.time.Days;import org.joda.time.format.DateTimeFormat;import org.joda.time.format.DateTimeFormatter;
public class JodaExample { public static void main(String[] args) { // Current date and time DateTime now = new DateTime(); System.out.println("Now: " + now);
// Creating a date DateTime specificDate = Also in library
Parsing HTML in Java: Best Libraries and Code Examples
Nodemailer for JavaScript (Node.js): Complete Guide with Examples
Rate Limiting in C++: Libraries and Tools for Request Control
Working with Images in TypeScript: resize and crop with Sharp and Jimp
Authorization and Authentication in Kotlin: Ktor, JWT, and Spring Security