Java में Date और Time
In this page:
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
LocalDateTime
LocalDateTime दोनों को एक single date-and-time value में combine करता है, एक specific moment represent करने के लिए उपयोगी बिना timezone information attached की ज़रूरत के।
उदाहरण: LocalDateTime
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Formatting Dates
DateTimeFormatter control करता है कि एक date या time कैसे एक string में render हो (और इससे वापस parse हो), आपको locale-specific या application-specific formatting requirements match करने देते हुए।
उदाहरण: Formatting Dates
// 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));
}
}
Login to try C/C++/Java/PHP code in the editor
Modifying Dates
चूंकि ये classes immutable हैं, plusDays() या minusMonths() जैसे methods original को modify करने के बजाय एक *नया* date/time object return करते हैं — change देखने के लिए आपको return value capture करना चाहिए।
उदाहरण: Modifying Dates
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- पुरानी
Dateclass उपयोग करना जबLocalDaterecommended है। LocalDate.of(2023, 2, 30)जैसी एक invalid date बनाना, जोDateTimeExceptionthrow करता है।- यह भूल जाना कि date objects immutable हैं, इसलिए
plusDays()एक नया object return करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: