JVM Languages

Tools

How do I get today’s date and time?

This example show you how to use the new Date and Time API introduced in JDK 8 to get the current date and time. To get today’s date in this new API you can construct an instance of LocalDate, LocalTime or LocalDateTime and call its toString() method.

All of these classes is located under the new java.time package and defined as a final class. Because its constructor declared as a private constructor, this mean you can’t use their constructor to create a new instance. But these classes offer some static factory method to get the value it represents or to create a new instance. For example all of these classes provide a now() method that return the current date, time or date and time information.

Let’s seen a complete code snippet in the TodayDateTime example to demonstrate it.