Convert a LocalDate to an Instant

Last Updated On Sunday 19th Dec 2021

LocalDate to Instant

In order to convert it to an instant you need to have a LocalDateTime instance

  • Java Instant class is used to represent a specific moment on the time line. This might be used to record event time-stamps in the application.
  • java.util.Date represents an instant on the time-line, not a date. The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z.
  • The equivalent class to java.util.Date in JSR-310 is Instant, thus there is a convenient method toInstant() to provide the conversion.
	public Instant toInstant()
	
  • Converts this Date object to an Instant.
  • The conversion creates an Instant that represents the same point on the time-line as this Date.
  • Returns an instant representing the same point on the time-line as this Date object.

Date to LocalDate

	LocalDate date = LocalDate.now();
Instant instant = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
	
	Date input = new Date();
Instant instant = input.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
LocalDate date = zdt.toLocalDate();