In this article, we will learn how to extract/get LocalDate & LocalTime & LocalDateTime from ZonedDateTime using toLocalDate() & toLocalTime() & toLocalDateTime() methods respectively provided in Java 1.8 version
For LocalDate/LocalTime/LocalDateTime to ZonedDateTime conversion, read
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalDateTime to ZonedDateTime ?
Get LocalDate, LocalTime & LocalDateTime from ZonedDateTime :
- ZonedDateTime has below methods –
- toLocalDate() – gets the LocalDate part from the invoking ZonedDateTime
- toLocalTime() – gets the LocalTime part from the invoking ZonedDateTime
- toLocalDateTime() – gets the LocalDateTime part from the invoking ZonedDateTime
- Lets see an example for getting LocalDate & LocalTime & LocalDateTime from ZonedDateTime in the below illustration
GetLocalDateAndLocalTimeFromZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
public class GetLocalDateAndLocalTimeFromZonedDateTime {
public static void main(String[] args) {
// 1. get Zoned Date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time is :- \n"
+ zonedDateTime);
// 2. get Zone
System.out.println("\nZone is :- \n"
+ zonedDateTime.getZone());
// 3. convert ZonedDateTime to LocalDate using toLocalDate()
LocalDate localDate = zonedDateTime.toLocalDate();
System.out.println("\nConversion of ZonedDateTime to LocalDate is :- \n"
+ localDate);
// 4. convert ZonedDateTime to LocalTime using toLocalTime()
LocalTime localTime = zonedDateTime.toLocalTime();
System.out.println("\nConversion of ZonedDateTime to LocalTime is :- \n"
+ localTime);
// 5. convert ZonedDateTime to LocalDateTime using toLocalDateTime()
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
System.out.print("\nConversion of ZonedDateTime to LocalDateTime is :- \n"
+ localDateTime);
}
}
Output:
Zoned Date/time is :-
2022-08-13T15:35:24.309207800+05:30[Asia/Calcutta]
Zone is :-
Asia/Calcutta
Conversion of ZonedDateTime to LocalDate is :-
2022-08-13
Conversion of ZonedDateTime to LocalTime is :-
15:35:24.309207800
Conversion of ZonedDateTime to LocalDateTime is :-
2022-08-13T15:35:24.309207800
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- 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/ZoneId.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 !!