Java LocalDate class is an immutable class that represents Date with a default format of yyyy-mm-dd. It inherits Object class and implements the ChronoLocalDate interface.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
// Creating a LocalDate object representing today's date
LocalDate today = LocalDate.now();
System.out.println("Today's date: " + today);
// Creating a LocalDate object for a specific date
LocalDate specificDate = LocalDate.of(2024, 4, 23);
System.out.println("Specific date: " + specificDate);
// Getting components of a LocalDate
int year = specificDate.getYear();
int month = specificDate.getMonthValue();
int day = specificDate.getDayOfMonth();
System.out.println("Year: " + year + ", Month: " + month + ", Day: " + day);
// Manipulating LocalDate objects
LocalDate futureDate = specificDate.plusDays(7); // Adding 7 days
System.out.println("Date 7 days from now: " + futureDate);
LocalDate pastDate = specificDate.minusMonths(3); // Subtracting 3 months
System.out.println("Date 3 months ago: " + pastDate);
// Comparing LocalDate objects
LocalDate anotherDate = LocalDate.of(2024, 5, 15);
if (specificDate.isBefore(anotherDate)) {
System.out.println(specificDate + " is before " + anotherDate);
} else {
System.out.println(specificDate + " is after " + anotherDate);
}
}
}
Java LocalTime class is an immutable class that represents time with a default format of hour-minute-second. It inherits Object class and implements the Comparable interface.
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// Creating a LocalTime object representing the current time
LocalTime currentTime = LocalTime.now();
System.out.println("Current time: " + currentTime);
// Creating a LocalTime object for a specific time
LocalTime specificTime = LocalTime.of(14, 30, 45); // hour, minute, second
System.out.println("Specific time: " + specificTime);
// Getting components of a LocalTime
int hour = specificTime.getHour();
int minute = specificTime.getMinute();
int second = specificTime.getSecond();
int nano = specificTime.getNano(); // fraction of second
System.out.println("Hour: " + hour + ", Minute: " + minute + ", Second: " + second + ", Nano: " + nano);
// Manipulating LocalTime objects
LocalTime futureTime = specificTime.plusHours(2); // Adding 2 hours
System.out.println("Time 2 hours from now: " + futureTime);
LocalTime pastTime = specificTime.minusMinutes(15); // Subtracting 15 minutes
System.out.println("Time 15 minutes ago: " + pastTime);
// Comparing LocalTime objects
LocalTime anotherTime = LocalTime.of(10, 0, 0);
if (specificTime.isBefore(anotherTime)) {
System.out.println(specificTime + " is before " + anotherTime);
} else {
System.out.println(specificTime + " is after " + anotherTime);
}
}
}
Java LocalDateTime class is an immutable date-time object that represents a date-time, with the default format as yyyy-MM-dd HH-mm-ss.zzz. It inherits object class and implements the ChronoLocalDateTime interface.
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
// Creating a LocalDateTime object representing the current date and time
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current date and time: " + currentDateTime);
// Creating a LocalDateTime object for a specific date and time
LocalDateTime specificDateTime = LocalDateTime.of(2024, 4, 23, 14, 30, 45); // year, month, day, hour, minute, second
System.out.println("Specific date and time: " + specificDateTime);
// Getting components of a LocalDateTime
int year = specificDateTime.getYear();
int month = specificDateTime.getMonthValue();
int day = specificDateTime.getDayOfMonth();
int hour = specificDateTime.getHour();
int minute = specificDateTime.getMinute();
int second = specificDateTime.getSecond();
System.out.println("Year: " + year + ", Month: " + month + ", Day: " + day +
", Hour: " + hour + ", Minute: " + minute + ", Second: " + second);
// Manipulating LocalDateTime objects
LocalDateTime futureDateTime = specificDateTime.plusDays(7); // Adding 7 days
System.out.println("Date and time 7 days from now: " + futureDateTime);
LocalDateTime pastDateTime = specificDateTime.minusMonths(3); // Subtracting 3 months
System.out.println("Date and time 3 months ago: " + pastDateTime);
// Comparing LocalDateTime objects
LocalDateTime anotherDateTime = LocalDateTime.of(2024, 5, 15, 10, 0, 0);
if (specificDateTime.isBefore(anotherDateTime)) {
System.out.println(specificDateTime + " is before " + anotherDateTime);
} else {
System.out.println(specificDateTime + " is after " + anotherDateTime);
}
}
}
Java Period class is used to measures time in years, months and days. It inherits the Object class and implements the ChronoPeriod interface.
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
// Creating LocalDate objects for two dates
LocalDate startDate = LocalDate.of(2020, 1, 1);
LocalDate endDate = LocalDate.of(2024, 4, 23);
// Calculating the period between the two dates
Period period = Period.between(startDate, endDate);
// Accessing the components of the period
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
System.out.println("Period between " + startDate + " and " + endDate + ":");
System.out.println("Years: " + years);
System.out.println("Months: " + months);
System.out.println("Days: " + days);
// Creating a Period using of() method
Period additionalPeriod = Period.of(1, 2, 3); // 1 year, 2 months, and 3 days
// Printing the additionalPeriod
System.out.println("Additional Period: " + additionalPeriod); // Additional Period: P1Y2M3D
}
}
Java Duration class is used to measures time in seconds and nanoseconds. It inherits the Object class and implements the Comparable interface.
import java.time.Duration;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// Creating LocalTime objects for two times
LocalTime startTime = LocalTime.of(10, 30, 0);
LocalTime endTime = LocalTime.of(12, 45, 30);
// Calculating the duration between the two times
Duration duration = Duration.between(startTime, endTime);
// Accessing the components of the duration
long seconds = duration.getSeconds();
long nano = duration.getNano();
System.out.println("Duration between " + startTime + " and " + endTime + ":");
System.out.println("Seconds: " + seconds);
System.out.println("Nanoseconds: " + nano);
// Manipulating a LocalTime using a Duration
LocalTime newTime = startTime.plus(duration);
System.out.println("New time after adding the duration: " + newTime);
}
}