Java provides several classes for handling date and time. The most commonly used classes are part of the java.time
package introduced in Java 8. These classes make working with date and time much easier and more reliable.
Examples
Get Current Date
1 2 |
LocalDate currentDate = LocalDate.now(); System.out.println("Current Date: " + currentDate); |
Parse a Date from a String
1 2 3 |
String dateStr = "2025-01-28"; LocalDate parsedDate = LocalDate.parse(dateStr); System.out.println("Parsed Date: " + parsedDate); |
Add Days to Current Date
1 2 |
LocalDate futureDate = LocalDate.now().plusDays(5); System.out.println("Future Date (5 days later): " + futureDate); |
Calculate Duration Between Two Dates
1 2 3 4 |
LocalDate startDate = LocalDate.of(2025, 1, 1); LocalDate endDate = LocalDate.now(); Duration duration = Duration.between(startDate.atStartOfDay(), endDate.atStartOfDay()); System.out.println("Duration in days: " + duration.toDays()); |
Other date and time functions
Function | Description | Example |
---|---|---|
LocalDate.now() | Gets the current date. | LocalDate currentDate = LocalDate.now(); |
LocalTime.now() | Gets the current time. | LocalTime currentTime = LocalTime.now(); |
LocalDateTime.now() | Gets the current date and time. | LocalDateTime currentDateTime = LocalDateTime.now(); |
LocalDate.of(int year, int month, int dayOfMonth) | Creates a LocalDate instance from specified year, month, and day. | LocalDate date = LocalDate.of(2025, 1, 28); |
LocalDate.parse(CharSequence text) | Parses a date from a string. | LocalDate parsedDate = LocalDate.parse("2025-01-28"); |
plusDays(long days) | Adds days to a date. | LocalDate futureDate = LocalDate.now().plusDays(10); |
minusDays(long days) | Subtracts days from a date. | LocalDate pastDate = LocalDate.now().minusDays(5); |
getDayOfWeek() | Returns the day of the week for a given date. | DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek(); |
getMonth() | Returns the month of a date. | Month month = LocalDate.now().getMonth(); |
isBefore(ChronoLocalDate other) | Checks if the current date is before another date. | boolean isBefore = LocalDate.now().isBefore(LocalDate.of(2025, 1, 1)); |
isAfter(ChronoLocalDate other) | Checks if the current date is after another date. | boolean isAfter = LocalDate.now().isAfter(LocalDate.of(2024, 12, 31)); |
atStartOfDay() | Returns the LocalDateTime at the start of the day (00:00:00). | LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); |
Duration.between(Temporal startInclusive, Temporal endExclusive) | Calculates the duration between two points in time (e.g., in seconds, minutes). | Duration duration = Duration.between(startDate.atStartOfDay(), endDate.atStartOfDay()); |
Period.between(LocalDate startDate, LocalDate endDate) | Calculates the period between two dates (in years, months, and days). | Period period = Period.between(startDate, endDate); |
getYear() | Retrieves the year from a LocalDate or LocalDateTime. | int year = LocalDate.now().getYear(); |
getMonthValue() | Retrieves the numeric month value (1 for January, 2 for February, etc.). | int month = LocalDate.now().getMonthValue(); |
getDayOfMonth() | Retrieves the day of the month from a LocalDate. | int day = LocalDate.now().getDayOfMonth(); |