Java 8 – How to get current Date and Time ?

In this article, we will learn how to get current system date and time in Java 1.8 version using newly introduced Date/Time API or Joda Time API

While developing any application, it is required to obtain current date and time in either default or specific formats so as to capture the application access date/time or profile modification date/time or transaction date/time and store it in the database for future retrieval

Get current System Date and Time :

  1. Using java.time.LocalDate
  2. Using java.time.LocalTime
  3. Using java.time.LocalDateTime
  4. Using java.time.ZonedDateTime
  5. Using java.time.OffsetDateTime
  6. Using java.time.Instant
  7. Using java.util.Date
  8. Using java.util.Calendar

1. Using java.time.LocalDate

  • LocalDate.now() method returns current system date consisting of year, month and day fields in the default yyyy-MM-dd format
  • We can convert yyyy-MM-dd format to any of the custom format/pattern using DateTimeFormatter
  • We can also convert default format to any of the below listed Format Style like,
    1. FormatStyle.SHORT
    2. FormatStyle.MEDIUM
    3. FormatStyle.LONG
    4. FormatStyle.FULL
  • Finally, print default and custom formats to the console

CurrentDateTimeInJavaUsingLocalDate.java

package in.bench.resources.java8.current.date.time;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class CurrentDateTimeInJavaUsingLocalDate {

	public static void main(String[] args) {

		// 1. current date in default zone
		LocalDate localDate = LocalDate.now();
		System.out.println("Current Date :- " + localDate);
		System.out.println("Default Zone :- " + ZoneId.systemDefault());


		// 2. format localDate to dd-MM-yyyy
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
		String currentDateInStr = dateTimeFormatter.format(localDate);
		System.out.println("\nCurrent Date in dd-MM-yyyy format is :- \n" 
				+ currentDateInStr);


		// 3. format localDate to FULL Format Style
		DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
		String currentDateInStr2 = dateTimeFormatter2.format(localDate);
		System.out.println("\nCurrent Date in FULL Format Style is :- \n" 
				+ currentDateInStr2);
	}
}

Output:

Current Date :- 2022-07-27
Default Zone :- Asia/Calcutta

Current Date in dd-MM-yyyy format is :- 
27-07-2022

Current Date in FULL Format Style is :- 
Wednesday, 27 July, 2022

2. Using java.time.LocalTime

  • LocalTime.now() method returns current system time consisting of hour, minute, second and nanosecond fields in the default HH:mm:ss.nnn format
  • We can convert HH:mm:ss.nnn format to any of the custom format/pattern using DateTimeFormatter
  • We can also convert default format to any of the below listed Format Style like,
    1. FormatStyle.SHORT
    2. FormatStyle.MEDIUM
  • Note: trying to format Date/Time in FormatStyle.LONG or FormatStyle.FULL throws exception stating ZoneId information missing

CurrentDateTimeInJavaUsingLocalTime.java

package in.bench.resources.java8.current.date.time;

import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class CurrentDateTimeInJavaUsingLocalTime {

	public static void main(String[] args) {

		// 1. current date in default zone
		LocalTime localTime = LocalTime.now();
		System.out.println("Current Time :- " + localTime);
		System.out.println("Default Zone :- " + ZoneId.systemDefault());


		// 2. format localTime to HH:mm:ss
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
		String currentTimeInStr = dateTimeFormatter.format(localTime);
		System.out.println("\nCurrent Time in HH:mm:ss format is :- \n" 
				+ currentTimeInStr);


		// 3. format localTime to MEDIUM Format Style
		DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
		String currentDateInStr2 = dateTimeFormatter2.format(localTime);
		System.out.println("\nCurrent Time in Medium Format Style is :- \n" 
				+ currentDateInStr2);
	}
}

Output:

Current Time :- 18:02:47.143893800
Default Zone :- Asia/Calcutta

Current Time in HH:mm:ss format is :- 
18:02:47

Current Time in Medium Format Style is :- 
6:02:47 pm

3. Using java.time.LocalDateTime

  • LocalDateTime.now() method returns current system date and time consisting of day, month, year, hour, minute, second and nanosecond fields in the default yyyy-MM-ddTHH:mm:ss.nnn format
  • We can convert yyyy-MM-ddTHH:mm:ss.nnn format to any of the custom format/pattern using DateTimeFormatter
  • We can also convert default format to any of the below listed Format Style like,
    1. FormatStyle.SHORT
    2. FormatStyle.MEDIUM
  • Note: trying to format Date/Time in FormatStyle.LONG or FormatStyle.FULL throws exception stating ZoneId information missing

