In this article, we will learn how to extract/get LocalDate & LocalTime from LocalDateTime using toLocalDate() and toLocalTime() methods respectively provided in Java 1.8 version
For LocalDate/LocalTime to LocalDateTime conversion, read
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
Get LocalDate/LocalTime from LocalDateTime :
- LocalDateTime has below methods –
- toLocalDate() – gets the LocalDate part from the invoking LocalDateTime
- toLocalTime() – gets the LocalTime part from the invoking LocalDateTime
- Lets see an example for getting LocalDate & LocalTime from LocalDateTime in the below illustration
GetLocalDateAndLocalTimeFromLocalDateTime.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class GetLocalDateAndLocalTimeFromLocalDateTime {
public static void main(String[] args) {
// 1. get current System Date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current System Date/time is :- \n" + localDateTime);
// 2. get LocalDate from LocalDateTime
LocalDate localDate = localDateTime.toLocalDate();
System.out.println("\nLocalDate is :- \n" + localDate);
// 3. get LocalTime from LocalDateTime
LocalTime localTime = localDateTime.toLocalTime();
System.out.print("\nLocalTime is :- \n" + localTime);
}
}
Output:
Current System Date/time is :-
2022-08-09T12:59:30.585626900
LocalDate is :-
2022-08-09
LocalTime is :-
12:59:30.585626900
Related Articles:
- Java 8 – LocalDateTime with method details and examples
- Java 8 – How to get Date and Time fields from LocalDateTime ?
- Java 8 – How to form LocalDateTime passing Date and Time fields ?
- Java 8 – How to form LocalDateTime passing LocalDate and LocalTime ?
- Java 8 – How to form LocalDateTime passing Instant and ZoneId ?
- Java 8 – How to form LocalDateTime passing Second/Nano and ZoneOffset ?
- Java 8 – How to parse LocalDateTime in String form ?
- Java 8 – How to convert String to LocalDateTime ?
- Java 8 – How to convert LocalDateTime to String ?
- Java 8 – How to convert LocalDateTime in different formats ?
- Java 8 – How to convert LocalDateTime in different Format Style ?
- Java 8 – How to convert LocalDateTime to ZonedDateTime ?
- Java 8 – How to convert LocalDateTime to an OffsetDateTime ?
- Java 8 – How to convert LocalDateTime to an Instant ?
- Java 8 – How to extract LocalDateTime and LocalTime from LocalDateTime ?
- Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDateTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime in different ways ?
- Java 8 – How to add Date and Time fields to LocalDateTime ?
- Java 8 – How to subtract Date and Time fields from LocalDateTime ?
- Java 8 – How to alter Date and Time fields of LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?
- Java 8 – How to compare two LocalDateTime instances ?
- Java 8 – How to find difference between two LocalDateTime using Period & Duration ?
- Java 8 – What are all the Temporal Fields supported by LocalDateTime ?
- Java 8 – What are all the Temporal Units supported by LocalDateTime ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!