0

I have string date format like below

2023-01-11 18:27:59UTC-06:00

need to convert to like 2023-01-12T00:27:59.000Z (in UTC zone)

I tried the below. I am getting exception Exception in thread "main" java.time.format.DateTimeParseException: Text '2023-01-09 23:56:59UTC-05:30' could not be parsed at index 10. The exception is coming from this line:

LocalDateTime labelTime = LocalDateTime.parse(dateUTC, DateTimeFormatter.ofPattern(INPUT_FORMAT));

My short code example:

String dateUTC="2023-01-09 23:56:59UTC-05:30";
final String INPUT_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
final String OUTPUT_FORMAT = "yyyy-MM-dd'T'HH:mm:ssXXX";
final DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern(OUTPUT_FORMAT);
LocalDateTime labelTime = LocalDateTime.parse(dateUTC, DateTimeFormatter.ofPattern(INPUT_FORMAT));
ZoneId utcZoneId = ZoneId.of("UTC");
ZonedDateTime zdt = labelTime.atZone(utcZoneId);
System.out.println("OUT PUT Format"+dtf2.format(zdt));
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Welcome to Stack Overflow. Sorry, but this site is not a discussion forum or programming tutorial site. Please take the [tour], visit the [help] and read [Ask] to learn how to use this site effectively. Also read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236/18157) and [Open letter to students with homework problems](http://meta.softwareengineering.stackexchange.com/q/6166/18157) – Jim Garrison Jan 12 '23 at 00:33
  • 2
    Also, `2023-01-12T00:27:59.000Z` is UTC, not local. A `LocalDateTime` does not have a timezone. It's not clear what you are asking, and you need to show us what you have tried and what didn't work. – Jim Garrison Jan 12 '23 at 00:35
  • Just remember, "formatting" is not the same as "representation" (ie `ZonedDateTime` and `LocalDateTime`) - these class "represent" a time period since some kind of anchor point or epoch (ie the Unix Epoch). "Formatting" is the process of taking a "representation" and generating a `String`, typically for presentation to a human. Sooo, is your question about "formatting" a `ZonedDateTime`/`LocalDateTime` or converting a `ZonedDateTime` to `LocalDateTime` – MadProgrammer Jan 12 '23 at 00:47
  • `2023-01-09 23:56:59UTC-05:30` is not the same as `yyyy-MM-dd'T'HH:mm:ss`, there's no time zone information in the formatter – MadProgrammer Jan 12 '23 at 00:48
  • how to convert this 2023-01-09 23:56:59UTC-05:30 to UTC local date time – user3763183 Jan 12 '23 at 00:50
  • 1
    `INPUT_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"` needs to be `INPUT_FORMAT = "yyyy-MM-dd'T'HH:mm:ssVV"` in order to parse something like `yyyy-MM-dd HH:mm:ssVV`; You can then parse it using something like `ZonedDateTime offsetDateTime = ZonedDateTime.parse(dateUTC, DateTimeFormatter.ofPattern(INPUT_FORMAT, Locale.US));`; which will allow you to convert this to UTC, using something like `ZonedDateTime utcDateTime = offsetDateTime.withZoneSameInstant(ZoneId.of("UTC"));`. Now if you "need" an instance of `LocalDateTime`, you can use `LocalDateTime localUTCDateTime = utcDateTime.toLocalDateTime();` – MadProgrammer Jan 12 '23 at 01:13
  • Remember, `LocalDateTime` has no time zone, so can't use a format like `yyyy-MM-dd'T'HH:mm:ssXXX`, you'd have to use the `ZonedDateTime` instead – MadProgrammer Jan 12 '23 at 01:13
  • 1
    There's quite a few examples about, like [this](https://mkyong.com/java8/java-8-zoneddatetime-examples/#:~:text=1.-,Convert%20LocalDateTime%20to%20ZonedDateTime,predefined%20zone%20id%20or%20offset.) – MadProgrammer Jan 12 '23 at 01:14
  • Use `final String INPUT_FORMAT = "uuuu-MM-dd HH:mm:ss'UTC'xxx";`. Don’t use `LocalDateTime` at all, and also not `ZonedDateTime`. Use `OffsetDateTime`. Parse using `OffsetDateTime labelTime = OffsetDateTime.parse(dateUTC, DateTimeFormatter.ofPattern(INPUT_FORMAT));`. Convert to UTC through `OffsetDateTime odt = labelTime.withOffsetSameInstant(ZoneOffset.UTC);`. For output you may simply use `toString()`, or you may define `final String OUTPUT_FORMAT = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX";`. – Ole V.V. Jan 12 '23 at 06:25
  • 2
    @OleV.V. - Maybe if "someone" could first correct the egregiously bad terminology in the title, please? That's not what parsing means. – Stephen C Jan 12 '23 at 06:36
  • As a native English speaker .... I would call the entire process "conversion" (or maybe "reformatting") myself. – Stephen C Jan 12 '23 at 07:33
  • Looks good to me. – Stephen C Jan 12 '23 at 07:36

1 Answers1

1

Use the pattern, uuuu-MM-dd HH:mm:ss'UTC'XXX to parse the given date-time string into an OffsetDateTime and convert the result into another OffsetDateTime with ZoneOffset.UTC using OffsetDateTime#withOffsetSameInstant.

Demo:

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss'UTC'XXX", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse("2023-01-11 18:27:59UTC-06:00", parser)
                                           .withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println(odt);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
        String formatted = odt.format(formatter);
        System.out.println(formatted);
    }
}

Output:

2023-01-12T00:27:59Z
2023-01-12T00:27:59.000Z

ONLINE DEMO

Note: Here, you can use y instead of u but I prefer u to y.

Learn more about the modern Date-Time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110