Java 如何将时间从 GMT 转换为 EST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23533746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:40:36  来源:igfitidea点击:

How to convert a time from GMT to EST

javaandroiddatetimezonejava-time

提问by Ashish Tiwari

I have a Date & time "20140508063630" in (yyyyMMddHHmmss) format. I want to convert this Time into EST time zone. How can I do it? If there is any API for this conversion please let me know. Thanks in advance.

我有 (yyyyMMddHHmmss) 格式的日期和时间“20140508063630”。我想将此时间转换为 EST 时区。我该怎么做?如果此转换有任何 API,请告诉我。提前致谢。

采纳答案by Suren Raju

    try {
          DateFormat gmtFormat = new SimpleDateFormat();
          TimeZone estTime = TimeZone.getTimeZone("EST");
          gmtFormat.setTimeZone(estTime);
          SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
          sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
          System.out.println("EST Time: " + gmtFormat.format(sdf.parse("20140508063630")));
        } catch (ParseException e) {
          e.printStackTrace();
        }

回答by Sameer Kazi

Following code for convert date from one timezone to another timezone with any date format

以下代码用于将日期从一个时区转换为具有任何日期格式的另一个时区

{   String parsedDate = null;

    try {
        SimpleDateFormat dbDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    dbDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date = dbUtcDateFormat.parse("20140508063630");

        SimpleDateFormat userDateFormat = new SimpleDateFormat(yyyyMMddHHmmss);
        userDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
        parsedDate = userDateFormat.format(date);
    } catch (Throwable t) {
        logger.warn("Date Parse Faied : ", t);
    }

    return parsedDate;
}

回答by Ole V.V.

Since you have tagged your question java-time, you do deserve a java.timeanswer.

既然你已经标记了你的问题 java-time,你确实应该得到一个java.time答案。

    String dateAndTime = "20140508063630";
    DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    ZonedDateTime coralHarbourDateTime = LocalDateTime.parse(dateAndTime, parseFormatter)
            .atOffset(ZoneOffset.UTC)
            .atZoneSameInstant(ZoneId.of("America/Coral_Harbour"));
    System.out.println(coralHarbourDateTime);

This prints

这打印

2014-05-08T01:36:30-05:00[America/Coral_Harbour]

There are a number of things to note though:

不过有几点需要注意:

  • You also tagged your question android. java.timeseems to be coming to Android, but as of writing it is not on the majority of Android phones. Don' t despair, though, get the ThreeTenABPand use the classes on Android.
  • The SimpleDateFormatand TimeZoneclasses used in the other answers are long outdated, so if your Android app is doing any work with dates and/or times and/or time zones, I do recommend ThreeTenABP and the modern classes as the programmer friendly and the futureproof way to go.
  • You asked for EST time zone and then gave us a date of May 8, which is in the summer time (DST) part of the year. First, three letter abbreviations are dangerous in being ambiguous and in this case not a full time zone, so avoid them where you can. I solved the problem by using America/Coral_Harbour since I read that this particular place uses EST all year round, no summer time. If this was not what you intended, please provide a different location. If instead I use America/Montreal, I get 2014-05-08T02:36:30-04:00[America/Montreal], for example, with time zone offset -4 instead of -5.
  • 您还标记了您的问题 android。java.time似乎即将进入 Android,但截至撰写本文时,大多数 Android 手机都没有。不过,不要绝望,获取ThreeTenABP并在 Android 上使用这些类。
  • 其他答案中使用的SimpleDateFormatandTimeZone类早已过时,因此如果您的 Android 应用程序正在处理日期和/或时间和/或时区,我确实推荐 ThreeTenABP 和现代类作为程序员友好且面向未来的方式走。
  • 您询问了 EST 时区,然后给了我们 5 月 8 日的日期,这是一年中的夏令时 (DST)。首先,三个字母的缩写在模棱两可的情况下是危险的,在这种情况下不是完整的时区,所以尽可能避免它们。我通过使用 America/Coral_Harbour 解决了这个问题,因为我读到这个特定的地方全年都使用 EST,没有夏季时间。如果这不是您想要的,请提供不同的位置。相反,如果我使用 America/Montreal,我会得到2014-05-08T02:36:30-04:00[America/Montreal],例如,时区偏移 -4 而不是 -5。

Furhter link:How to use ThreeTenABP in Android Project

进一步链接:如何在 Android 项目中使用 ThreeTenABP