Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 05:45:24 Viewed : 234
The java.time
package, introduced in Java 8, provides a comprehensive set of classes for handling date and time in a more modern and sophisticated way than the previous java.util.Date
and java.util.Calendar
classes. The new API in java.time
is part of the Java Date and Time API (JSR-310) and is inspired by the popular Joda-Time library. The primary classes in the java.time
package are in the java.time
subpackage.
Here is an overview of some key classes and concepts in the java.time
package:
javaLocalDate today = LocalDate.now();
javaLocalTime now = LocalTime.now();
LocalDate
and LocalTime
to represent both date and time.javaLocalDateTime dateTime = LocalDateTime.now();
LocalDateTime
to include time zone information.javaZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
javaDuration duration = Duration.ofHours(2);
javaPeriod period = Period.ofMonths(3);
javaInstant instant = Instant.now();
ZonedDateTime
and Instant
.javaZoneId zoneId = ZoneId.of("America/Los_Angeles");
ZonedDateTime losAngelesTime = ZonedDateTime.now(zoneId);9. DateTimeFormatter:
javaLocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = dateTime.format(formatter);
javaLocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
Duration
to perform time-based arithmetic.javaLocalDateTime now = LocalDateTime.now();
LocalDateTime future = now.plus(3, ChronoUnit.DAYS);
javaClock clock = Clock.systemDefaultZone();
LocalDateTime now = LocalDateTime.now(clock);
javaimport java.time.*;
public class DateTimeExample {
public static void main(String[] args) {
// LocalDate
LocalDate today = LocalDate.now();
System.out.println("Todays date: " + today);
// LocalTime
LocalTime now = LocalTime.now();
System.out.println("Current time: " + now);
// LocalDateTime
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("Date and time: " + dateTime);
// ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println("New York time: " + zonedDateTime);
// Duration
Duration duration = Duration.ofHours(2);
System.out.println("Duration: " + duration);
// Period
Period period = Period.ofMonths(3);
System.out.println("Period: " + period);
// Instant
Instant instant = Instant.now();
System.out.println("Instant: " + instant);
// ZoneId
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime parisTime = ZonedDateTime.now(zoneId);
System.out.println("Paris time: " + parisTime);
// DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = dateTime.format(formatter);
System.out.println("Formatted date and time: " + formattedDateTime);
// TemporalAdjusters
LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
System.out.println("First day of the month: " + firstDayOfMonth);
// ChronoUnit
LocalDateTime future = dateTime.plus(