# java.time से Dates और Times — Core Java Fundamentals: Syntax, OOP और Collections

Source: https://www.geekswithgeeks.com/hi/core-java/java-time-api

> Date और Calendar की जगह immutable java.time API से dates, times, durations और time zones के साथ काम करें।

## LocalDate, LocalDateTime और formatting

`java.time` classes immutable और thread-safe हैं। Dates के लिए `LocalDate`, date व time के लिए `LocalDateTime` और print या parse के लिए `DateTimeFormatter` उपयोग करें।

```java
LocalDate today = LocalDate.now();
LocalDate due = today.plusDays(14);
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy");
System.out.println(due.format(f));
System.out.println(LocalDate.parse("2026-10-01").getDayOfWeek());
```

## Durations, periods और zones

`Duration` समय-आधारित मात्राएँ, `Period` तारीख-आधारित मात्राएँ मापता है और `ZonedDateTime` time zone रखता है।

```java
Duration d = Duration.between(start, end);
System.out.println(d.toMinutes());
Period p = Period.between(LocalDate.of(2026, 1, 1), LocalDate.now());
ZonedDateTime tokyo = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
```

Timestamps को `Instant` (UTC) के रूप में रखें और केवल प्रदर्शन के लिए zone में बदलें। पुराने `Date`, `Calendar` और `SimpleDateFormat` से बचें।

त्वरित जाँच

**Quiz:** कौन-सा class UTC timeline पर एक बिंदु दर्शाता है?

- [x] Instant
- [ ] LocalDate
- [ ] Period
- [ ] SimpleDateFormat

*Answer:* Instant. Instant machine timestamp है; प्रदर्शन के लिए उसे zone में बदलें।

## अंतिम क्विज़ 1/8

अंतिम क्विज़

**Quiz:** javac क्या बनाता है?

- [ ] Native machine code
- [ ] An .exe file
- [x] Platform-neutral bytecode
- [ ] Nothing

*Answer:* Platform-neutral bytecode. JVM bytecode चलाता है।

## अंतिम क्विज़ 2/8

अंतिम क्विज़

**Quiz:** String की सामग्री की तुलना कैसे करें?

- [ ] ==
- [ ] compare with +
- [ ] hashCode()
- [x] equals()

*Answer:* equals(). == references की तुलना करता है।

## अंतिम क्विज़ 3/8

अंतिम क्विज़

**Quiz:** कौन-सा class loops में O(n²) string concatenation से बचाता है?

- [x] StringBuilder
- [ ] String
- [ ] Scanner
- [ ] Objects

*Answer:* StringBuilder. StringBuilder एक buffer बदलता है।

## अंतिम क्विज़ 4/8

अंतिम क्विज़

**Quiz:** equals() override करते समय क्या करना जरूरी है?

- [ ] Make the class final only
- [x] Override hashCode() consistently
- [ ] Return null
- [ ] Nothing

*Answer:* Override hashCode() consistently. बराबर objects का hash code समान होना चाहिए।

## अंतिम क्विज़ 5/8

अंतिम क्विज़

**Quiz:** कौन-सा collection unique values और तेज़ membership checks रखता है?

- [ ] ArrayList
- [ ] LinkedList
- [x] HashSet
- [ ] int[]

*Answer:* HashSet. HashSet औसत O(1) contains देता है।

## अंतिम क्विज़ 6/8

अंतिम क्विज़

**Quiz:** Unchecked custom exception किस base class को extend करे?

- [ ] Exception
- [ ] Throwable
- [ ] Error
- [x] RuntimeException

*Answer:* RuntimeException. Callers को उसे पकड़ना जरूरी नहीं।

## अंतिम क्विज़ 7/8

अंतिम क्विज़

**Quiz:** try-with-resources क्या करता है?

- [x] Closes AutoCloseable resources automatically
- [ ] Skips exceptions
- [ ] Starts threads
- [ ] Compiles faster

*Answer:* Closes AutoCloseable resources automatically. Exception आने पर भी resources बंद हो जाते हैं।

## अंतिम क्विज़ 8/8

अंतिम क्विज़

**Quiz:** बिना समय वाली तारीख के लिए कौन-सा java.time class उपयुक्त है?

- [ ] Instant
- [x] LocalDate
- [ ] Duration
- [ ] ZonedDateTime

*Answer:* LocalDate. LocalDate केवल वर्ष, माह और दिन रखता है।
