पाठ 37 / 39

java.time से Dates और Times

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 उपयोग करें।

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 रखता है।

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 से बचें।

त्वरित जाँच

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

  • Instant
  • LocalDate
  • Period
  • SimpleDateFormat
Answer

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

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

अंतिम क्विज़

त्वरित जाँच: javac क्या बनाता है?

  • Native machine code
  • An .exe file
  • Platform-neutral bytecode
  • Nothing
Answer

Platform-neutral bytecode — JVM bytecode चलाता है।

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

अंतिम क्विज़

त्वरित जाँच: String की सामग्री की तुलना कैसे करें?

  • ==
  • compare with +
  • hashCode()
  • equals()
Answer

equals() — == references की तुलना करता है।

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

अंतिम क्विज़

त्वरित जाँच: कौन-सा class loops में O(n²) string concatenation से बचाता है?

  • StringBuilder
  • String
  • Scanner
  • Objects
Answer

StringBuilder — StringBuilder एक buffer बदलता है।

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

अंतिम क्विज़

त्वरित जाँच: equals() override करते समय क्या करना जरूरी है?

  • Make the class final only
  • Override hashCode() consistently
  • Return null
  • Nothing
Answer

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

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

अंतिम क्विज़

त्वरित जाँच: कौन-सा collection unique values और तेज़ membership checks रखता है?

  • ArrayList
  • LinkedList
  • HashSet
  • int[]
Answer

HashSet — HashSet औसत O(1) contains देता है।

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

अंतिम क्विज़

त्वरित जाँच: Unchecked custom exception किस base class को extend करे?

  • Exception
  • Throwable
  • Error
  • RuntimeException
Answer

RuntimeException — Callers को उसे पकड़ना जरूरी नहीं।

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

अंतिम क्विज़

त्वरित जाँच: try-with-resources क्या करता है?

  • Closes AutoCloseable resources automatically
  • Skips exceptions
  • Starts threads
  • Compiles faster
Answer

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

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

अंतिम क्विज़

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

  • Instant
  • LocalDate
  • Duration
  • ZonedDateTime
Answer

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