Convert LocalDateTime to LocalDate in Java
In this tutorial, we will see how to Convert LocalDateTime to LocalDate in Java. In the earlier tutorial, we have covered how we can Convert Java Date to LocalDateTime in Java.
Here is the brief intro about what is LocalDateTime and LocalDate class in java.
LocalDateTime
- LocalDateTime is an instance of date-time without a time-zone in the ISO-8601 calendar system, such as 2017-12-03T10:15:30.
- LocalDateTime is an immutable date-time object that represents a date-time, often viewed as a year-month-day-hour-minute-second.
LocalDate
- LocalDate class is immutable and thread-safe.
- This is a value-based class thus use of equals method is recommended for comparisons.
- LocalDate provides date format as YYYY-MM-dd such as 2017-12-03.
- The LocalDate class does not have time or timezone data, So LocalDate is preferable for Birthday, National Holiday representation.
Convert LocalDateTime to LocalDate
To Convert LocalDateTime to LocalDate in Java we will use LocalDateTime.toLocalDate() method.
Method Declaration
public LocalDate toLocalDate()
This method retrieves the LocalDate part of this date-time. This returns a LocalDate with the same year, month and day as this date-time.
Specified by:
The toLocalDate method declared in interface ChronoLocalDateTime<LocalDate> and implemented by LocalDateTime class.
Returns:
The method will return the date part of this date-time which is not null.
Example:
import java.time.LocalDate;
import java.time.LocalDateTime;
public class LocalDateTimeToLocalDate {
public static void main(String[] args) {
// Creating LocalDateTime instance
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("CurrentDateTime : " + currentDateTime);
// Creating LocalDate instance
LocalDate currentDate = currentDateTime.toLocalDate();
// Prints LocalDateTime to LocalDate value
System.out.println("LocalDateTime to LocalDate : " + currentDate);
}
}Output:
CurrentDateTime : 2020-06-29T17:05:58.854
LocalDateTime to LocalDate : 2020-06-29
Conclusion:
In this tutorial, we have seen how we can convert LocalDateTime to LocalDate in Java using LocalDateTime.toLocalDate() method.

Comments
Post a Comment
If you have any doubts, Please let me know.