# Java 8 Date/Time API Old `java.util.Date` aur `Calendar` replace karta hai. Immutable, thread-safe. ## Key Classes ### LocalDate — Date only (no time, no timezone) ```java LocalDate today = LocalDate.now(); LocalDate specific = LocalDate.of(2024, 1, 15); LocalDate parsed = LocalDate.parse("2024-01-15"); today.getYear() // 2024 today.getMonth() // JANUARY today.getMonthValue() // 1 today.getDayOfMonth() // 15 today.getDayOfWeek() // MONDAY // Arithmetic today.plusDays(10) today.minusMonths(2) today.plusYears(1) // Compare today.isBefore(specific) today.isAfter(specific) today.isEqual(specific) // Period between dates Period p = Period.between(LocalDate.of(2000,1,1), today); p.getYears(); // Age in years ``` ### LocalTime — Time only ```java LocalTime now = LocalTime.now(); LocalTime specific = LocalTime.of(10, 30, 0); now.getHour() // 10 now.getMinute() // 30 now.getSecond() // 0 now.plusHours(2) now.minusMinutes(15) ``` ### LocalDateTime — Date + Time ```java LocalDateTime now = LocalDateTime.now(); LocalDateTime dt = LocalDateTime.of(2024, 3, 15, 10, 30); LocalDateTime parsed = LocalDateTime.parse("2024-03-15T10:30:00"); now.toLocalDate() now.toLocalTime() ``` ### ZonedDateTime — With timezone ```java ZonedDateTime zdt = ZonedDateTime.now(); ZonedDateTime ist = ZonedDateTime.now(ZoneId.of("Asia/Kolkata")); ZonedDateTime utc = ZonedDateTime.now(ZoneId.of("UTC")); ``` ### DateTimeFormatter ```java LocalDateTime now = LocalDateTime.now(); // Format karna DateTimeFormatter f1 = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); String formatted = now.format(f1); // "15/01/2024 10:30:00" // Parse karna LocalDate date = LocalDate.parse("15-01-2024", DateTimeFormatter.ofPattern("dd-MM-yyyy")); ``` ### Duration — Between two times ```java LocalTime start = LocalTime.of(9, 0); LocalTime end = LocalTime.of(17, 30); Duration d = Duration.between(start, end); d.toHours(); // 8 d.toMinutes(); // 510 ```