Working with Dates in Java: Overview of Libraries and Tools

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Dates in Java: Overview of Libraries and Tools



Working with dates and time is one of the most common tasks in Java development. For a long time, the standard classes java.util.Date and java.util.Calendar caused numerous complaints due to their non-intuitiveness, mutability, and thread-safety issues. However, with the release of Java 8, the situation changed dramatically. In this overview, we will look at the main libraries for working with dates in Java, their strengths, and usage examples.



1. java.time (JSR-310) — Standard API



Starting with Java 8, the java.time package is the primary tool for working with dates and time. It was developed under the guidance of Stephen Colebourne, the author of Joda-Time, and incorporates best practices.



Key classes:

  • LocalDate — date without time (year, month, day).
  • LocalTime — time without date (hours, minutes, seconds, nanoseconds).
  • LocalDateTime — date and time without time zone.
  • ZonedDateTime — date and time with time zone.
  • Instant — a moment in time in UTC (machine-readable representation).
  • Duration and Period — for working with time intervals.


Usage example:



import java.time.*;import java.time.format.DateTimeFormatter;

public class JavaTimeExample { public static void main(String[] args) { // Current date LocalDate today = LocalDate.now(); System.out.println("Today: " + today);

// Creating a specific date LocalDate specificDate = LocalDate.of(2025, Month.MARCH, 15); System.out.println("Specific date: " + specificDate);

// Working with time LocalTime now = LocalTime.now(); LocalTime meetingTime = LocalTime.of(14, 30); System.out.println("Meeting time: " + meetingTime);

// Formatting DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm"); LocalDateTime event = LocalDateTime.of(2025, 12, 31, 23, 59); String formatted = event.format(formatter); System.out.println("Formatted: " + formatted);

// Difference between dates Period period = Period.between(today, specificDate); System.out.println("Days until March 15: " + period.getDays()); }}


Advantages: built into JDK, thread-safe, immutable objects, rich API, support for all time zones.



2. Joda-Time



Before the advent of java.time, Joda-Time was the de facto standard for working with dates in Java. Today, its use is justified only in older projects (pre-Java 8), as developers recommend migrating to java.time.



Example:



import org.joda.time.DateTime;import org.joda.time.Days;import org.joda.time.LocalDate;

public class JodaTimeExample { public static void main(String[] args) { // Current date LocalDate today = LocalDate.now(); DateTime now = DateTime.now();

// Difference between dates LocalDate start = new LocalDate(2025, 1, 1); LocalDate end = new LocalDate(2025, 12, 31); int days = Days.daysBetween(start, end).getDays(); System.out.println("Days between: " + days); }}


Disadvantages: requires an external dependency, not actively updated, inferior to java.time in functionality.



3. Apache Commons Lang 3



The library org.apache.commons.lang3.time provides convenient utilities for working with dates, especially for formatting, parsing, and calculations.



Popular classes:

  • DateUtils — methods for

Recommendations