Introduce Java Date and Time and give examples
Java provides a comprehensive set of classes for handling date and time through the java.time
package, which was introduced in Java 8. This package is inspired by the Joda-Time library and aims to provide a more intuitive and flexible way to work with date and time compared to the older java.util.Date
and java.util.Calendar
classes.
java.time
PackageLocalDate
and LocalTime
without time-zone.import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("Today's date: " + today);
LocalDate specificDate = LocalDate.of(2023, 10, 15);
System.out.println("Specific date: " + specificDate);
LocalDate tomorrow = today.plusDays(1);
System.out.println("Tomorrow's date: " + tomorrow);
}
}
import java.time.LocalTime;
public class LocalTimeExample {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println("Current time: " + now);
LocalTime specificTime = LocalTime.of(14, 30); // 2:30 PM
System.out.println("Specific time: " + specificTime);
LocalTime later = now.plusHours(2);
System.out.println("Time after 2 hours: " + later);
}
}
import java.time.LocalDateTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("Current date and time: " + now);
LocalDateTime specificDateTime = LocalDateTime.of(2023, 10, 15, 14, 30);
System.out.println("Specific date and time: " + specificDateTime);
LocalDateTime futureDateTime = now.plusDays(5).plusHours(3);
System.out.println("Date and time after 5 days and 3 hours: " + futureDateTime);
}
}
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZonedDateTime zonedNow = ZonedDateTime.now();
System.out.println("Current date and time with zone: " + zonedNow);
ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 10, 15, 14, 30, 0, 0, ZoneId.of("America/New_York"));
System.out.println("Specific date and time in New York: " + zonedDateTime);
}
}
import java.time.Instant;
public class InstantExample {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println("Current timestamp: " + now);
Instant specificInstant = Instant.parse("2023-10-15T14:30:00Z");
System.out.println("Specific timestamp: " + specificInstant);
}
}
import java.time.Duration;
public class DurationExample {
public static void main(String[] args) {
Duration duration = Duration.ofHours(2);
System.out.println("Duration of 2 hours: " + duration);
Duration between = Duration.between(Instant.now(), Instant.now().plusSeconds(3600));
System.out.println("Duration between two instants: " + between);
}
}
import java.time.LocalDate;
import java.time.Period;
public class PeriodExample {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2023, 1, 1);
LocalDate endDate = LocalDate.of(2023, 10, 15);
Period period = Period.between(startDate, endDate);
System.out.println("Period between two dates: " + period);
}
}
The java.time
package provides a rich set of classes for handling date and time in Java. It is designed to be more user-friendly and less error-prone than the older date and time classes. By using these classes, developers can easily manipulate dates and times, perform calculations, and format them for display.