CurrentDateTimeInJavaUsingLocalDateTime.java

package in.bench.resources.java8.current.date.time;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class CurrentDateTimeInJavaUsingLocalDateTime {

	public static void main(String[] args) {

		// 1. current date/time in default zone
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current Date/Time :- " + localDateTime);
		System.out.println("Default Zone :- " + ZoneId.systemDefault());


		// 2. format LocalDateTime to dd-MM-yyyy HH:mm:ss
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
		String currentDateTimeInStr = dateTimeFormatter.format(localDateTime);
		System.out.println("\nCurrent Date/Time in dd-MM-yyyy HH:mm:ss format is :- \n"
				+ currentDateTimeInStr);


		// 3. format localDateTime to MEDIUM Format Style
		DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
		String currentDateInStr2 = dateTimeFormatter2.format(localDateTime);
		System.out.println("\nCurrent Date/Time in Medium Format Style is :- \n" 
				+ currentDateInStr2);


		// 4. format LocalDateTime to MMM d, uuuu h:mm:ss a zzz
		DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("E MMM d, uuuu hh:mm:ss a");
		String currentDateTimeInStr3 = dateTimeFormatter3.format(localDateTime);
		System.out.println("\nCurrent Date/Time in dd-MM-yyyy HH:mm:ss format is :- \n"
				+ currentDateTimeInStr3);
	}
}

Output:

Current Date/Time :- 2022-07-27T18:03:42.792753600
Default Zone :- Asia/Calcutta

Current Date/Time in dd-MM-yyyy HH:mm:ss format is :- 
27-07-2022 18:03:42

Current Date/Time in Medium Format Style is :- 
27-Jul-2022, 6:03:42 pm

Current Date/Time in dd-MM-yyyy HH:mm:ss format is :- 
Wed Jul 27, 2022 06:03:42 pm

4. Using java.time.ZonedDateTime

  • ZonedDateTime.now() method returns current system date and time along with Zone information consisting of day, month, year, hour, minute, second, nanosecond, offset and Zone fields in the default yyyy-MM-ddTHH:mm:ss.nnnO[VV] format
  • We can convert yyyy-MM-ddTHH:mm:ss.nnnO[VV] format to any of the custom format/pattern using DateTimeFormatter
  • We can also convert default format to any of the below listed Format Style like,
    1. FormatStyle.SHORT
    2. FormatStyle.MEDIUM
    3. FormatStyle.LONG
    4. FormatStyle.FULL
  • Finally, print default and custom formatted date/time in the console

CurrentDateTimeInJavaUsingZonedDateTime.java

package in.bench.resources.java8.current.date.time;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class CurrentDateTimeInJavaUsingZonedDateTime {

	public static void main(String[] args) {

		// 1. current date/time in default zone
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Current Date/Time :- " + zonedDateTime);
		System.out.println("Default Zone :- " + zonedDateTime.getZone());
		System.out.println("Default Offset :- " + zonedDateTime.getOffset());


		// 2. format ZonedDateTime to dd-MM-yyyy HH:mm:ss.nnn O VV
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss.nnn O VV");
		String currentDateTimeInStr = dateTimeFormatter.format(zonedDateTime);
		System.out.println("\nCurrent Date/Time in dd-MM-yyyy HH:mm:ss.nnn O VV format is :- \n"
				+ currentDateTimeInStr);


		// 3. format zonedDateTime to LONG Format Style
		DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
		String currentDateInStr2 = dateTimeFormatter2.format(zonedDateTime);
		System.out.println("\nCurrent Date/Time in Long Format Style is :- \n" 
				+ currentDateInStr2);


		// 4. format zonedDateTime to FULL Format Style
		DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
		String currentDateInStr3 = dateTimeFormatter3.format(zonedDateTime);
		System.out.println("\nCurrent Date/Time in Full Format Style is :- \n" 
				+ currentDateInStr3);
	}
}

Output:

Current Date/Time :- 2022-07-27T18:04:25.382835+05:30[Asia/Calcutta]
Default Zone :- Asia/Calcutta
Default Offset :- +05:30

