Working with Dates and Time in Java: From A to Z for Beginners

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

Introduction to Working with Dates and Time in Java



Working with dates and time is one of the key tasks in developing any application. Whether it's event logging, calculating delivery deadlines, working with schedules, or data analysis — understanding how Java manages temporal objects is essential. In this article, we will cover all the main aspects: from the old API (Date and Calendar) to the modern java.time package (Java 8+). You will learn how to create, format, compare, and calculate the difference between dates.



1. Old API: Date and Calendar



Before Java 8, the main classes for working with dates were java.util.Date and java.util.Calendar. They are still found in legacy code, so it's important to understand their basic capabilities and limitations.



1.1 The Date Class

Date represents a specific point in time with millisecond precision. However, it has many drawbacks: it is mutable (unsafe in multithreaded applications) and poorly supports internationalization.



import java.util.Date;

public class DateExample { public static void main(String[] args) { // Current date and time Date now = new Date(); System.out.println("Current date: " + now);

// Deprecated way to get the year (1900 + ...) int year = now.getYear() + 1900; System.out.println("Year: " + year); }}


1.2 The Calendar Class

Calendar is a more flexible but cumbersome class. It allows manipulating individual date components (year, month, day).



import java.util.Calendar;

public class CalendarExample { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println("Current time: " + cal.getTime());

// Setting a specific date cal.set(2025, Calendar.MARCH, 15); System.out.println("Set date: " + cal.getTime());

// Adding 7 days cal.add(Calendar.DAY_OF_MONTH, 7); System.out.println("In a week: " + cal.getTime()); }}


Drawbacks: month numbering starts at 0 (January = 0), mutability, lack of simple formatting.



2. New API: java.time (Java 8+)



Starting with Java 8, the java.time package was added to the standard library, based on the ideas of the Joda-Time library. It consists of immutable classes, making the code safe and predictable.



2.1 LocalDate — Date Only

LocalDate represents a date without time (year, month, day). It is ideal for birthdays, calendars.



import java.time.LocalDate;import java.time.Month;

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

// Specific date LocalDate specificDate = LocalDate.of(2025, Month.DECEMBER, 31); System.out.println("New Year: " + specificDate);

// Adding/subtracting days LocalDate nextWeek = today.plusDays(7); System.out.println("In a week: " + nextWeek); }}


2.2 LocalTime — Time Only

LocalTime stores time (hours, minutes, seconds, nanoseconds) without a date reference.



import java.time.LocalTime;

public class LocalTimeExample { public static void main(String[] args) { LocalTime now = LocalTime.now(); System.out.println("Current time: " + now);

LocalTime meeting = LocalTime.of(14, 30); System.out.println("Meeting at: " + meeting);

// Comparing time if (now.isAfter(meeting)) { System.out.println("The meeting has already passed"); } }}<

Blogs

Book Recommendations