In this article, we will learn how to convert LocalDate to OffsetDateTime using atTime() method of LocalDate provided in Java 1.8 version
For OffsetDateTime to LocalDate conversion, read Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from OffsetDateTime ?
Convert LocalDate to OffsetDateTime :
- LocalDate has a method atTime() which takes OffsetTime as argument and returns OffsetDateTime
- atTime() – Returns a Offset date-time from invoking LocalDate with an offset time
- Using this method, it is very easy to convert LocalDate to OffsetDateTime
- After conversion, OffsetDateTime have Date part same as that of LocalDate and Time part will consist of hour, minute, second, nano and offset information, for example 2022–08–01T15:50:58.797738+05:30
- Lets see an example for conversion of LocalDate to OffsetDateTime in the below illustration
ConvertLocalDateToOffsetDateTime.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
public class ConvertLocalDateToOffsetDateTime {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("Current System Date is :- \n" + localDate);
// 2. get current Offset Time
OffsetTime offsetTime = OffsetTime.now();
System.out.println("\nSystem Offset Time is :- \n" + offsetTime);
// 3. convert LocalDate to OffsetDateTime using atTime(OffsetTime time)
OffsetDateTime offsetDateTime = localDate.atTime(offsetTime);
System.out.print("\nConversion of LocalDate to OffsetDateTime is :- \n"
+ offsetDateTime);
}
}
Output:
Current System Date is :-
2022-08-01
System Offset Time is :-
15:59:13.511289300+05:30
Conversion of LocalDate to OffsetDateTime is :-
2022-08-01T15:59:13.511289300+05:30
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.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 !!