Search code examples
javadateiso8601

Convert ISO8601 date time format to another ISO8601 date time format


I am struck with a problem.I need to convert a date in ISO8601 date time formatted date to another ISO8601 date format.For example i need to convert 2019-01-08T09:51:34+0000 to another similar date time format based on the locale.I tried doing this:

OffsetDateTime offsetDateTime = OffsetDateTime.parse(date);
Instant instant = offsetDateTime.toInstant(); 

But from here I can convert to date object, but question is how can I convert to another ISO8601 date time format based on the locale I receive. You can assume that I have the format stored in db which I can retrieve using the locale mentioned above and there is no problem in that part.

I am struggling hard for past 3 weeks with this thing. Please point me out what I am missing or what I need to change in my approach.


Solution

  • Locale irrelevant to ISO 8601

    A Locale is used for localization while generating text describing a date-time value.

    ISO 8601 is a standard defining for defining textual formats representing date-time values that are not localized. Avoiding localization is the entire point of ISO 8601, to provide one single format for use worldwide. This provides for easy reliable exchange of data.

    For example, a date is represented in YYYY-MM-DD format in ISO 8601, everywhere. The 23rd of January is 2019-01-23 always, whether you are in Montréal Québec, Berlin Germany, Pune India, or Tokyo Japan.

    So your question asking about a Locale with ISO 8601 makes no sense.

    convert to another ISO8601 date time format based on the locale I receive

    Your phrase is a contradiction in terms. There is no Locale used in a ISO 8601 string.

    I suggest you take some time to study the Wikipedia page for ISO 8601.