JAVA BASICS IN DETAIL

Java dates and times

Java has the legacy Date and Calendar classes and the modern java.time API. For new code, generally choose java.timebecause its classes are clearer, immutable and thread-safe.

Which class should you choose?

Main Java date and time classes
ClassWhat does it represent?StateRecommendation
java.util.DateAn instant in milliseconds since the Unix epochMutable, legacy APIUse only when integrating with old code
java.util.CalendarA date and calendar-field calculationsMutable, complex legacy APIAvoid in new code
LocalDateA date without a time or time zoneImmutable, modernBirthdays, holidays and due dates
ZonedDateTimeA date, time and specific time zoneImmutable, modernTimes displayed to people in different zones
InstantAn exact instant on the UTC timelineImmutable, modernEvent timestamps, logs and system exchanges
An important distinction

Date and Calendar classes have not been fully removed or entirely marked deprecated. They are considered the legacy (legacy) API, and some Date constructors and methods are marked deprecated. For new code, the recommended API is java.time.

1. java.util.Date

Date stores one instant as milliseconds since January 1, 1970 at 00:00:00 UTC. Its name is misleading: it represents a specific instant, not merely a calendar date.

import java.util.Date;

Date pradzia = new Date(0);

System.out.println(pradzia.getTime());
System.out.println(pradzia.toInstant());
Console output

0
1970-01-01T00:00:00Z

  1. new Date(0) creates the very start of the Unix epoch.
  2. getTime() displays elapsed milliseconds since the epoch: zero in this case.
  3. toInstant() represents the same instant using the modern Instant type.

Avoid: old constructors that directly accept a year, month and day. Their rules are confusing, and many are marked deprecated.

2. java.util.Calendar

Calendar was created for easier date-field manipulation, time zones and locales. It still works but is mutable, broad and has surprising rules.

import java.util.Calendar;

Calendar kalendorius = Calendar.getInstance();
kalendorius.set(2026, Calendar.AUGUST, 14);
kalendorius.add(Calendar.DAY_OF_MONTH, 7);

System.out.println(kalendorius.get(Calendar.YEAR));
System.out.println(kalendorius.get(Calendar.MONTH) + 1);
System.out.println(kalendorius.get(Calendar.DAY_OF_MONTH));
Console output

2026
8
21

  1. We set August 14, 2026.
  2. add(..., 7) adds seven days to reach August 21.
  3. Calendar.MONTH is zero-based, so when printing we add 1.

Recommendation: in new code, instead of Calendar use LocalDate, ZonedDateTime or another suitable java.time class.

3. java.time.LocalDate

LocalDate stores only a year, month and day. It has no hours or time zone, making it suitable for birthdays, vacation days and invoice due dates.

import java.time.LocalDate;

LocalDate gimtadienis = LocalDate.of(2012, 4, 18);
LocalDate poSavaites = gimtadienis.plusDays(7);

System.out.println(gimtadienis);
System.out.println(poSavaites);
System.out.println(gimtadienis.getYear());
Console output

2012-04-18
2012-04-25
2012

  1. of(2012, 4, 18) creates an explicitly specified date.
  2. plusDays(7) does not change the original object: it returns a new date.
  3. This is an immutable (immutable) class, so its objects can be shared safely.
Do not use LocalDate for an exact instant.

“2026-08-14” does not tell you the hour or where in the world an event happened.

4. java.time.ZonedDateTime

ZonedDateTime combines a date, time and actual zone identifier, such as Europe/Vilnius. It applies daylight-saving rules automatically.

import java.time.ZoneId;
import java.time.ZonedDateTime;

ZoneId vilnius = ZoneId.of("Europe/Vilnius");
ZonedDateTime susitikimas = ZonedDateTime.of(
    2026, 8, 14, 10, 30, 0, 0, vilnius
);

System.out.println(susitikimas);
System.out.println(susitikimas.withZoneSameInstant(
    ZoneId.of("America/New_York")
));
Console output

2026-08-14T10:30+03:00[Europe/Vilnius]
2026-08-14T03:30-04:00[America/New_York]

Both lines show the same global instant, but the local clock times differ. The method withZoneSameInstant() preserves the instant and changes only its display zone.

5. java.time.Instant

Instant represents an exact instant on the UTC timeline and suits machine-to-machine data, event times, audit records and time measurements.

import java.time.Duration;
import java.time.Instant;

Instant pradzia = Instant.parse("2026-08-14T07:30:00Z");
Instant pabaiga = pradzia.plus(Duration.ofMinutes(45));

System.out.println(pradzia);
System.out.println(pabaiga);
System.out.println(Duration.between(pradzia, pabaiga).toMinutes());
Console output

2026-08-14T07:30:00Z
2026-08-14T08:15:00Z
45

  1. The letter Z means UTC.
  2. We add a 45-minute duration to obtain a new Instant.
  3. Duration.between() calculates the exact difference between the instants.

Formatting dates and parsing text

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

LocalDate data = LocalDate.of(2026, 8, 14);
DateTimeFormatter formatas = DateTimeFormatter.ofPattern("yyyy-MM-dd");

String tekstas = data.format(formatas);
LocalDate atkurtaData = LocalDate.parse(tekstas, formatas);

System.out.println(tekstas);
System.out.println(atkurtaData);
Console output

2026-08-14
2026-08-14

DateTimeFormatter is immutable and thread-safe. The legacy SimpleDateFormat is mutable and unsafe to share across threads, so it is best avoided in new code.

How do you connect the old and new APIs?

import java.time.Instant;
import java.util.Date;

Instant akimirka = Instant.parse("2026-08-14T07:30:00Z");
Date senaData = Date.from(akimirka);
Instant velAkimirka = senaData.toInstant();

System.out.println(velAkimirka);
Console output

2026-08-14T07:30:00Z

When integrating with an old library, you can temporarily convert Instant to Date and back. In the rest of a new application, it is more convenient to retain java.time types.

Practical selection rules

  • A date without a time: LocalDate.
  • Local time without a zone: LocalDateTime, but only when the zone truly does not matter or is stored separately.
  • Time displayed to people in a specific location: ZonedDateTime.
  • An exact system timestamp: Instant.
  • A time interval: Duration; a calendar difference in years, months or days: Period.
  • Date and Calendar Keep these for legacy compatibility:

What should you avoid?

  • Do not store a birthday as an Instantif the hour and zone have no meaning.
  • Do not store a global event only as a LocalDateTime : without a zone it is ambiguous.
  • Do not use three-letter zone names such as CST; choose an explicit identifier such as Europe/Vilnius instead.
  • Do not create a shared SimpleDateFormat for multiple threads.
  • Do not depend on the computer's default time zone when business rules require a specific one.
  • Do not confuse java.util.Date with java.sql.Date.