Core Date-Time Classes in Java 8
Java's traditional date and time handling prior to Java 8 presented challenges due to unintuitive design and thread safety concerns in java.util.Date and java.util.Calendar. The Java 8 Date-Time API introduced immutable, thread-safe alternatives including LocalDate, LocalTime, and LocalDateTime.
Date Handling with LocalDate
LocalDate represents date information without time components in ISO-8601 format (yyyy-MM-dd).
Instantiating Date Objects
import java.time.LocalDate;
public class DateCreation {
public static void main(String[] args) {
LocalDate current = LocalDate.now();
System.out.println("Current date: " + current);
LocalDate custom = LocalDate.of(2023, 12, 25);
System.out.println("Custom date: " + custom);
LocalDate fromText = LocalDate.parse("2024-01-01");
System.out.println("Parsed date: " + fromText);
}
}
Date Manipulation Techniques
public class DateOperations {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
int currentYear = today.getYear();
int currentMonth = today.getMonthValue();
int currentDay = today.getDayOfMonth();
System.out.printf("Year: %d, Month: %d, Day: %d%n", currentYear, currentMonth, currentDay);
LocalDate futureWeek = today.plusWeeks(1);
System.out.println("Next week: " + futureWeek);
LocalDate pastMonth = today.minusMonths(1);
System.out.println("Previous month: " + pastMonth);
boolean leapCheck = today.isLeapYear();
System.out.println("Leap year? " + leapCheck);
}
}
Time Management with LocalTime
LocalTime handles time-of-day values without date information, using HH:mm:ss format.
Creating Time Instances
import java.time.LocalTime;
public class TimeCreation {
public static void main(String[] args) {
LocalTime current = LocalTime.now();
System.out.println("Current time: " + current);
LocalTime specified = LocalTime.of(14, 45, 30);
System.out.println("Specified time: " + specified);
LocalTime fromString = LocalTime.parse("09:15:00");
System.out.println("Parsed time: " + fromString);
}
}
Time Operation Methods
public class TimeOperations {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
int hours = now.getHour();
int minutes = now.getMinute();
int seconds = now.getSecond();
System.out.printf("Hours: %d, Minutes: %d, Seconds: %d%n", hours, minutes, seconds);
LocalTime later = now.plusHours(2);
System.out.println("Two hours later: " + later);
LocalTime earlier = now.minusMinutes(15);
System.out.println("Fifteen minutes prior: " + earlier);
LocalTime reference = LocalTime.of(17, 0);
boolean beforeCheck = now.isBefore(reference);
boolean afterCheck = now.isAfter(reference);
System.out.printf("Before 5 PM? %s, After 5 PM? %s%n", beforeCheck, afterCheck);
}
}
Combined Date-Time with LocalDateTime
LocalDateTime integrates date and time components using the format yyyy-MM-ddTHH:mm:ss.
DateTime Object Creation
import java.time.LocalDateTime;
public class DateTimeCreation {
public static void main(String[] args) {
LocalDateTime current = LocalDateTime.now();
System.out.println("Current date-time: " + current);
LocalDateTime custom = LocalDateTime.of(2023, 8, 20, 16, 30);
System.out.println("Custom date-time: " + custom);
LocalDateTime parsed = LocalDateTime.parse("2023-10-31T19:00:00");
System.out.println("Parsed date-time: " + parsed);
}
}
DateTime Maniuplation
public class DateTimeOperations {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n",
year, month, day, hour, minute, second);
LocalDateTime tomorrow = now.plusDays(1);
System.out.println("Next day: " + tomorrow);
LocalDateTime previousHour = now.minusHours(1);
System.out.println("Previous hour: " + previousHour);
LocalDate dateComponent = now.toLocalDate();
LocalTime timeComponent = now.toLocalTime();
System.out.println("Date component: " + dateComponent);
System.out.println("Time component: " + timeComponent);
}
}
Integrating Date and Time Components
public class ComponentIntegration {
public static void main(String[] args) {
LocalDate datePart = LocalDate.of(2024, 5, 15);
LocalTime timePart = LocalTime.of(13, 45);
LocalDateTime combined = LocalDateTime.of(datePart, timePart);
System.out.println("Combined date-time: " + combined);
LocalDate extractedDate = combined.toLocalDate();
LocalTime extractedTime = combined.toLocalTime();
System.out.println("Extracted date: " + extractedDate);
System.out.println("Extracted time: " + extractedTime);
}
}