AdBlock Detected

It looks like you're using an ad-blocker!

Our team work realy hard to produce quality content on this website and we noticed you have ad-blocking enabled. Advertisements and advertising enable us to continue working and provide high-quality content.

From date to LocalDate and LocalDateTime

In this article, we will discuss the different approaches to convert Date to LocalDate or LocalDateTime in Java and vice versa.

With the introduction of the Java.time.* package in Java 8, the handling of dates and their usage has improved, and sometimes we may encounter dates in formats older than Java 8 that need to be converted.

Converting to Date from LocalDate in Java

To convert a Date object to LocalDate in Java, we can use the methods toInstant(), ofEpochMilli(), toLocalDate(), and ofInstant().

Let’s take a look at three different approaches:

  • Invoking toInstant(): The toInstant() method in Java was introduced in Java 8 in the java.util.Date package. It is necessary to inform the time zone through ZoneId.
  • Using ofEpochMilli(): This method belongs to Instant. It returns an instance of Instant in milliseconds. It is necessary to inform the time zone through ZoneId.
  • Using toLocalDate(): Using the toLocalDate method of java.sql.Date.
  • Using ofInstant(): A method introduced in Java 9 that facilitates conversion.

Let’s see how to convert an object to LocalDate using toInstant():

public LocalDate fromDateToLocalDate(Date date) {
    return date.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDate();
}

Next, the following example converts a Date object to LocalDate using Instant.ofEpochMilli:

public LocalDate fromDateToLocalDate(Date date) {
    return Instant.ofEpochMilli(date.getTime())
      .atZone(ZoneId.systemDefault())
      .toLocalDate();
}

Let’s take a look at the java.sql.Date class. The java.sql.Date method allows conversion to LocalDate using the toLocalDate method found in the class.

public LocalDate fromDateToLocalDate(Date date) {
    return new java.sql.Date(date.getTime()).toLocalDate();
}

Finally, if we use Java 9, we can use ofInstant of LocalDate to convert to LocalDate and LocalDateTime:

public LocalDate fromDateToLocalDate(Date date) {
    return LocalDate.ofInstant(
      date.toInstant(), ZoneId.systemDefault());
}

Next, we will convert to LocalDateTime where the approach is similar.

Converting from Date to LocalDate in Java

As we did in the previous step, we will convert to LocalDateTime from a Date object. We will use the same three approaches as for converting from Date to LocalDate.

Using toInstant():

public LocalDateTime fromDateToLocalDateTime(Date date) {
    return date.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDateTime();
}

Using Instant():

public LocalDateTime fromDateToLocalDateTime(Date date) {
    return Instant.ofEpochMilli(date.getTime())
      .atZone(ZoneId.systemDefault())
      .toLocalDateTime();
}

Converting to LocalDateTime from java.sql.Date:

public LocalDateTime fromDateToLocalDateTime(Date date) {
    return new java.sql.Date(date.getTime()).toLocalDateTime();
}

Using Java 9’s ofInstant to convert to LocalDateTime:

public LocalDateTime fromDateToLocalDateTime(Date date) {
    return LocalDateTime.ofInstant(
      date.toInstant(), ZoneId.systemDefault());

Converting from LocalDate to Date

Let’s see how we can convert LocalDate objects to java.util.Date using Date.from():

public Date toDate(LocalDate localDate) {
    return Date.from(localDate.atStartOfDay()
      .atZone(ZoneId.systemDefault())
      .toInstant());
}

Note that we use the default system ZoneId to set the zone.

Converting to Date from LocalDateTime in Java

With the same approach as for converting from LocalDate to Date, we will convert from LocalDateTime to Date:

public Date toDate(LocalDateTime localDateTime) {
    return java.util.Date.from(localDateTime.atZone(ZoneId.systemDefault())
      .toInstant());
}

Conclusion

When working with different date types in Java, it is very important to know how to convert them in order to homogenize the dates and display them as required in our application. In this article, we have been able to see the different approaches to convert Date to LocalDate or LocalDateTime in Java and vice versa.

Other articles that may interest you

Get day of week in Java

If you need more information, you can leave us a comment or send an email to refactorizando.web@gmail.com You can also contact us through our social media channels on Facebook or twitter and we will be happy to assist you!!

Leave a Reply

Your email address will not be published. Required fields are marked *