Java - How to get current date and time

Learn to get the current date and time and output it in various formats. In this article, I am using LocalDateTime and DateTimeFormatter to output date and time.

DateTimeFormatter, LocalDateTime is a new class introduced in JAVA8.

It replaces SimpleDateFormat which was used previously.

Get current date/time, output in default format

LocalDateTime.now() gets the current date and time. LocalDate.now only gets the current date.

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

public class Example {
    public static void main(String args[]) {
        LocalDateTime dateAndtime = LocalDateTime.now();
        LocalDate onlyDate = LocalDate.now();
        System.out.println("Current date and time: " + dateAndtime);
        System.out.println("Current date: " + onlyDate);
    }
}

The output will be in the following format:

Current date and time: 2019-03-23T00:05:54.608
Current date: 2019-03-23

For reference, to create LocalData and LocalDateTime of a specific time, you can use of().

LocalDate.of(2019, 3, 22) // 2019년 3월 22일
LocalDateTime.of(2019, 3, 22, 10, 10, 10) // 2019년 3월 22일 10시 10분 10초

Output in predefined format

You can use DateTimeFormatter to output in different formats. ISO_DATE is a predefined format provided by the library. Other than that, there are many other things.

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

public class Example {
    public static void main(String args[]) {
        LocalDateTime current = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
        String formatted = current.format(formatter);
        System.out.println("Current: " + formatted);
    }
}

Print

Current: 2019-03-22

The formats of DateTimeFormatter provided by the library are as follows.

constant output example
ISO_DATE_TIME 2019-03-22T23:56:36.4
ISO_LOCAL_DATE 2019-03-22
ISO_LOCAL_TIME 23:56:36.4
ISO_LOCAL_DATE_TIME 2019-03-22T23:56:36.4
ISO_DATE 2019-03-22
ISO_TIME 23:56:36.4

output in other formats

You can use DateTimeFormatter.ofPattern() to output in any format you want. Instead, you need to create a pattern and pass it as an argument.

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

public class Example {
    public static void main(String args[]) {
        LocalDateTime current = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시 mm분 ss초");
        String formatted = current.format(formatter);
        System.out.println("Current: " + formatted);
    }
}

Print

Current: 2019년 03월 22일 23시 46분 36초

You can use the following patterns in DateTimeFormatter.ofPattern().

pattern Example
yyyy-MM-dd “2019-07-04”
dd-MMM-yyyy “04-July-2019”
dd/MM/yyyy “04/07/2019”
yyyy-MM-dd'T'HH:mm:ssZ “2019-07-04T12:30:30+0530”
h:mm a “12:00 PM”
MM dd, yyyy "January 10, 2019"

Clean up

We saw how to create LocalDateTime and LocalDate and output them in various ways.

codechachaCopyright ©2019 codechacha