Handling time is one of the most complex tasks in programming due to time zones, leap years, and daylight savings. In older versions, Java used java.util.Date, which was confusing and buggy. Since Java 8, the java.time API (based on Joda-Time) has become the gold standard, offering a set of immutable and thread-safe classes to manage every aspect of time.
In this tutorial, we'll explore the core classes of the java.time package, how to display and format dates, perform calculations, and measure time intervals.
The modern API splits date and time into specific, logical classes so you only use what you need:
Represents only the date (Year, Month, Day). Use this for birthdays or holidays. Example: 2026-02-09
Represents only the time (Hour, Minute, Second, Nanosecond). Example: 14:30:05
A combination of both Date and Time. Example: 2026-02-09T14:30:05
To get the current system time, every core class provides a static now() method.
By default, Java prints dates in the ISO-8601 format (YYYY-MM-DD). To change this, we use the DateTimeFormatter class.
Common Patterns:
dd-MM-yyyy → 09-02-2026E, MMM dd yyyy → Mon, Feb 09 2026yyyy/MM/dd HH:mm:ss → 2026/02/09 11:45:00
DateTimeFormatter myFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = myDateObj.format(myFormat);
Because these classes are Immutable, methods like plusDays() or minusHours() do not change the original object. Instead, they return a new object with the result.
plusDays(long) / minusDays(long)plusMonths(long) / minusMonths(long)plusWeeks(long) / minusYears(long)withYear(int) (Changes specifically the year)If you want to know how much time has passed between two points, Java provides two specific classes:
This example demonstrates how to create a specific date, add time to it, and format it for the user.
If you are building a global app, LocalDateTime is not enough because it doesn't know where you are. You must use ZonedDateTime.
It combines LocalDateTime with a ZoneId (like "Asia/Kolkata" or "UTC"). This ensures that a meeting scheduled for 10 AM in New York appears at the correct time for a user in London.
Q: Why is the new Date API called "Immutable"?
A: It means the objects cannot be modified. If you add a day to a LocalDate, it doesn't change the current object; it creates a brand new one. This prevents bugs in multi-threaded environments.
Q: What is the difference between Instant and LocalDateTime?
A: Instant represents a single point on the timeline in UTC (useful for timestamps in databases). LocalDateTime is a "wall-clock" time that doesn't have a time zone attached to it.
Q: How do you convert a String to a Date?
A: Using the parse() method. Example: LocalDate date = LocalDate.parse("2026-02-09");
Mastering dates in Java is about knowing which class to pick for the job. Use LocalDate for simple dates, LocalDateTime for local logs, and ZonedDateTime for global systems. Always use DateTimeFormatter to ensure your data is readable and professional.
Next: Mastering the ArrayList Collection →