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?
| Class | What does it represent? | State | Recommendation |
|---|---|---|---|
java.util.Date | An instant in milliseconds since the Unix epoch | Mutable, legacy API | Use only when integrating with old code |
java.util.Calendar | A date and calendar-field calculations | Mutable, complex legacy API | Avoid in new code |
LocalDate | A date without a time or time zone | Immutable, modern | Birthdays, holidays and due dates |
ZonedDateTime | A date, time and specific time zone | Immutable, modern | Times displayed to people in different zones |
Instant | An exact instant on the UTC timeline | Immutable, modern | Event timestamps, logs and system exchanges |
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());0
1970-01-01T00:00:00Z
new Date(0)creates the very start of the Unix epoch.getTime()displays elapsed milliseconds since the epoch: zero in this case.toInstant()represents the same instant using the modernInstanttype.
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));2026
8
21
- We set August 14, 2026.
add(..., 7)adds seven days to reach August 21.Calendar.MONTHis zero-based, so when printing we add1.
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());2012-04-18
2012-04-25
2012
of(2012, 4, 18)creates an explicitly specified date.plusDays(7)does not change the original object: it returns a new date.- This is an immutable (immutable) class, so its objects can be shared safely.
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")
));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());2026-08-14T07:30:00Z
2026-08-14T08:15:00Z
45
- The letter
Zmeans UTC. - We add a 45-minute duration to obtain a new
Instant. 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);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);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. DateandCalendarKeep 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 asEurope/Vilniusinstead. - Do not create a shared
SimpleDateFormatfor multiple threads. - Do not depend on the computer's default time zone when business rules require a specific one.
- Do not confuse
java.util.Datewithjava.sql.Date.