Java 8 introduced a modern java.time
package that replaces the old Date
and Calendar
classes. This blog post will walk you through the most commonly used classes: LocalDate
, LocalTime
, LocalDateTime
, and ZonedDateTime
, along with practical examples.
β
1. LocalDate
β Date Only (No Time or Zone)
LocalDate
is used to represent a date without time or timezone, like 2025-05-21
.
Example: LocalDate in Action
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample {
public static void main(String[] args) {
// Current date
LocalDate today = LocalDate.now();
System.out.println("Today: " + today);
// Specific date
LocalDate independenceDay = LocalDate.of(2025, 7, 4);
System.out.println("Independence Day: " + independenceDay);
// Parse from string
LocalDate parsed = LocalDate.parse("2025-12-25");
System.out.println("Parsed Date: " + parsed);
// Formatting
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM uuuu");
System.out.println("Formatted: " + today.format(formatter));
// Date arithmetic
LocalDate nextWeek = today.plusWeeks(1);
System.out.println("One week later: " + nextWeek);
}
}
β° 2. LocalTime
β Time Only (No Date or Zone)
LocalTime
is for time-only values, like 14:30:00
.
Example: LocalTime in Action
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class LocalTimeExample {
public static void main(String[] args) {
// Current time
LocalTime now = LocalTime.now();
System.out.println("Now: " + now);
// Specific time
LocalTime lunchTime = LocalTime.of(12, 30);
System.out.println("Lunch time: " + lunchTime);
// Parse from string
LocalTime parsed = LocalTime.parse("18:45:00");
System.out.println("Parsed Time: " + parsed);
// Formatting
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
System.out.println("Formatted: " + now.format(formatter));
// Time arithmetic
LocalTime plusHour = now.plusHours(1);
System.out.println("One hour later: " + plusHour);
}
}
π 3. LocalDateTime
β Date and Time (No Zone)
LocalDateTime
combines date and time, useful for timestamps without zone handling.
Example: LocalDateTime in Action
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample {
public static void main(String[] args) {
// Current date-time
LocalDateTime now = LocalDateTime.now();
System.out.println("Now: " + now);
// Specific date-time
LocalDateTime meeting = LocalDateTime.of(2025, 5, 21, 14, 0);
System.out.println("Meeting: " + meeting);
// Parse from string
LocalDateTime parsed = LocalDateTime.parse("2025-08-15T10:15:30");
System.out.println("Parsed DateTime: " + parsed);
// Formatting
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM uuuu, hh:mm a");
System.out.println("Formatted: " + now.format(formatter));
// Date-time arithmetic
LocalDateTime tomorrowSameTime = now.plusDays(1);
System.out.println("Same time tomorrow: " + tomorrowSameTime);
}
}
π 4. ZonedDateTime
β Date and Time with Zone
ZonedDateTime
includes zone and offset information. This is vital for global applications.
Example: ZonedDateTime in Action
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeExample {
public static void main(String[] args) {
// Current date-time with system zone
ZonedDateTime now = ZonedDateTime.now();
System.out.println("Now (default zone): " + now);
// Specific zone
ZoneId parisZone = ZoneId.of("Europe/Paris");
ZonedDateTime parisTime = ZonedDateTime.now(parisZone);
System.out.println("Paris time: " + parisTime);
// From LocalDateTime + Zone
ZonedDateTime meetingInNY = ZonedDateTime.of(
2025, 12, 31, 23, 59, 0, 0,
ZoneId.of("America/New_York")
);
System.out.println("NYE meeting in NY: " + meetingInNY);
// Parsing with zone
ZonedDateTime parsed = ZonedDateTime.parse("2025-07-01T12:00:00+02:00[Europe/Paris]");
System.out.println("Parsed ZonedDateTime: " + parsed);
// Formatting
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM uuuu HH:mm z");
System.out.println("Formatted Paris time: " + parisTime.format(formatter));
// Zone conversion
ZonedDateTime inTokyo = parisTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
System.out.println("Same instant in Tokyo: " + inTokyo);
}
}
π§ Final Thoughts
- Use
LocalDate
andLocalTime
for simple calendar and clock tasks. - Use
LocalDateTime
for timestamps without needing time zone handling. - Use
ZonedDateTime
when you need to track events across different regions of the world. - All classes in
java.time
are immutable and thread-safe.
Top comments (0)