Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

java - Receive Date including timezone offset as String and convert to a different format using that timezone

I am receiving from my clients a String like these in this format

yyyy-MM-dd'T'HH:mm:ss.SSSXXX:

2021-02-04T15:31:22.265-05:00
2021-02-04T15:31:22.265+00:00
2021-02-04T15:31:22.265-06:00

and i need to convert them to a new format:

MMMM dd, yyyy - HH:mm a z (where z is the three character abbreviation for the timezone. Ex. EST)

February 02, 2021 - 15:31 PM EST
February 02, 2021 - 15:31 PM UTC
February 02, 2021 - 15:31 PM CST

Normally i would do something like:

SimpleDateFormat inDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
        SimpleDateFormat outDateFormat = new SimpleDateFormat("MMMM dd, yyyy - HH:mm a z")
        Date date = inDateFormat.parse("2021-02-04T15:31:22.265-05:00");
        String convertedTime = outDateFormat.format(date) // February 02, 2021 - 15:31 PM EST

But i then realized that it was only printing EST because thats where I am. But if a user were to be in another timezone and use this it would become their time but EST would still be on the end. Such as if they send 2021-02-04T15:31:22.265+00:00 then it would return February 02, 2021 - 15:31 PM EST which is 5 hours in the past for them.

So how can i take a string that includes the offset and simply turn it into a string in a different format from the same offset? I would expect the outcome of someone from UTC sending 2021-02-04T15:31:22.265+00:00 to be February 02, 2021 - 15:31 PM UTC

question from:https://stackoverflow.com/questions/66054361/receive-date-including-timezone-offset-as-string-and-convert-to-a-different-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can derive a timezone offset from a timezone ID but not the other way round i.e. you can not derive a timezone ID using a timezone offset; because the same timezone offset can be applicable for many timezone IDs e.g.

The following code is absolutely valid

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]");
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("MMMM dd, uuuu - HH:mm a XXX");
        System.out.println(zdt.format(dtfOutput));
    }
}

Output:

December 03, 2007 - 10:15 am +01:00

whereas the following code will throw exception:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2021-02-04T15:31:22.265-05:00");
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("MMMM dd, uuuu - HH:mm a z");
        System.out.println(odt.format(dtfOutput));
    }
}

Output:

Exception in thread "main" java.time.DateTimeException: Unable to extract ZoneId from temporal 2021-02-04T15:31:22.265-05:00
    at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:289)
    at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.format(DateTimeFormatterBuilder.java:4083)
    at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2343)
    at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1848)
    at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1822)
    at java.base/java.time.OffsetDateTime.format(OffsetDateTime.java:1681)
    at Main.main(Main.java:8)

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

Note that the date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...