Current Date/Time in dd-MM-yyyy HH:mm:ss.nnn O VV format is :- 
27-07-2022 18:04:25.382835000 GMT+5:30 Asia/Calcutta

Current Date/Time in Long Format Style is :- 
27 July 2022 at 6:04:25 pm IST

Current Date/Time in Full Format Style is :- 
Wednesday, 27 July, 2022 at 6:04:25 pm India Standard Time

5. Using java.time.OffsetDateTime

  • OffsetDateTime.now() method returns current system date and time along with Offset information consisting of day, month, year, hour, minute, second, nanosecond and offset fields in the default yyyy-MM-ddTHH:mm:ss.nnnO format
  • We can convert yyyy-MM-ddTHH:mm:ss.nnnO format to any of the custom format/pattern using DateTimeFormatter
  • We can also convert default format to any of the below listed Format Style like,
    1. FormatStyle.SHORT
    2. FormatStyle.MEDIUM
  • Note: trying to format Date/Time in FormatStyle.LONG or FormatStyle.FULL throws exception stating ZoneId information missing

CurrentDateTimeInJavaUsingOffsetDateTime.java

package in.bench.resources.java8.current.date.time;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class CurrentDateTimeInJavaUsingOffsetDateTime {

	public static void main(String[] args) {

		// 1. current date/time in default zone
		OffsetDateTime offsetDateTime = OffsetDateTime.now();
		System.out.println("Current Date/Time :- " + offsetDateTime);
		System.out.println("Default Offset :- " + offsetDateTime.getOffset());


		// 2. format OffsetDateTime to dd-MM-yyyy HH:mm:ss.nnn O
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss.nnn O");
		String currentDateTimeInStr = dateTimeFormatter.format(offsetDateTime);
		System.out.println("\nCurrent Date/Time in dd-MM-yyyy HH:mm:ss.nnn O format is :- \n"
				+ currentDateTimeInStr);


		// 3. format OffsetDateTime to SHORT Format Style
		DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
		String currentDateInStr2 = dateTimeFormatter2.format(offsetDateTime);
		System.out.println("\nCurrent Date/Time in Short Format Style is :- \n" 
				+ currentDateInStr2);


		// 4. format OffsetDateTime to MEDIUM Format Style
		DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
		String currentDateInStr3 = dateTimeFormatter3.format(offsetDateTime);
		System.out.println("\nCurrent Date/Time in Medium Format Style is :- \n" 
				+ currentDateInStr3);
	}
}

Output:

Current Date/Time :- 2022-07-27T18:04:48.344221+05:30
Default Offset :- +05:30

Current Date/Time in dd-MM-yyyy HH:mm:ss.nnn O format is :- 
27-07-2022 18:04:48.344221000 GMT+5:30

Current Date/Time in Short Format Style is :- 
27/07/22, 6:04 pm

Current Date/Time in Medium Format Style is :- 
27-Jul-2022, 6:04:48 pm

6. Using java.time.Instant

  • Instant.now() method returns current system date and time at UTC/GMT with Z suffix in the default yyyy-MM-ddTHH:mm:ss.nnnZ format
  • Above Instant consists of day, month, year, hour, minute, second and nanosecond with suffix Z at the end stating instantaneous moment at GMT/UTC
  • We can convert Instant to any of the following using atZone() and atOffset() methods,
    1. LocalDate
    2. LocalTime
    3. LocalDateTime
    4. ZonedDateTime
    5. OffsetDateTime

CurrentDateTimeInJavaUsingInstant.java

package in.bench.resources.java8.current.date.time;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class CurrentDateTimeInJavaUsingInstant {

	public static void main(String[] args) {

		// 1. current date/time in default zone
		Instant nowInstant = Instant.now();
		System.out.println("Current Instant at UTC/GMT :- " + nowInstant);
		System.out.println("Default Zone :- " + ZoneId.systemDefault());


		// 2. convert Instant now() to LocalDate
		LocalDate localDate = nowInstant.atZone(ZoneId.systemDefault()).toLocalDate();
		System.out.println("\nInstant to LocalDate :- " + localDate);


		// 3. convert Instant now() to LocalTime
		LocalTime localTime = nowInstant.atZone(ZoneId.systemDefault()).toLocalTime();
		System.out.println("\nInstant to LocalTime :- " + localTime);


		// 4. convert Instant now() to LocalDateTime
		LocalDateTime localDateTime = nowInstant.atZone(ZoneId.systemDefault()).toLocalDateTime();
		System.out.println("\nInstant to LocalDateTime :- " + localDateTime);


		// 5. convert Instant now() to ZonedDateTime
		ZonedDateTime zonedDateTime = nowInstant.atZone(ZoneId.systemDefault());
		System.out.println("\nInstant to ZonedDateTime :- " + zonedDateTime);


		// 6. convert Instant now() to ZonedDateTime
		OffsetDateTime offsetDateTime = nowInstant.atOffset(ZoneOffset.of("+05:30"));
		System.out.println("\nInstant to OffsetDateTime :- " + offsetDateTime);
	}
}

