Arbeiten mit Datum und Uhrzeit in Java: Eine ausführliche Anleitung
What is it and why? (a real-life analogy)
Imagine you are keeping a diary. Every day you write: "Today is March 15, 14:30. I did my homework." Without a date and time, your entries would turn into a mess — you wouldn't know when something happened. In programming, it's the same: the computer needs to know exactly when an event occurred.
Let's imagine your program is a smart alarm clock. It should:
- Know what time it is (to wake you up at 7:00)
- Remember when you go to bed (so it doesn't ring at night)
- Understand that February 31 doesn't exist (so it doesn't break)
- Be able to calculate how many days are left until New Year
In real life, we use calendars and clocks. In Java, there are special "smart clocks" for this — classes for working with dates and times. They can do everything regular clocks can, but they can also:
- Compare dates (which happened earlier?)
- Calculate the difference between dates (how many days have passed?)
- Format the date (show it in Russian "15 марта 2024" or in English "March 15, 2024")
- Check if a year is a leap year
💡 Important: In older versions of Java (before 8), working with dates was very inconvenient. Now we use the modern API — java.time. It appeared in Java 8 (2014) and is considered the best way to work with dates and times.
Theory: how it works internally
The computer doesn't understand "March 15" the way we do. For it, time is simply a huge number. This number is the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC. This date is called the Unix epoch (or Epoch).
Why 1970? Because that's when the Unix operating system was created, and the developers decided: "Let time be counted from this date." Since then, all computers in the world use this reference point.
Let's see how it works:
- January 1, 1970, 00:00:00 — is 0 milliseconds
- January 1, 1970, 00:00:01 — is 1000 milliseconds
- January 1, 2024, 00:00:00 — is approximately 1,704,067,200,000 milliseconds
When you write LocalDate.of(2024, 3, 15) in code, Java internally does approximately the following:
- Checks if such a date exists (no February 30)
- Calculates the number of days from 1970 to this date
- Saves this number in memory
In Java 8+, there are several main classes for working with dates and times. They are located in the java.time package:
| Class | What it stores | Example |
|---|---|---|
LocalDate |
Only date (year, month, day) | 2024-03-15 |
LocalTime |
Only time (hours, minutes, seconds) | 14:30:00 |
LocalDateTime |
Date and time together | 2024-03-15T14:30:00 |
ZonedDateTime |
Date, time, and time zone | 2024-03-15T14:30:00+03:00 |
Instant |
Moment in time (for machines) | 2024-03-15T11:30:00Z |
Duration |
Difference in time (hours, minutes) | PT2H30M (2 hours 30 minutes) |
Period |
Difference in dates (years, months, days) | P1Y2M3D (1 year 2 months 3 days) |
⚠️ Important note: All classes from java.time are immutable. This means you cannot change an existing date object. Instead, you create a new object with the changed values. This is done for safety — so you don't accidentally "break" a date in another part of the program.
Syntax in Java: all forms and options
Let's go over all the main ways to work with dates and times. We will use the modern java.time API.
1. Creating dates and times
There are several ways to create a date object:
import java.time.*;
public class CreateDates {
public static void main(String[] args) {
// Method 1: Current date and time
LocalDate today = LocalDate.now(); // 2024-03-15
LocalTime now = LocalTime.now(); // 14:30:00.123
LocalDateTime current = LocalDateTime.now(); // 2024-03-15T14:30:00.123
// Method 2: Specify a specific date
LocalDate birthday = LocalDate.of(2010, 5, 20); // May 20, 2010
LocalTime alarm = LocalTime.of(7, 30); // 07:30
LocalDateTime meeting = LocalDateTime.of(2024, 12, 31, 23, 59);
// Method 3: From a string
LocalDate parsed = LocalDate.parse("2024-03-15"); // YYYY-MM-DD
LocalTime parsedTime = LocalTime.parse("14:30:00"); // HH:MM:SS
// Method 4: With time zone
ZonedDateTime moscowTime = ZonedDateTime.now(ZoneId.of("Europe/Moscow"));
ZonedDateTime newYorkTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
}
}
2. Getting parts of a date
You can "extract" individual parts from a date:
LocalDate date = LocalDate.of(2024, 3, 15);
int year = date.getYear(); // 2024
int month = date.getMonthValue(); // 3 (number)
Month monthEnum = date.getMonth(); // MARCH (enumeration)
int day = date.getDayOfMonth(); // 15
int dayOfYear = date.getDayOfYear(); // 75 (March 15 is the 75th day of the year)