Date and Time

Java get current date and time based on system date

public final class LocalDate extends Object

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

public class Date extends Object

The class Date represents a specific instant in time, with millisecond precision.

public abstract class Clock extends Object

A clock providing access to the current instant, date and time using a time-zone.

public static Clock systemUTC()

Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the UTC time-zone.

This clock, rather than systemDefaultZone(), should be used when you need the current instant without the date or time.

package com.candidjava.datetime;

import java.time.Clock;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

public class GetCurrentDateAndTime {
	public static void main(String[] args) {
		// Using LocalDateTime Class
		LocalDateTime datetime = LocalDateTime.now();
		System.out.println("Current Date and Time Using LocalDateTime Class:");
		System.out.println(datetime);
		System.out.println("-------------------------------------------------");
		// Using Util Date Class
		Date date = new Date();
		System.out.println("Current Date and Time Using Util Date Class:");
		System.out.println(date);
		System.out.println("-------------------------------------------------");
		// Using Clock class
		System.out.println("Current Date and Time Using Clock Class:");
		System.out.println(Clock.systemUTC().instant());
		System.out.println("-------------------------------------------------");
		// Using Calendar Class
		Date datecalendar = Calendar.getInstance().getTime();
		System.out.println("Current Date and Time Using Calendar Class:");
		System.out.println(datecalendar);
		System.out.println("-------------------------------------------------");

	}

}

OUTPUT

Current Date and Time Using LocalDateTime Class:
2020-11-18T19:51:41.174271100
-------------------------------------------------
Current Date and Time Using Util Date Class:
Wed Nov 18 19:51:41 IST 2020
-------------------------------------------------
Current Date and Time Using Clock Class:
2020-11-18T14:21:41.217244500Z
-------------------------------------------------
Current Date and Time Using Calendar Class:
Wed Nov 18 19:51:41 IST 2020
-------------------------------------------------