Get Current Date and Time in Java

thumbnail

In this tutorial, you will learn to get the current date and time in different format in Java. You can also learn how to get the current date and time in different format in Kotlin from this link.

Example 1: Get current date and time in default format

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class GetCurrentDateTime {
    public static void main(String[] args) {
        // Example 1: Get Current date and time in default format
        LocalDateTime current1 = LocalDateTime.now();
        System.out.println("Current Date and Time is: " + current1);
    }
}

The output from the above code will be:

Current Date and Time is: 2019-09-29T14:06:26.792

In the above code, the current date and time is stored in variable current using LocalDateTime.now() method. For the default format, it is simply converted from a LocalDateTime object to a string internally using a toString() method.

Example 2: Get current date and time with pattern

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class GetCurrentDateTime {
    public static void main(String[] args) { 
        // Example 2: Get Current date and time with pattern
        LocalDateTime current2 = LocalDateTime.now();
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        String formatted2 = current2.format(formatter2);
        System.out.println("Current Date and Time is: " + formatted2);
    }
}

The output will be:

Current Date and Time is: 2019-09-29 14:06:26.793

In the above code, we’ve defined a pattern of format Year-Month-Day Hours:Minutes:Seconds.Milliseconds using DateTimeFormatter object. Then, we’ve used LocalDateTime’s format() method to use the given formatter. This gets us the formatted string output.

Example 3: Get current date time using predefined constants

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class GetCurrentDateTime {
    public static void main(String[] args) { 
        // Example 3: Get Current Date time using predefined constants
        LocalDateTime current3 = LocalDateTime.now();
        DateTimeFormatter formatter3 = DateTimeFormatter.BASIC_ISO_DATE;
        String formatted3 = current3.format(formatter3);
        System.out.println("Current Date is: " + formatted3);
    }
}

The output will be:

Current Date is: 20190929

From the above program, we’ve used a predefined format constant BASIC_ISO_DATE to get the current ISO date as the output.

Example 4: Get current date time in localized style

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class GetCurrentDateTime {
    public static void main(String[] args) { 
        // Example 4: Get Current Date time in localized style
        LocalDateTime current4 = LocalDateTime.now();
        DateTimeFormatter formatter4 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        String formatted4 = current4.format(formatter4);
        System.out.println("Current Date is: " + formatted4);
    }
}

The output will be:

Current Date is: 29-Sep-2019 14:06:26

We’ve used a localized style Medium to get the current date time in the given format. You can find another sytles as well: Full, Long, and Short. You can find the full code in my github. For another tutorial you can find it in this website.

Back To Top