Java – How to get current Date and Time | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Find several examples to get the current date, current time, current date & time, current date & time in a specific timezone in Java.

Get current Date in Java

package com.example.java.programming.datetime;

import java.time.LocalDate;

/**
 * @author code.factory
 *
 */
public class CurrentDateExample {
	public static void main(String... args) {
		LocalDate currentDate = LocalDate.now();
		System.out.println("Current Date : " + currentDate);
	}
}

Output :

Current Date : 2020-06-29

Get current Time in Java

package com.example.java.programming.datetime;

import java.time.LocalTime;

/**
 * @author code.factory
 *
 */
public class CurrentTimeExample {
	public static void main(String... args) {
		LocalTime currentTime = LocalTime.now();
		System.out.println("Current Time : " + currentTime);
	}
}

Output :

Current Time : 14:00:59.165

Get current Date and Time in Java

package com.example.java.programming.datetime;

import java.time.LocalDateTime;

/**
 * @author code.factory
 *
 */
public class CurrentDateTimeExample {
	public static void main(String... args) {
		LocalDateTime currentDateTime = LocalDateTime.now();
		System.out.println("Current Date & Time : " + currentDateTime);
	}
}

Output :

Current Date & Time : 2020-06-29T14:03:56.960

Get current Date and Time in a specific Timezone in Java

package com.example.java.programming.datetime;

import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * @author code.factory
 *
 */
public class CurrentDateTimeExample {
	public static void main(String... args) {
		ZonedDateTime currentKolkataDateTime = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
		System.out.println("Current Date & Time : " + currentKolkataDateTime);
	}
}

Output :

Current Date & Time : 2020-06-29T14:11:49.193+05:30[Asia/Kolkata]

2 thoughts on “Java – How to get current Date and Time | Code Factory”

Leave a comment