How to Get Current Date and Current Time

If you are working on Java 8, it is recommended to use java.time package. If you are using prior to Java 8, it is often recommended to go for Joda API. In this post, we will discuss on how to get current date and current time using java.time package.

To obtain current date from system clock in default time zone:
String str;
str = LocalDate.now().toString();
System.out.println(str); // 2020-04-18
To obtain current time from system clock in default time zone:
str = LocalTime.now().toString();
System.out.println(str); // 06:30:06.006
To obtain current date and time from system clock in default time zone:
str = LocalDateTime.now().toString();
System.out.println(str); // 2020-04-18T06:30:06.006
To obtain current date and time from system clock in default time zone using ZonedDateTime:
str = ZonedDateTime.now().toString();
System.out.println(str); // 2020-04-18T06:30:06.007+05:30[Asia/Calcutta]
To obtain today’s date from system clock in default time zone using ZonedDateTime and format only date:
str = ZonedDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(str); // 2020-04-18
To obtain today’s date from system clock in default time zone using ZonedDateTime and format only time:
str = ZonedDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME);
System.out.println(str); // 06:30:06.03
To obtain today’s date from system clock in default time zone using ZonedDateTime and format date and time:
str = ZonedDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(str); // 2020-04-18T06:30:06.032
To obtain current date and current time from system clock in specified time zone (ref: IANA Time Zone Database for your {area}/{city}):
str = ZonedDateTime.now(ZoneId.of("America/Montreal")).toString();
System.out.println(str); // 2020-04-17T21:00:06.035-04:00[America/Montreal]
To obtain today’s date and current time from system clock in specified time zone:
str = ZonedDateTime.now(ZoneId.ofOffset("GMT", ZoneOffset.of("-4"))).toString();
System.out.println(str); // 2020-04-17T21:00:06.038-04:00[GMT-04:00]
To obtain current time from system clock in specified time zone:
str = ZonedDateTime.now(ZoneId.ofOffset("GMT", 
			ZoneOffset.of("-4"))).format(DateTimeFormatter.ISO_LOCAL_TIME);
System.out.println(str); // 21:00:06.038

Full Example:

package com.techstackjournal;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class CurrentDate {

	public static void main(String[] args) {

		String str;
		str = LocalDate.now().toString();
		System.out.println(str);

		str = LocalTime.now().toString();
		System.out.println(str);

		str = LocalDateTime.now().toString();
		System.out.println(str);

		str = ZonedDateTime.now().toString();
		System.out.println(str);
	
		str = ZonedDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE);
		System.out.println(str);

		str = ZonedDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME);
		System.out.println(str);

		str = ZonedDateTime.now().format(
				DateTimeFormatter.ISO_LOCAL_DATE_TIME);
		System.out.println(str);

		str = ZonedDateTime.now(ZoneId.of("America/Montreal")).toString();
		System.out.println(str);

		str = ZonedDateTime.now(ZoneId.ofOffset("GMT", 
				ZoneOffset.of("-4"))).toString();
		System.out.println(str);

		str = ZonedDateTime.now(ZoneId.ofOffset("GMT", 
				ZoneOffset.of("-4")))
				.format(DateTimeFormatter.ISO_LOCAL_TIME);
		System.out.println(str);

	}

}

Output

2020-04-18
06:30:06.006
2020-04-18T06:30:06.006
2020-04-18T06:30:06.007+05:30[Asia/Calcutta]
2020-04-18
06:30:06.03
2020-04-18T06:30:06.032
2020-04-17T21:00:06.035-04:00[America/Montreal]
2020-04-17T21:00:06.038-04:00[GMT-04:00]
21:00:06.038

Leave a Comment