Get startedGet started for free

Introduction to Date and Time in Java

1. Introduction to Date and Time in Java

In Java, handling date and time is key to building reliable software. Whether scheduling tasks, logging activity, or managing events across time zones, accurate time handling helps avoid errors and ensures consistency, making applications more dependable and user-friendly.

2. Creating Date objects

The `LocalDate` and `LocalTime` classes provide an easy way to work separately with dates or times in Java, especially useful when the date or time alone matters to our application. To get started, we import both classes from `java.time`.

3. Creating Date objects(continued)

We use the `.now()` method to easily retrieve the current date or time. In this example, `LocalDate.now()` gives us today's date, and `LocalTime.now()` provides the current time. When we run this program, it prints the exact date and time based on the moment of execution.

4. Formatting Dates in Java

When working with dates, we often need to format them in a specific structure. Java provides the `DateTimeFormatter` class, which allows us to define custom date formats. The formatter can be customized to support different formats, such as `yyyy-MM-dd` for standard date format or `MM/dd/yyyy` for US-style date representation. Before using it, we need to import `LocalDate` from the `java.time` package and `DateTimeFormatter` from the `java.time.format` package.

5. Formatting Dates in Java(continued)

Now that we have seen `DateTimeFormatter`, let’s apply it. First, we retrieve the current date using `LocalDate.now()`, which will return a date in `yyyy-MM-dd` format by default. In our example, it is `2025-03-10`, though the exact date will vary depending on when the user runs the program. Next, we define a `DateTimeFormatter` named `formatter` using `.ofPattern()` along with the target format `MM/dd/yyyy`. Lastly, we used `.format()` to format the `date` object we created earlier. At the end, the `date` object converted to `MM/dd/yyyy` format - in this case, `03/10/2025`.

6. Parsing String into Date

Sometimes, dates are provided as text, such as in configuration files or user input. To convert them into `LocalDate`, we use the `.parse()` method. Here, we provide a date string `2024-03-10`, and `.parse()` converts it into a `LocalDate` object. It will output `2024-03-10` when we print the `LocalDate` object `parsedDate`.

7. Performing Date adjustment

Working with dates often involves calculations, such as scheduling deadlines. Java provides `.plusDays()`, `.minusDays()`, and other arithmetic methods to adjust dates. We will retrieve the current date using `.now()`, which gives us the current date `2025-03-10` Then we add 7 days to the current date using `.plusDays()`, giving us a new `LocalDate` for one week later. Printing the `futureDate` object will output `2025-03-17`.

8. Performing Date adjustment(continued)

We can also get the date in the past by applying `.minusDays()`. In this example, we subtract 7 days to the current date using `.minusDays()`, giving us a new `LocalDate` for one week earlier. Printing the `pastDate` object will output `2025-03-03`.

9. Summary

In this video, we used `LocalDate` and `LocalTime` to manage dates and times, formatted and parsed dates with `DateTimeFormatter`, and performed date calculations with `.plusDays()` and `.minusDays()`. This enables effective date handling in Java.

10. Let's practice!

Now, let’s apply these concepts in the exercises!