Introduction of Java Date
It’s important to work with dates and time in many Java programs, because when we create a to-do app, record when a user logs in, or display the current time.
Handling dates and times correctly makes our application more meaningful and reliable.
Why Do We Need Dates in Java?
Dates and times play an important role in almost every type of program. Here are some common reasons why they’re important:
- Timestamping activities – For example, saving when a file was created or a user logged in.
- Scheduling events – Applications like calendars or reminders need to store and manage future dates.
- Tracking real-world events – Birthdays, appointments, meetings, and deadlines are all based on date and time.
- Displaying formatted dates – Showing the current date or time in a readable format for users, such as 14-Oct-2025, 3:45 PM.
Before Java 8, the main classes were used java.util.Date and java.util.Calendar. These were powerful but sometimes confusing and hard to use correctly.
After Java 8, we can use the java.time package (inspired by the Joda-Time library) that makes data and time handling easier and cleaner.
Legacy Date Handling: The java.util.Date Class
the java.util.Date class was the main way to work with dates and times in Java. It was introduced in the very first version of Java and served as the foundation for time-related programming for many years.
It is now considered an old or legacy approach, because its methods can be confusing and sometimes produce unexpected results. And modern Java developers prefer using the java.time package.
Features of java.util.Date
- It stores both the current date and time together (year, month, day, hour, minute, second).
- You can create a Date object, display it, and compare two dates.
- Many methods like getYear(), getMonth(), and getDay() are deprecated, meaning they are no longer recommended for use.
- It shows time in the system’s default time zone and doesn’t handle different time zones well.
Example: Displaying the Current Date and Time
import java.util.Date;
public class LegacyDateExample {
public static void main(String[] args) {
// Create a Date object representing the current date and time
Date currentDate = new Date();
// Display the current date and time
System.out.println("Current Date and Time: " + currentDate);
}
}
Output:
Current Date and Time: Tue Oct 14 16:45:12 IST 2025
Important Methods in java.util.Date
| Method | Description |
|---|---|
| getTime() | Returns the time in milliseconds since January 1, 1970. |
| after(Date date) | Checks if this date is after the specified date. |
| before(Date date) | Checks if this date is before the specified date. |
| toString() | Converts the date to a human-readable string. |
Example: Comparing Dates
This feature is useful in real-world applications, for example, comparing deadlines, event timings, or checking if a user’s subscription has expired.
import java.util.Date;
public class CompareDates {
public static void main(String[] args) {
// Create the first Date object (current time)
Date currentTime = new Date();
// Create the second Date object (5 seconds earlier)
Date earlierTime = new Date(System.currentTimeMillis() - 5000);
// Compare the two dates
if (currentTime.after(earlierTime)) {
System.out.println("Current time is after the earlier time.");
} else if (currentTime.before(earlierTime)) {
System.out.println("Current time is before the earlier time.");
} else {
System.out.println("Both times are equal.");
}
}
}
Output:
Current time is after the earlier time.
Modern Date Handling: The java.time Package
This package includes modern classes like:
- LocalDate – for handling only dates
- LocalTime – for handling only times
- LocalDateTime – for handling both date and time
How To Working with java.time Classes
1. LocalDate: Handling Dates
LocalDate is used when you only care about the date, not time or timezone. For example, today’s date or someone’s birthday.
Example:
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args) {
// Get the current system date
LocalDate today = LocalDate.now();
// Create a specific date (like birthday)
LocalDate birthDate = LocalDate.of(2000, 5, 20);
// Compare two dates
if (today.isAfter(birthDate)) {
System.out.println("Today is after your birthday.");
} else {
System.out.println("Your birthday is yet to come!");
}
// Print current date
System.out.println("Today's Date: " + today);
}
}
Output:
Today is after your birthday.
Today's Date: 2025-10-14
2. LocalTime: Working with Time
LocalTime handles time without dealing with the date, perfect for tracking hours, minutes, and seconds.
Example:
import java.time.LocalTime;
public class TimeDemo {
public static void main(String[] args) {
// Get current system time
LocalTime now = LocalTime.now();
// Add 2 hours to the current time
LocalTime futureTime = now.plusHours(2);
// Compare times
if (futureTime.isAfter(now)) {
System.out.println("Future time is ahead of current time.");
}
System.out.println("Current Time: " + now);
System.out.println("Time after 2 hours: " + futureTime);
}
}
Output:
Future time is ahead of current time.
Current Time: 14:35:40.512
Time after 2 hours: 16:35:40.512
3. LocalDateTime: Combining Date and Time
LocalDateTime is used when you want both date and time together, like a timestamp of an event.
Example:
import java.time.LocalDateTime;
public class DateTimeDemo {
public static void main(String[] args) {
// Get current date and time
LocalDateTime now = LocalDateTime.now();
// Add 1 day and 3 hours to current date-time
LocalDateTime nextMeeting = now.plusDays(1).plusHours(3);
System.out.println("Current Date and Time: " + now);
System.out.println("Next meeting scheduled at: " + nextMeeting);
}
}
Output:
Current Date and Time: 2025-10-14T14:40:25.324
Next meeting scheduled at: 2025-10-15T17:40:25.324
Formatting and Parsing Dates
When working with dates and times, you often need to display them in a specific way or read dates from text. The DateTimeFormatter class helps us to format and parse dates easily.
Example: Formatting Date and Time
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CustomDateFormat {
public static void main(String[] args) {
// Current date and time
LocalDateTime now = LocalDateTime.now();
// Define a custom pattern: day-month-year hour:minute
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
// Format the date-time using the pattern
String formatted = now.format(formatter);
System.out.println("Custom Formatted Date and Time: " + formatted);
}
}
Output:
Custom Formatted Date and Time: 15/10/2025 15:50
Working with Time Zones
Sometimes, applications need to know the current time in different parts of the world, not just the local system time.
Java provides the ZonedDateTime class in the java.time package, which stores both date-time and time zone information.
- ZonedDateTime → Represents a date and time with a time zone.
- ZoneId → Represents a specific time zone, like “Asia/Kolkata” or “America/New_York”.
Example: Displaying Time in Different Cities
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class WorldTimeExample {
public static void main(String[] args) {
// Current date and time in Tokyo
ZonedDateTime tokyoTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println("Time in Tokyo: " + tokyoTime);
// Current date and time in London
ZonedDateTime londonTime = ZonedDateTime.now(ZoneId.of("Europe/London"));
System.out.println("Time in London: " + londonTime);
// Current date and time in New York
ZonedDateTime newYorkTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println("Time in New York: " + newYorkTime);
}
}
Output:
Time in Tokyo: 2025-10-15T23:45:12+09:00[Asia/Tokyo]
Time in London: 2025-10-15T15:45:12+01:00[Europe/London]
Time in New York: 2025-10-15T10:45:12-04:00[America/New_York]
Also Learn Important Topics of Java
- What are the operators in Java Programming?
- What are Data Types in Java?
- What are the Variables in Java?
- Learn Java syntax.
- What is Java if else statements?
- What is method overloading in Java?
- What are the Classes and Objects in Java?
- What is Polymorphism in Java?
- What is Interfaces in Java?

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.