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.

package org.kodejava.example.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class TodayDateTime {
    public static void main(String[] args) {
        // Obtains the current date from the system clock in the
        // default time-zone.
        LocalDate currentDate =;
        System.out.println("currentDate = " + currentDate);

        // Obtains the current time from the system clock in the
        // default time-zone.
        LocalTime currentTime =;
        System.out.println("currentTime = " + currentTime);

        // Obtains the current date-time from the system clock
        // in the default time-zone.
        LocalDateTime currentDateTime =;
        System.out.println("currentDateTime = " + currentDateTime);

Running this program will give you the following result:

currentDate = 2014-09-05
currentTime = 16:39:24.819
currentDateTime = 2014-09-05T16:39:24.820

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.

Leave a Reply