← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 2 of 10

Java में Date और Time

Java के date और time tools एक program को dates, clock times, और उनके बीच gaps के साथ काम करने देते हैं, एक calendar और clock combined जैसा। आप days जोड़ सकते हैं, उन्हें format कर सकते हैं, या दो dates के बीच कितना समय है यह ढूंढ सकते हैं।
Syntax
java
import java.time.*;

LocalDate date = LocalDate.now();
LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute);

LocalDate and LocalTime

LocalDate बिना किसी time component के एक calendar date represent करता है, और LocalTime बिना किसी date के एक time of day represent करता है — जो भी आप असल में model कर रहे हों उससे match करे वह उपयोग करें।

उदाहरण: LocalDate and LocalTime

java
// Import java.time.LocalDate so it can be used by its short name
import java.time.LocalDate;
// Import java.time.LocalTime so it can be used by its short name
import java.time.LocalTime;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		LocalDate date = LocalDate.of(2024, 1, 15);
		LocalTime time = LocalTime.of(14, 30);
		// Print a line to the console
		System.out.println(date);
		// Print a line to the console
		System.out.println(time);
	}
}

LocalDateTime

LocalDateTime दोनों को एक single date-and-time value में combine करता है, एक specific moment represent करने के लिए उपयोगी बिना timezone information attached की ज़रूरत के।

उदाहरण: LocalDateTime

java
// Import java.time.LocalDateTime so it can be used by its short name
import java.time.LocalDateTime;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		LocalDateTime dt = LocalDateTime.of(2024, 1, 15, 14, 30);
		// Print a line to the console
		System.out.println(dt);
	}
}

Formatting Dates

DateTimeFormatter control करता है कि एक date या time कैसे एक string में render हो (और इससे वापस parse हो), आपको locale-specific या application-specific formatting requirements match करने देते हुए।

उदाहरण: Formatting Dates

java
// Import java.time.LocalDate so it can be used by its short name
import java.time.LocalDate;
// Import java.time.format.DateTimeFormatter so it can be used by its short name
import java.time.format.DateTimeFormatter;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		LocalDate date = LocalDate.of(2024, 1, 15);
		DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy");
		// Print a line to the console
		System.out.println(date.format(fmt));
	}
}

Modifying Dates

चूंकि ये classes immutable हैं, plusDays() या minusMonths() जैसे methods original को modify करने के बजाय एक *नया* date/time object return करते हैं — change देखने के लिए आपको return value capture करना चाहिए।

उदाहरण: Modifying Dates

java
import java.time.LocalDate;
public class Main {
	public static void main(String[] args) {
		LocalDate date = LocalDate.of(2024, 1, 15);
		LocalDate later = date.plusDays(10); // new object, original unchanged
		System.out.println(date);
		System.out.println(later);
	}
}

Duration and Period

Duration seconds/nanoseconds में समय का एक span measure करता है (time-based values के लिए), जबकि Period years/months/days में एक span measure करता है (date-based values के लिए) — अपनी calculation के लिए असल में कौन सी unit sense बनाती है उसके आधार पर चुनें।

उदाहरण: Duration and Period

java
// Import java.time.Duration so it can be used by its short name
import java.time.Duration;
// Import java.time.Period so it can be used by its short name
import java.time.Period;
import java.time.LocalDate;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Duration duration = Duration.ofMinutes(90);
		Period period = Period.between(LocalDate.of(2024, 1, 1), LocalDate.of(2024, 3, 1));
		// Print a line to the console
		System.out.println(duration);
		// Print a line to the console
		System.out.println(period);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. पुरानी Date class उपयोग करना जब LocalDate recommended है।
  2. LocalDate.of(2023, 2, 30) जैसी एक invalid date बनाना, जो DateTimeException throw करता है।
  3. यह भूल जाना कि date objects immutable हैं, इसलिए plusDays() एक नया object return करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.