In this tutorial we gonna learn, how to get the current date and time in java using date and time API.


Example:

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

public class JavaApplication {

    public static void main(String[] args) {

        //Display Current Date
        LocalDate current_date = LocalDate.now(); // Create a date object
        System.out.println(current_date); // Display the current date

        ////Display Current Date
        LocalTime current_time = LocalTime.now();
        System.out.println(current_time);

    }

}


output:

run:
2021-03-16
05:41:35.419


Display Current Date and Time

Example 2

import java.time.LocalDateTime;


public class JavaApplication {

    public static void main(String[] args) {

     LocalDateTime date_time = LocalDateTime.now();
    System.out.println(date_time);

    }

}

Output:

run:
2021-03-16T05:46:53.299