Java Date and Time Methods
The java.time package has classes to work with dates and times. Import the class you need at the top of your program, for example import java.time.LocalDate;.
LocalDate Methods
A LocalDate object holds a date (year, month and day), like 2026-01-15.
| Method | Description | Return Type |
|---|---|---|
| now() | Returns the current date, read from the system clock | LocalDate |
| of() | Creates a date from a year, a month and a day | LocalDate |
| parse() | Creates a date from a text in the format yyyy-MM-dd | LocalDate |
| getYear() | Returns the year of the date | int |
| getMonthValue() | Returns the month of the date, as a number from 1 to 12 | int |
| getMonth() | Returns the month of the date, as a Month value like JANUARY | Month |
| getDayOfMonth() | Returns the day of the month, from 1 to 31 | int |
| getDayOfWeek() | Returns the day of the week, as a DayOfWeek value like MONDAY | DayOfWeek |
| plusDays() | Returns a copy of the date with a number of days added | LocalDate |
| minusDays() | Returns a copy of the date with a number of days subtracted | LocalDate |
| plusMonths() | Returns a copy of the date with a number of months added | LocalDate |
| plusYears() | Returns a copy of the date with a number of years added | LocalDate |
| isBefore() | Checks if the date comes before another date | boolean |
| isAfter() | Checks if the date comes after another date | boolean |
| isLeapYear() | Checks if the year of the date is a leap year | boolean |
| lengthOfMonth() | Returns the number of days in the month of the date | int |
| format() | Returns the date as text, in the format of a DateTimeFormatter | String |
LocalTime Methods
A LocalTime object holds a time (hour, minute, second and nanosecond), like 14:30.
| Method | Description | Return Type |
|---|---|---|
| now() | Returns the current time, read from the system clock | LocalTime |
| of() | Creates a time from an hour and a minute, and optionally a second | LocalTime |
| getHour() | Returns the hour of the time, from 0 to 23 | int |
| getMinute() | Returns the minute of the time, from 0 to 59 | int |
| plusHours() | Returns a copy of the time with a number of hours added | LocalTime |
| plusMinutes() | Returns a copy of the time with a number of minutes added | LocalTime |
LocalDateTime Methods
A LocalDateTime object holds both a date and a time, like 2026-01-15T14:30.
| Method | Description | Return Type |
|---|---|---|
| now() | Returns the current date and time, read from the system clock | LocalDateTime |
| of() | Creates a date and time from a year, month, day, hour and minute | LocalDateTime |
| toLocalDate() | Returns the date part, as a LocalDate | LocalDate |
| toLocalTime() | Returns the time part, as a LocalTime | LocalTime |
| format() | Returns the date and time as text, in the format of a DateTimeFormatter | String |
DateTimeFormatter Methods
A DateTimeFormatter object describes how to show a date or time as text, like 15/01/2026.
| Method | Description | Return Type |
|---|---|---|
| ofPattern() | Creates a formatter from a pattern of letters, like dd/MM/yyyy | DateTimeFormatter |
| format() | Formats a date or time object as text | String |