如何从给定的字符串转换时区

huangapple go评论132阅读模式
英文:

How to convert timezones from given string

问题

  1. SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy'T'hh:mm:ss'Z'");
  2. SimpleDateFormat format2 = new SimpleDateFormat("M/d/yy h:mm a");
  3. format1.setTimeZone(TimeZone.getTimeZone("UTC"));
  4. Date date = format1.parse("06/17/2008T13:53:23Z");
  5. System.out.println(date);
  6. format2.setTimeZone(TimeZone.getTimeZone("America/New_York")); // Use the correct time zone ID for EDT
  7. System.out.println(format2.format(date));
英文:

I have a given string 06/17/2008T13:53:23Z, I want to convert that to EDT time zone. So my output will be 6/17/08 9:53 AM EDT.

Below is my Java Code, it is giving wrong output:

  1. SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy'T'hh:mm:ss'Z'");
  2. SimpleDateFormat format2 = new SimpleDateFormat("M/d/yy hh:mm a");
  3. format1.setTimeZone(TimeZone.getTimeZone("UTC"));
  4. Date date = format1.parse("06/17/2008T13:53:23Z");
  5. System.out.println(date);
  6. format2.setTimeZone(TimeZone.getTimeZone("EDT"));
  7. System.out.println(format2.format(date));

It gives output as :

  1. Tue Jun 17 19:23:23 IST 2008
  2. 6/17/08 01:53 PM

Here the output time as 01:53 PM instead of 9:53 AM EDT

How to fix this issue?

答案1

得分: 2

你的解析模式使用了 hh(适用于12小时制),而你的日期时间字符串中的时间是24小时制,所以你需要使用 HH。另外,你应避免使用三个字母的时区名称。正如你已经预期的那样,EDT 的时区偏移为 -4 小时,你可以在 SimpleDateFormat 中将其表示为 GMT-4

尽管时区偏移是以数字(小时、分钟或秒)表示的,但时区是以表示时区名称的字符串形式(例如 America/New_York)表示的。时区和时区偏移之间的关系是多对一,即多个时区可以具有相同的时区偏移。

请注意,java.util 日期时间类已经过时且容易出错,同样的情况也适用于它们的格式化API,即 SimpleDateFormat。我建议你完全停止使用它们,并切换到现代日期时间API。在**教程: 日期时间**中可以了解有关现代日期时间API的更多信息。

如果你在进行 Android 项目,并且你的 Android API 版本仍不符合 Java 8,可以查看通过 desugaring 使用的 Java 8+ API以及在 Android 项目中使用 ThreeTenABP 的方法

使用现代日期时间API:

  1. import java.time.ZoneId;
  2. import java.time.ZonedDateTime;
  3. import java.time.format.DateTimeFormatter;
  4. public class Main {
  5. public static void main(String[] args) {
  6. String str = "06/17/2008T13:53:23Z";
  7. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/u'T'H:m:s'z'");
  8. ZonedDateTime zdt = ZonedDateTime.parse(str, formatter);
  9. // 转换为东部时间
  10. ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneId.of("America/New_York"));
  11. // ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneOffset.ofHours(-4));// 或者使用这种方式
  12. // 以默认格式打印,即 ZonedDateTime#toString
  13. System.out.println(zdtET);
  14. // 以自定义格式打印
  15. System.out.println(zdtET.format(DateTimeFormatter.ofPattern("MM/dd/uuuu'T'HH:mm:ss zzzz")));
  16. System.out.println(zdtET.format(DateTimeFormatter.ofPattern("M/d/uu HH:mm:ss a")));
  17. }
  18. }

输出:

  1. 2008-06-17T09:53:23-04:00[America/New_York]
  2. 06/17/2008T09:53:23 Eastern Daylight Time
  3. 6/17/08 09:53:23 am

注意: 如果你使用 ZoneOffset.ofHours(-4)(在上面的代码中已注释),你将无法在输出中得到时区名称(例如 America/New_York),因为正如前面所述,许多时区可以具有相同的时区偏移,并且没有默认的时区用于某个时区偏移。

