-1

I want to convert a String timestamp "20221225182600Z+0100" to date in java without using simpledateformat

I am getting an error

Text '20221225182600Z+0100' could not be parsed at index 2

String dateString = "20221225182600Z+0100";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssZ");
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateString,  dateTimeFormatter.ISO_OFFSET_TIME);
System.out.println(zonedDateTime);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    You're parsing with `DateTimeFormatter.ISO_OFFSET_TIME` instead of your own formatter (the historic mistake in Java is that you can access static fields of a class through instance of that class, which is why this compiles with the local variable `dateTimeFormatter`). Use `ZonedDateTime.parse(dateString, dateTimeFormatter)` – Mark Rotteveel Mar 04 '23 at 11:09
  • 3
    "Z+0100" is a very odd format. "Z" means "no UTC offset" whereas "+0100" means "1 hour UTC offset". Where did this data come from? – Jon Skeet Mar 04 '23 at 11:11
  • Hi Mark i tried like you said but not working – VIMALJITH K Mar 04 '23 at 11:15
  • Hi jon thanks for the reply. i got it from some scanned data – VIMALJITH K Mar 04 '23 at 11:19
  • Use `DateTimeFormatter.ofPattern("uuuuMMddHHmmss'Z'Z")` and then leave out `.ISO_OFFSET_TIME` as @MarkRotteveel said. Also prefer to parse into an `OffsetDateTime` over a `ZonedDateTime`. – Ole V.V. Mar 04 '23 at 12:47
  • I assume the error changed after addressing the thing I pointed out in the comments (at minimum the "at index" value would change. The new error is because your format doesn't match your actual input. – Mark Rotteveel Mar 04 '23 at 12:51
  • Why index 2? `ISO_OFFSET_TIME` expects a time of day with offset from UTC, for example `20:34:45+01:00` (possibly not a useful concept). So your parsing successfully parsed `20` as an hour of day and then threw the exception because there was no `:` (colon) after `20` — at index 2. – Ole V.V. Mar 04 '23 at 12:53
  • 1
    String dateString = "20221225182600Z+0100"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMddHHmmss'Z'Z"); OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateString, dtf); LocalDateTime localDateTime = offsetDateTime.toLocalDateTime(); – VIMALJITH K Mar 04 '23 at 16:21
  • Thank you Ole V.V and others for the suggestions .The above one worked for me – VIMALJITH K Mar 04 '23 at 16:22
  • Good to read. You may post it as an answer to your own question. – Ole V.V. Mar 04 '23 at 17:35
  • You should either not convert to `LocalDateTime` (all it does is throwing information away, no reason to do that) or before you do, convert to the intended time zone so that you result date and time agree with that zone. – Ole V.V. Mar 04 '23 at 17:38

1 Answers1

2

Cannot parse nonsense

I want to convert a String timestamp "20221225182600Z+0100" to date

You cannot.

Your input string apparently is a date, 2022-12-25, with time-of-day, 18:26. Plus two offsets from UTC.

  • Z near the end means an offset of zero hours-minutes-seconds from the temporal meridian of UTC.
  • The +0100 is an abbreviation of +01:00 which means one hour ahead of UTC.

Including two offsets with a date-time makes no sense. You cannot parse nonsense.

Your situation is like asking to parse an amount of money in two currencies. “42 AUD USD” as an input is nonsense.

Your input of 20221225182600Z+0100 is likely an error. I imagine one of those two offsets was intended while the other was an accident.

By the way, I suggest always including the COLON in your offsets. While omission is officially permitted by the ISO 8601 standard, I have seen multiple protocols and libraries where the : between hours and minutes is expected.

I also suggest educating the publisher of your data about the benefits of using only standard ISO 8601 formats for exchanging date-time values textually. See Wikipedia.

The standard format for your input would be:

  • 2022-12-25T18:26:00Z
  • 2022-12-25T18:26:00+01:00

Those two inputs represent two moments, two different points on the timeline, an hour apart. The first moment arrived an hour behind the second moment.

Parsing standard strings requires no formatting patterns. The java.time classes use ISO 8601 formats by default when parsing/generating text.

  • Instant.parse( "2022-12-25T18:26:00Z" )
  • OffsetDateTime.parse( "2022-12-25T18:26:00+01:00" )
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154