Saturday, 20 August 2022

Java: Get current date and time using LocalDate, LocalTime, LocalDateTime

Get current date from LocalDate

LocalDate.now() return a date without a time-zone in the ISO-8601 calendar system, such as 2022-08-21.

 

Get current time from LocalTime

'LocalTime.now()' return A time without a time-zone in the ISO-8601 calendar system, such as 11:47:16.436502.

 

Get current date and time from LocalDateTime

LocalDateTime.now() return a date-time without a time-zone in the ISO-8601 calendar system, such as 2022-08-21T11:47:16.436539

 

Find the below working application.

 


LocalDateTimeDemo.java

package com.sample.app;

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

public class LocalDateTimeDemo {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println("localDate : " + localDate);
        System.out.println("localTime : " + localTime);
        System.out.println("localDateTime : " + localDateTime);
    }

}

 

Output

localDate : 2022-08-21
localTime : 11:47:16.436502
localDateTime : 2022-08-21T11:47:16.436539

 


Previous                                                 Next                                                 Home

No comments:

Post a Comment