1

Can I somehow parse a datetime that's without offset using OffsetDateTime.parse(....)

Java code:

DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS XXX");
OffsetDateTime date = OffsetDateTime.parse("2017-02-03 12:30:3000", FORMATTER);

I'm getting datetime as a String without offset, but need to parse it into OffsetDateTime, I know I need an offset here, but can I some how alter that String to insert default/dummy offset (maybe +00:00) & parse it using OffsetDateTime. The problem is that object has to be OffsetDateTime.

Eugene S
  • 6,119
  • 7
  • 52
  • 82
new guy
  • 99
  • 4
  • 10
  • You can do this: `OffsetDateTime date = LocalDateTime.parse("2017-02-03 12:30:3000", FORMATTER).atOffset(ZoneOffset.UTC);` – Pieter Dec 20 '17 at 09:00
  • @peter I got it half working by below code, but it produces 2017-02-03T12:30:30+11:00 not 2017-02-03 12:30:30+11:00 I want to get rid of T in between `DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); OffsetDateTime date = OffsetDateTime.of(LocalDateTime.parse("2017-02-03 12:30:30.000", FORMATTER), OffsetDateTime.now().getOffset());` – new guy Dec 20 '17 at 09:04
  • Then you should format the parsed date afterwards using `DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssXXX").format(date)` – Pieter Dec 20 '17 at 10:03
  • 1
    `12:30:3000`?? What is that supposed to mean? – Ole V.V. Dec 20 '17 at 10:46

2 Answers2

2

The simplest solution is to parse your string into a LocalDateTime and then convert:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    OffsetDateTime date = LocalDateTime.parse("2017-02-03 12:30:30", formatter)
            .atOffset(ZoneOffset.UTC);

This gives an OffsetDateTime of 2017-02-03T12:30:30Z, where Z means UTC or offset zero.

You can parse directly into an OffsetDateTime if you want:

    DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
            .appendPattern("yyyy-MM-dd HH:mm:ss")
            .parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
            .toFormatter();
    OffsetDateTime date = OffsetDateTime.parse("2017-02-03 12:30:30", FORMATTER);

Finally, if you are required to use the formatter given in the question, altering the string to fit is of course an option. For example:

    DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS XXX");
    String fixedDateTimeString 
            = "2017-02-03 12:30:3000".replaceFirst("(\\d{2})0*$", "$1.000 +00:00");
    OffsetDateTime date = OffsetDateTime.parse(fixedDateTimeString, FORMATTER);

As you can see, in the last example I have also kept the too many zeroes in the string I am using as a starting point, removing them in the same operation that appends the offset. The result is the same, 2017-02-03T12:30:30Z.

Edit: uuuu or yyyy for year in the format pattern string? Since the year is always in the common era (Anno Domini), either works. yyyy is for year of era and would be right of there was an era designator (like AD or BC, format pattern letter G). uuuu is a signed year, where year 0 means 1 BCE, -1 means 2 BCE, etc. There’s more in this question: uuuu versus yyyy in DateTimeFormatter formatting pattern codes in Java?

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
1

Actually I achieved it by:

DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
OffsetDateTime.of(LocalDateTime.parse("2017-02-03 12:30:30", FORMATTER), ZoneOffset.UTC);
new guy
  • 99
  • 4
  • 10