Course Content
Java OOP
3. Dive Deeper into Java
Java OOP
Dates and Times
The modern Java Date and Time API was introduced in Java 8 and offer a comprehensive and intuitive way to work with dates and times in Java. The previous date and time APIs, such as java.util.Date
and java.util.Calendar
had various design flaws and were considered difficult to use and understand. The modern Java Date and Time API provides a much more intuitive and easy-to-use way to work with dates and times while providing improved performance and accuracy.
Here are the basic steps to work with the Java Date and Time API:
Import the required classes
To work with the modern Java Date and Time API, you will need to import the following classes.
Main.java
Creating dates and times
The basic building blocks of the Java Date and Time API are the LocalDate
, LocalTime
, and LocalDateTime
classes. These classes represent a date, a time, and a combination of a date and time, respectively.
Here is an example of creating a LocalDate
object.
Main.java
This code will create a LocalDate
object that represents the current date and print it to the console.
Similarly, you can create a LocalTime object and a LocalDateTime object.
Main.java
You can also create a specific date or time by specifying the year, month, day, hour, minute, and second.
Main.java
Working with time zones
In addition to the basic date and time classes, the Java Date and Time API also provides the ZonedDateTime
class, which represents a date and time in a specific time zone.
Here is an example of creating a ZonedDateTime
object.
Main.java
You can also create a ZonedDateTime
object for a specific date and time in a specific time zone.
Main.java
Modifying dates and times
The Java Date and Time API provides a variety of methods for modifying dates and times, such as adding or subtracting days, months, or years or setting the hour, minute, or second.
Here is an example of adding one month to a LocalDate
object:
Main.java
This code will create a LocalDate
object for the current date, add one month to it, and print the result.
Formatting dates and times
The Java Date and Time API provides a java.time.format.DateTimeFormatter
class, which allows you to format dates and times into strings and parse strings into dates and times.
Here is an example of formatting a LocalDate
object into a string.
Main.java
This code will create a LocalDate
object for the current date, create a DateTimeFormatter
object with the pattern "dd-MM-yyyy"
, format the date into a string using the formatter, and print the result.
Conclusion
The modern Java Date and Time API provide a powerful and intuitive way to work with dates and times in Java. Whether you need to create, modify, format, or parse dates and times, the Java Date and Time API cover you.
What class allows you to format dates and times into strings, and parse strings into dates and times in the Java Date and Time API?
Select the correct answer
How can you add time to a LocalTime or LocalDateTime object in the Java Date and Time API?
Select the correct answer
Everything was clear?