[!NOTE] Historically, working with Dates in Java was notoriously awful. The original
java.util.Dateclass was confusing, mutable, and lacked basic timezone support.
In Java 8, Oracle completely overhauled the system and introduced the java.time package, standardizing how dates should be computed.
The Big Three Classes
- LocalDate
Represents a date (Year, Month, Day) with no time and no timezone. Perfect for birthdays or national holidays.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println(today); // Output: 2024-05-14
// You can also create specific dates!
LocalDate releaseDate = LocalDate.of(1995, 5, 23);
}
}
- LocalTime
Represents a time (Hour, Minute, Second, Nanosecond) with no date. Perfect for "Opening Store Hours".
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalTime rightNow = LocalTime.now();
System.out.println(rightNow); // Output: 14:35:10.827364
}
}
- LocalDateTime
A combination of both. Still essentially "floating" in space, completely untethered to a timezone. This is effectively "The time that is physically reading on your wristwatch".
import java.time.LocalDateTime;
LocalDateTime current = LocalDateTime.now();
Formatting Dates for Users
By default, Java prints dates in ISO-8601 format (YYYY-MM-DD). However, end users generally prefer localized strings, like 14-May-2024.
We accomplish this using the DateTimeFormatter.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now();
// The 'E' is the Day of week.
// The 'MMM' is the 3-letter month.
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm:ss");
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("Formatted: " + formattedDate); // E.g., Tue, May 14 2024 14:30:00
}
}
[!TIP] If you are building a global web application, NEVER store
LocalDateTimein your database! Always convert the user's action into UTC usingZonedDateTimeorInstant, save it to the database as UTC, and only ever convert it back to the local time when rendering the frontend UI in the browser.