Date
, Calendar
etc) from the java.util
package.
Date
and Calendar
classes are mutable, making them not thread-safe.
0
(January), which is confusing for developers.
Calendar
and GregorianCalendar
are verbose and difficult to use for simple tasks.
java.time
package, which is immutable, thread-safe and much easier to use.
java.time.LocalDate
class to represent dates in a
simple, clean and immutable way.
S.No | Class / Interface | Important Methods | Purpose / Description |
---|---|---|---|
1 | LocalDate |
now() , of() , plusDays() , minusDays() , getDayOfWeek() , isLeapYear() |
Represents a date (year, month, day) without time and timezone. |
2 | LocalDateTime |
now() , of() , plusHours() , minusMinutes() , toLocalDate() , toLocalTime() |
Represents a date + time (year, month, day, hour, minute, second) without timezone. |
3 | ZonedDateTime |
now() , of() , withZoneSameInstant() , getZone() , toLocalDateTime() |
Represents a date + time with a specific timezone. |
4 | OffsetDateTime |
now() , of() , getOffset() , withOffsetSameInstant() , toLocalDateTime() |
Represents date and time with an offset from UTC but not full time zone rules. |
5 | Year |
now() , of() , isLeap() , plusYears() , minusYears() |
Represents a specific year. |
6 | YearMonth |
now() , of() , lengthOfMonth() , isLeapYear() , plusMonths() |
Represents a year and month without a day. |
7 | MonthDay |
now() , of() , isValidYear() , atYear() |
Represents a combination of month and day, without year. |
import java.time.*;
public class MainApp1
{
public static void main(String[] args)
{
// 1. LocalDate
LocalDate today = LocalDate.now();
LocalDate specificDate = LocalDate.of(2025, 9, 19);
LocalDate nextWeek = today.plusDays(7);
LocalDate lastWeek = today.minusDays(7);
DayOfWeek dayOfWeek = today.getDayOfWeek();
boolean isLeap = today.isLeapYear();
System.out.println("------ LocalDate ------");
System.out.println("Today: " + today);
System.out.println("Specific Date: " + specificDate);
System.out.println("Next Week: " + nextWeek);
System.out.println("Last Week: " + lastWeek);
System.out.println("Day of Week: " + dayOfWeek);
System.out.println("Is Leap Year: " + isLeap);
System.out.println();
// 2. LocalDateTime
LocalDateTime currentDateTime = LocalDateTime.now();
LocalDateTime specificDateTime = LocalDateTime.of(2025, 9, 19, 10, 30, 45);
LocalDateTime plusHours = currentDateTime.plusHours(5);
LocalDateTime minusMinutes = currentDateTime.minusMinutes(15);
System.out.println("------ LocalDateTime ------");
System.out.println("Current DateTime: " + currentDateTime);
System.out.println("Specific DateTime: " + specificDateTime);
System.out.println("Plus 5 Hours: " + plusHours);
System.out.println("Minus 15 Minutes: " + minusMinutes);
System.out.println("To LocalDate: " + currentDateTime.toLocalDate());
System.out.println("To LocalTime: " + currentDateTime.toLocalTime());
System.out.println();
// 3. ZonedDateTime
ZonedDateTime zonedNow = ZonedDateTime.now();
ZonedDateTime specificZoned = ZonedDateTime.of(2025, 9, 19, 10, 30, 0, 0, ZoneId.of("Asia/Kolkata"));
ZonedDateTime convertedZone = zonedNow.withZoneSameInstant(ZoneId.of("Europe/London"));
System.out.println("------ ZonedDateTime ------");
System.out.println("Current ZonedDateTime: " + zonedNow);
System.out.println("Specific ZonedDateTime: " + specificZoned);
System.out.println("Converted to London Zone: " + convertedZone);
System.out.println("Zone: " + zonedNow.getZone());
System.out.println("To LocalDateTime: " + zonedNow.toLocalDateTime());
System.out.println();
// 4. OffsetDateTime
OffsetDateTime offsetNow = OffsetDateTime.now();
OffsetDateTime specificOffset = OffsetDateTime.of(2025, 9, 19, 10, 30, 0, 0, ZoneOffset.ofHours(5));
OffsetDateTime newOffset = offsetNow.withOffsetSameInstant(ZoneOffset.ofHours(2));
System.out.println("------ OffsetDateTime ------");
System.out.println("Current OffsetDateTime: " + offsetNow);
System.out.println("Specific OffsetDateTime: " + specificOffset);
System.out.println("Offset: " + offsetNow.getOffset());
System.out.println("Converted Offset: " + newOffset);
System.out.println("To LocalDateTime: " + offsetNow.toLocalDateTime());
System.out.println();
}
}
------ LocalDate ------ Today: 2025-09-19 Specific Date: 2025-09-19 Next Week: 2025-09-26 Last Week: 2025-09-12 Day of Week: FRIDAY Is Leap Year: false ------ LocalDateTime ------ Current DateTime: 2025-09-19T08:57:58.855210900 Specific DateTime: 2025-09-19T10:30:45 Plus 5 Hours: 2025-09-19T13:57:58.855210900 Minus 15 Minutes: 2025-09-19T08:42:58.855210900 To LocalDate: 2025-09-19 To LocalTime: 08:57:58.855210900 ------ ZonedDateTime ------ Current ZonedDateTime: 2025-09-19T08:57:58.856207900+05:30[Asia/Calcutta] Specific ZonedDateTime: 2025-09-19T10:30+05:30[Asia/Kolkata] Converted to London Zone: 2025-09-19T04:27:58.856207900+01:00[Europe/London] Zone: Asia/Calcutta To LocalDateTime: 2025-09-19T08:57:58.856207900 ------ OffsetDateTime ------ Current OffsetDateTime: 2025-09-19T08:57:58.860198200+05:30 Specific OffsetDateTime: 2025-09-19T10:30+05:00 Offset: +05:30 Converted Offset: 2025-09-19T05:27:58.860198200+02:00 To LocalDateTime: 2025-09-19T08:57:58.860198200
import java.time.*;
public class MainApp2
{
public static void main(String[] args)
{
// 5. Year
Year thisYear = Year.now();
Year specificYear = Year.of(2028);
boolean leap = specificYear.isLeap();
Year nextYear = thisYear.plusYears(1);
Year lastYear = thisYear.minusYears(1);
System.out.println("------ Year ------");
System.out.println("This Year: " + thisYear);
System.out.println("Specific Year: " + specificYear);
System.out.println("Is Leap Year: " + leap);
System.out.println("Next Year: " + nextYear);
System.out.println("Last Year: " + lastYear);
System.out.println();
// 6. YearMonth
YearMonth currentMonth = YearMonth.now();
YearMonth specificMonth = YearMonth.of(2025, 12);
int lengthOfMonth = specificMonth.lengthOfMonth();
boolean isLeapMonthYear = specificMonth.isLeapYear();
YearMonth nextMonth = currentMonth.plusMonths(1);
System.out.println("------ YearMonth ------");
System.out.println("Current YearMonth: " + currentMonth);
System.out.println("Specific YearMonth: " + specificMonth);
System.out.println("Length of Month: " + lengthOfMonth);
System.out.println("Is Leap Year: " + isLeapMonthYear);
System.out.println("Next Month: " + nextMonth);
System.out.println();
// 7. MonthDay
MonthDay todayMonthDay = MonthDay.now();
MonthDay specificMonthDay = MonthDay.of(12, 25);
boolean validForYear = specificMonthDay.isValidYear(2025);
LocalDate dateFromMonthDay = specificMonthDay.atYear(2025);
System.out.println("------ MonthDay ------");
System.out.println("Today MonthDay: " + todayMonthDay);
System.out.println("Specific MonthDay: " + specificMonthDay);
System.out.println("Is Valid for 2025: " + validForYear);
System.out.println("Converted to LocalDate: " + dateFromMonthDay);
}
}
------ Year ------ This Year: 2025 Specific Year: 2028 Is Leap Year: true Next Year: 2026 Last Year: 2024 ------ YearMonth ------ Current YearMonth: 2025-09 Specific YearMonth: 2025-12 Length of Month: 31 Is Leap Year: false Next Month: 2025-10 ------ MonthDay ------ Today MonthDay: --09-19 Specific MonthDay: --12-25 Is Valid for 2025: true Converted to LocalDate: 2025-12-25
java.time.LocalTime
class to represent time in a
simple, clean, and immutable way.
S.No | Class / Interface | Important Methods | Purpose / Description |
---|---|---|---|
1 | LocalTime |
now() , of() , plusHours() , minusMinutes() , getHour() , getMinute() |
Represents a time (hour, minute, second, nanosecond) without date and timezone. |
2 | OffsetTime |
now() , of() , getOffset() , withOffsetSameInstant() , toLocalTime() |
Represents time with an offset from UTC, but not full time zone rules. |
3 | Duration |
between() , ofHours() , ofMinutes() , toHours() , toMinutes() |
Represents a time-based amount of time, such as seconds, minutes, or hours. |
4 | Instant |
now() , ofEpochSecond() , plusSeconds() , minusMillis() |
Represents a moment on the timeline in UTC, useful for timestamps. |
5 | ChronoUnit |
between() , plus() , minus() |
Used to measure amounts of time in units like hours, minutes, seconds, etc. |
import java.time.*;
import java.time.temporal.ChronoUnit;
public class MainApp3
{
public static void main(String[] args)
{
// 1. LocalTime
LocalTime nowTime = LocalTime.now();
LocalTime specificTime = LocalTime.of(14, 30, 45);
LocalTime plusHours = nowTime.plusHours(2);
LocalTime minusMinutes = nowTime.minusMinutes(15);
System.out.println("------ LocalTime ------");
System.out.println("Current Time: " + nowTime);
System.out.println("Specific Time: " + specificTime);
System.out.println("Plus 2 Hours: " + plusHours);
System.out.println("Minus 15 Minutes: " + minusMinutes);
System.out.println("Hour: " + nowTime.getHour());
System.out.println("Minute: " + nowTime.getMinute());
System.out.println();
// 2. OffsetTime
OffsetTime offsetNow = OffsetTime.now();
OffsetTime specificOffset = OffsetTime.of(14, 30, 0, 0, ZoneOffset.ofHours(5));
OffsetTime newOffset = offsetNow.withOffsetSameInstant(ZoneOffset.ofHours(2));
System.out.println("------ OffsetTime ------");
System.out.println("Current OffsetTime: " + offsetNow);
System.out.println("Specific OffsetTime: " + specificOffset);
System.out.println("Converted Offset: " + newOffset);
System.out.println("Offset: " + offsetNow.getOffset());
System.out.println("To LocalTime: " + offsetNow.toLocalTime());
System.out.println();
// 3. Duration
LocalTime start = LocalTime.of(9, 0);
LocalTime end = LocalTime.of(17, 30);
Duration duration = Duration.between(start, end);
Duration plusDuration = duration.plusHours(1);
System.out.println("------ Duration ------");
System.out.println("Duration between 9:00 and 17:30: " + duration.toHours() + " hours");
System.out.println("Duration plus 1 hour: " + plusDuration.toHours() + " hours");
System.out.println();
// 4. Instant
Instant instantNow = Instant.now();
Instant instantLater = instantNow.plusSeconds(3600);
System.out.println("------ Instant ------");
System.out.println("Current Instant: " + instantNow);
System.out.println("Instant after 1 hour: " + instantLater);
System.out.println();
// 5. ChronoUnit
long minutesBetween = ChronoUnit.MINUTES.between(start, end);
long hoursBetween = ChronoUnit.HOURS.between(start, end);
System.out.println("------ ChronoUnit ------");
System.out.println("Minutes between 9:00 and 17:30: " + minutesBetween);
System.out.println("Hours between 9:00 and 17:30: " + hoursBetween);
}
}
------ LocalTime ------ Current Time: 08:57:58.123 Specific Time: 14:30:45 Plus 2 Hours: 10:57:58.123 Minus 15 Minutes: 08:42:58.123 Hour: 8 Minute: 57 ------ OffsetTime ------ Current OffsetTime: 08:57:58.123+05:30 Specific OffsetTime: 14:30+05:00 Converted Offset: 05:27:58.123+02:00 Offset: +05:30 To LocalTime: 08:57:58.123 ------ Duration ------ Duration between 9:00 and 17:30: 8 hours Duration plus 1 hour: 9 hours ------ Instant ------ Current Instant: 2025-09-19T03:27:58.123Z Instant after 1 hour: 2025-09-19T04:27:58.123Z ------ ChronoUnit ------ Minutes between 9:00 and 17:30: 510 Hours between 9:00 and 17:30: 8
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.