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