使用传统API:

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.TimeZone;
  5. public class Main {
  6. public static void main(String[] args) throws ParseException {
  7. String str = "06/17/2008T13:53:23Z";
  8. SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
  9. formatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
  10. Date date = formatter.parse(str);
  11. // 转换为东部时间
  12. SimpleDateFormat sdfOutput = new SimpleDateFormat("M/d/yy hh:mm a");
  13. sdfOutput.setTimeZone(TimeZone.getTimeZone("America/New_York"));
  14. // sdfOutput.setTimeZone(TimeZone.getTimeZone("GMT-4"));// 或者使用这种方式
  15. System.out.println(sdfOutput.format(date));
  16. }
  17. }

输出:

  1. 6/17/08 09:53 am
英文:

Your parsing pattern uses hh (which is for 12-hour format) whereas the time in your date-time string is in 24-hour format for which you need to use HH. Secondly, you should avoid using the three-letter time-zone name. As you have already expected, EDT has a Zone-Offset of -4 hours and you can use this as GMT-4 with SimpleDateFormat.

While a Zone-Offset is expressed in terms of numbers (hours, minutes or seconds), a timezone is expressed as a string representing the name (e.g. America/New_York) of the timezone. The relation between timezone and Zone-Offset is many-to-one i.e. many timezones can have the same Zone-Offset.

Note that java.util date-time classes are outdated and error-prone and so is their formatting API, SimpleDateFormat. I suggest you should stop using them completely and switch to the modern date-time API. Learn more about the modern date-time API at Trail: Date Time.

If you are doing it for your Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Using the modern date-time API:

  1. import java.time.ZoneId;
  2. import java.time.ZonedDateTime;
  3. import java.time.format.DateTimeFormatter;
  4. public class Main {
  5. public static void main(String[] args) {
  6. String str = "06/17/2008T13:53:23Z";
  7. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/u'T'H:m:sz");
  8. ZonedDateTime zdt = ZonedDateTime.parse(str, formatter);
  9. // Convert to Eastern Time
  10. ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneId.of("America/New_York"));
  11. // ZonedDateTime zdtET = zdt.withZoneSameInstant(ZoneOffset.ofHours(-4));// Or this
  12. // Print in default format i.e. ZonedDateTime#toString
  13. System.out.println(zdtET);
  14. // Print in custom formats
  15. System.out.println(zdtET.format(DateTimeFormatter.ofPattern("MM/dd/uuuu'T'HH:mm:ss zzzz")));
  16. System.out.println(zdtET.format(DateTimeFormatter.ofPattern("M/d/uu hh:mm:ss a")));
  17. }
  18. }

Output:

  1. 2008-06-17T09:53:23-04:00[America/New_York]
  2. 06/17/2008T09:53:23 Eastern Daylight Time
  3. 6/17/08 09:53:23 am

Note: If you use ZoneOffset.ofHours(-4) [commented in the code above], you can not get the name of timezone (e.g. America/New_York) in the output because, as explained earlier, many timezones can have the same Zone-Offset and there is no default timezone for a Zone-Offset.

Using the legacy API:

  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.TimeZone;
  5. public class Main {
  6. public static void main(String[] args) throws ParseException {
  7. String str = "06/17/2008T13:53:23Z";
  8. SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy'T'HH:mm:ss");
  9. formatter.setTimeZone(TimeZone.getTimeZone("GMT+0"));
  10. Date date = formatter.parse(str);
  11. // Convert to Eastern Time
  12. SimpleDateFormat sdfOutput = new SimpleDateFormat("M/d/yy hh:mm a");
  13. sdfOutput.setTimeZone(TimeZone.getTimeZone("America/New_York"));
  14. // sdfOutput.setTimeZone(TimeZone.getTimeZone("GMT-4"));// Or this
  15. System.out.println(sdfOutput.format(date));
  16. }
  17. }

Output:

  1. 6/17/08 09:53 am

huangapple
  • 本文由 发表于 2020年10月23日 01:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64487407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定