Java Date and Time
Model dates with the right abstraction.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Date and Time.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
java.time provides types with distinct purposes. LocalDate is a date without a time or time zone. LocalDateTime also has no time zone. Choose Instant or a zoned type when you need a point on the timeline or a location-specific time. Fixed dates make examples reproducible.
Explore the details
Java Dates
Java does not have a built-in Date class, but we can import the java.time
package to work with the date and time API. The package includes many date and time classes.
For example:
If you don't know what a package is, read our Java Packages Tutorial.
Display Current Date
To display the current date, import the java.time.LocalDate class, and use its now() method:
Display Current Time
To display the current time (hour, minute, second, and nanoseconds), import the java.time.LocalTime class, and use its now() method:
Display Current Date and Time
To display the current date and time, import the java.time.LocalDateTime class, and use its now() method:
Formatting Date and Time
The "T" in the example above is used to separate the date from the time. You can use the DateTimeFormatter class
with the ofPattern() method in the same package to format or parse date-time objects.
The following example will remove both the "T" and nanoseconds from the date-time:
The ofPattern() method accepts all sorts of values, if you want to display
the date and time in a different format. For example:
Common Date Format Patterns
| Pattern | Meaning | Example output |
|---|---|---|
yyyy | 4-digit year | 2025 |
MM | 2-digit month (01–12) | 04 |
dd | 2-digit day (01–31) | 14 |
HH | Hour in 24-hour format (00–23) | 15 |
mm | Minutes (00–59) | 30 |
ss | Seconds (00–59) | 05 |
EEEE | Full day name | Monday |
MMMM | Full month name | April |
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
LocalDateTime now = LocalDateTime.now();
// Different format patterns
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("EEEE, MMMM dd yyyy");
DateTimeFormatter f3 = DateTimeFormatter.ofPattern("hh:mm a"); // 12-hour with AM/PM
DateTimeFormatter f4 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(now.format(f1)); // 14/04/2025
System.out.println(now.format(f2)); // Monday, April 14 2025
System.out.println(now.format(f3)); // 03:30 PM
System.out.println(now.format(f4)); // 14-04-2025 15:30:05Prefer java.time over java.util.Date: The java.time package (introduced in Java 8) is immutable, thread-safe, and much easier to use than the older Date and Calendar classes.
A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) throws Exception {
LocalDate start = LocalDate.of(2026, 1, 1);
System.out.println(start.plusDays(7));
System.out.println(start);
}
}Predict first. Then reveal the expected output / வெளியீடு
2026-01-08 2026-01-01
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Store start.plusDays(7) back into start and print it.
Reveal the answer and reasoning / விடை
The variable then refers to the later date. The original LocalDate object was not modified.
One mistake to avoid: java.time objects such as LocalDate are immutable. Calling plusDays without using the return value does not change the variable.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.