Output:

Current Instant at UTC/GMT :- 2022-07-27T12:35:05.066036900Z
Default Zone :- Asia/Calcutta

Instant to LocalDate :- 2022-07-27

Instant to LocalTime :- 18:05:05.066036900

Instant to LocalDateTime :- 2022-07-27T18:05:05.066036900

Instant to ZonedDateTime :- 2022-07-27T18:05:05.066036900+05:30[Asia/Calcutta]

Instant to OffsetDateTime :- 2022-07-27T18:05:05.066036900+05:30

7. Using java.util.Date

  • Instantiating Date class returns current Date/Time in the default format – EEE MMM dd HH:mm:ss zzz yyyy
  • We can convert default format to custom format using DateFormat and SimpleDateFormat classes

CurrentDateTimeInJavaUsingDate.java

package in.bench.resources.java8.current.date.time;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CurrentDateTimeInJavaUsingDate {

	public static void main(String[] args) {

		// 1. current date
		Date date = new Date();
		System.out.println("Current Date/Time"
				+ " using Date() :- \n" + date);


		// 1.1 format
		DateFormat dateFormat =  new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String str = dateFormat.format(date);
		System.out.println("\nFormatted Date/Time in dd-MM-yyyy HH:mm:ss format"
				+ " using Date() :- \n" + str);



		// 2. current date
		Date date2 = new Date(System.currentTimeMillis());
		System.out.println("\n\nCurrent Date/Time"
				+ " using Date(System.currentTimeMillis()) :- \n" + date2);


		// 2.1 format
		DateFormat dateFormat2 =  new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
		str = dateFormat2.format(date2);
		System.out.println("\nFormatted Date/Time in dd.MM.yyyy HH:mm:ss format"
				+ " using Date(System.currentTimeMillis()) :- \n" + str);
	}
}

Output:

Current Date/Time using Date() :- 
Wed Jul 27 18:05:21 IST 2022

Formatted Date/Time in dd-MM-yyyy HH:mm:ss format using Date() :- 
27-07-2022 18:05:21


Current Date/Time using Date(System.currentTimeMillis()) :- 
Wed Jul 27 18:05:21 IST 2022

Formatted Date/Time in dd.MM.yyyy HH:mm:ss format using Date(System.currentTimeMillis()) :- 
27.07.2022 18:05:21

8. Using java.util.Calendar

  • Calendar.getInstance() method returns Calendar which has many useful methods and one such method is getTime() which returns current System Date/Time in the default format – EEE MMM dd HH:mm:ss zzz yyyy
  • We can convert default format to custom format using DateFormat and SimpleDateFormat classes

CurrentDateTimeInJavaUsingCalendar.java

package in.bench.resources.java8.current.date.time;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CurrentDateTimeInJavaUsingCalendar {

	public static void main(String[] args) {

		// 1. current date
		Calendar calendar = Calendar.getInstance();
		System.out.println("Current Date/Time"
				+ " using Calendar :- \n" + calendar.getTime());


		// 2. format dd-MM-yyyy HH:mm:ss
		DateFormat dateFormat =  new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		String str = dateFormat.format(calendar.getTime());
		System.out.println("\nFormatted Date/Time"
				+ " using Calendar :- \n" + str);
	}
}

Output:

Current Date/Time using Calendar :- 
Wed Jul 27 18:05:37 IST 2022

Formatted Date/Time using Calendar :- 
27/07/2022 18:05:37

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 - How to get Year, Month and Day fields from LocalDate ?
Java 8 – How to Sort List by OffsetDateTime in 4 ways ?