Java Date and Time

Profile picture for user arilio666

Java provides several classes and libraries for working with date and time.

Java.time.LocalDate: Represents a date (year, month, day) without a time zone or time of day.

LocalDate date =; 
int year = date.getYear(); 
int month = date.getMonthValue(); 
int day = date.getDayOfMonth();

Java.time.LocalTime: Represents a time (hour, minute, second) without a date or time zone.

LocalTime time =; 
int hour = time.getHour();
int minute = time.getMinute(); 
int second = time.getSecond();

Java.time.LocalDateTime: This represents a combination of date and time without a time zone.

LocalDateTime dateTime =; 
int year = dateTime.getYear(); 
int month = dateTime.getMonthValue(); 
int day = dateTime.getDayOfMonth(); 
int hour = dateTime.getHour();
int minute = dateTime.getMinute(); 
int second = dateTime.getSecond();

Java.time.ZonedDateTime: Represents a date, time, and time zone.

ZonedDateTime zonedDateTime =; // Current date, time, and time zone
ZoneId zoneId = zonedDateTime.getZone(); // Get time zone

Java.time.Duration: Represents time, such as the difference between dates or times.

LocalTime startTime = LocalTime.of(9, 30); // Start time
LocalTime endTime = LocalTime.of(12, 30); // End time
Duration duration = Duration.between(startTime, endTime); // Calculate duration
long hours = duration.toHours(); // Get hours
long minutes = duration.toMinutes(); // Get minutes

Java.time.format.DateTimeFormatter: Allows formatting and parsing dates and times according to specific patterns.

LocalDate date = LocalDate.of(2023, 4, 20); // Create a date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); // Create a formatter
String formattedDate = date.format(formatter); // Format the date
System.out.println(formattedDate); // Output: 20-04-2023


These are some commonly used classes and libraries for working with date and time in Java. They are part of the Java standard library and provide comprehensive functionality for various date and time operations.