Date Time Functions in Java

Handling dates and times is a crucial aspect of many software applications, ranging from scheduling events to calculating time differences between transactions. Prior to Java 8, Java's standard library provided the 'java.util.Date' class for representing dates and times, but it had several shortcomings, including mutability, lack of thread safety, and limited functionality.
To address these issues and provide a modern, comprehensive API for date and time manipulation, Java 8 introduced the 'java.time package'. This package, also known as the Date-Time API, includes a set of classes to represent dates, times, durations, and periods. It follows the ISO 8601 standard for date and time representation and provides better support for internationalization and time zone handling.
The key classes and interfaces in the java.time package include:


1.Instant:Represents a point in time, typically used for machine-based timing.

Instant now = Instant.now();

2.LocalDate:Represents a date without a time zone, consisting of year, month, and day.

LocalDate date = LocalDate.now();

3.LocalTime:Represents a time without a time zone, consisting of hour, minute, second, and fractional seconds.

LocalTime time = LocalTime.now();

4.LocalDateTime:Represents a date and time without a time zone.

LocalDateTime dateTime = LocalDateTime.now();

5.ZonedDateTime:Represents a date and time with a time zone.

ZonedDateTime zonedDateTime = ZonedDateTime.now();

6.DateTimeFormatter:Allows formatting and parsing of dates and times.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = dateTime.format(formatter);

7.Period:Represents a period of time, such as "2 years and 3 months".

Period period = Period.ofYears(2).plusMonths(3);

8.Duration: Represents a duration of time, such as "5 hours and 30 minutes"..

Duration duration = Duration.ofHours(5).plusMinutes(30);

9.TemporalAdjusters:Provides methods to adjust dates, such as finding the first day of the month.

LocalDate firstDayOfMonth = date.with(TemporalAdjusters.firstDayOfMonth());

10.ChronoUnit:Represents units of time, such as days, hours, minutes, etc.

long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
  • Real-life examples demonstrating the use of date time functions:
  • Question.1 Write a Java program to get the last day of the current month.

    Solution
    import java.util.*; public class PBA { public static void main(String[] args) { //Gets a calendar using the default time zone and locale. Calendar calendar = Calendar.getInstance(); System.out.println(); System.out.println(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); System.out.println(); } }

    Question.2 Write a Java program to get the dates 10 days before and after today.

    Solution
    import java.time.*; public class PBA { public static void main(String[] args) { LocalDate today = LocalDate.now(); System.out.println("\nCurrent Date: "+today); System.out.println("10 days before today will be "+today.plusDays(-10)); System.out.println("10 days after today will be "+today.plusDays(10)+"\n"); } }

    OUTPUT:
    Current Date: 2024-07-20
    10 days before today will be 2024-07-10
    10 days after today will be 2024-07-30

  • Practice Exercise:
  • 1.Write a Java program to create a Date object using the Calendar class.
    2.Write a Java program to get the maximum value of the year, month, week, date from the current date of a default calendar.
    3.Write a Java program to calculate the first and last day of each week.

  • Conclusion:
  • In conclusion, Java's java.time package revolutionizes date and time handling with its comprehensive set of classes and methods. Offering immutability, thread safety, and adherence to ISO 8601 standards, it surpasses the limitations of the legacy java.util.Date class. With functionalities like formatting, parsing, and time zone support, it provides a robust solution for modern date and time requirements, making code more readable and maintainable.