Java Date
Model dates with the right abstraction.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- 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.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
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.
விரிவான விளக்கங்கள்
Java Dates
Java-ல dates-ஐ work பண்றதுக்கு java.time package use பண்றோம் (Java 8-ல introduce ஆச்சு).
Key classes:
LocalDate— date மட்டும் (time இல்ல): 2024-03-15LocalTime— time மட்டும் (date இல்ல): 14:30:00LocalDateTime— date மற்றும் time: 2024-03-15T14:30:00DateTimeFormatter— date/time-ஐ format பண்றதுக்கு
Current Date மற்றும் Time
Current date/time-ஐ now() method-ல get பண்ணலாம்:
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 | 15 |
mm | Minutes (00–59) | 30 |
EEEE | Full day name | Monday |
MMMM | Full month name | April |
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("EEEE, MMMM dd yyyy");
DateTimeFormatter f3 = 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)); // 14-04-2025 15:30:05Modern Java-வில் date/time-க்கு எந்த class prefer செய்வோம்?
- java.util.Date
- java.util.Calendar
- java.time.LocalDate (Java 8+)
- java.sql.Date
Reveal answer / விடை
java.time.LocalDate (Java 8+)
`java.time` package (Java 8+) immutable, thread-safe, மற்றும் intuitive. `LocalDate`, `LocalDateTime`, `ZonedDateTime` — modern applications-ல் இதையே பயன்படுத்தவும். Old `Date` class deprecated.
முழுமையான உதாரணம்
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.
நீங்களே முயற்சி செய்யுங்கள்
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.
தவிர்க்க வேண்டிய தவறு: java.time objects such as LocalDate are immutable. Calling plusDays without using the return value does not change the variable.
பாடத்தின் சுருக்கம்
- 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.