Convert string to OffsetDateTime in Java

We Are Going To Discuss About Convert string to OffsetDateTime in Java. So lets Start this Java Article.

Convert string to OffsetDateTime in Java

  1. Convert string to OffsetDateTime in Java

    Earlier I was using joda datetime (look at below method) to handle date and datetime both but I wanted to use Java8 libraries instead of the external libraries.

  2. Convert string to OffsetDateTime in Java

    Earlier I was using joda datetime (look at below method) to handle date and datetime both but I wanted to use Java8 libraries instead of the external libraries.

Solution 1

OffsetDateTime represents a date-time with an offset , for eg.

2007-12-03T10:15:30+01:00

The text you are trying to parse does not conform to the requirements of OffsetDateTime.
See https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html

The string being parsed neither contains the ZoneOffset nor time. From the string and the pattern of the formatter, it looks like you just need a LocalDate. So, you could use :

LocalDate.parse("20150101", DateTimeFormatter.ofPattern("yyyyMMdd"));
Advertisements

Original Author Pallavi Sonal Of This Content

Solution 2

Thanks everyone for your reply.
Earlier I was using joda datetime (look at below method) to handle date and datetime both but I wanted to use Java8 libraries instead of the external libraries.

static public DateTime convertStringInDateFormat(String date, String dateFormat){
    DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat);
return formatter.parseDateTime(date);
}

I was expecting same with OffsetDateTime but got to know we can use ZonedDateTime or OffsetDateTime if we want to work with a date/time in a certain time zone.
As I am working on Period and Duration for which LocalDate can help.

String to DateTime:

LocalDate date =
LocalDate.parse("20150101", DateTimeFormatter.ofPattern("yyyyMMdd"));

LocalDate to desired string format:

String dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
date.atStartOfDay().format(DateTimeFormatter.ofPattern(dateFormat));
Advertisements

Original Author Pallavi Sonal Of This Content

Solution 3

Use a LocalDate instead of offsetDatetime for your case as you want to parse only date (no time/offset). The usage of offsetDatetime is very well discussed here

Advertisements

Original Author Yuvi Of This Content

Solution 4

As Pallavi said correctly OffsetDateTime makes only in the context of a offset.
Hence to go from a date string to an OffsetDateTime, you need a timezone!

Here is the recipe. Find yourself a Timezone, UTC or other.

ZoneId zoneId = ZoneId.of("UTC");   // Or another geographic: Europe/Paris
ZoneId defaultZone = ZoneId.systemDefault();

Make the LocalDateTime, works the same with LocalDate.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");    
LocalDateTime dateTime = LocalDateTime.parse("2022-01-28T14:29:10.212", formatter);

An offset makes only sense for a timezone and a time.
For instance, Eastern Time hovers between GMT-4 and GMT-5 depending on the time of year.

ZoneOffset offset = zoneId.getRules().getOffset(dateTime);

Finally you can make your OffsetDateTime from both the time and the offset:

OffsetDateTime offsetDateTime = OffsetDateTime.of(dateTime, offset);

Hope this helps anyone.

Advertisements

Original Author Patrice Gagnon Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment