【问题标题】:Convert GMT to IST in java?在java中将GMT转换为IST?
【发布时间】:2021-08-05 16:30:02
【问题描述】:

我有一个 GMT 字段,用户在其中输入要转换为 IST 的时间(例如:在小时字段 18、分钟字段 30 中,在会话字段 am/pm 中)。我需要获取这些输入并在 java 中转换为 IST???

【问题讨论】:

  • IST = 印度/爱尔兰/以色列/伊朗标准时间?
  • @harishtps 你错过了 Kutik 评论的重点。 3-4 个字母的缩写不是实际时区,没有标准化,甚至不是唯一的!您的IST 至少在地球上四个不同的地方使用。避免这些伪区域。 real time zone is named 格式为 continent/region,例如 Asia/Kolkata

标签: java


【解决方案1】:

如果您意识到时区仅与格式化为字符串的日期相关 - 秒/毫秒时间戳(其中 java.util.Date 只是一个包装器)总是隐含的 UTC(格林威治标准时间被正确称为)。在这样的时间戳和字符串之间进行转换总是使用时区,两种方式。

所以这就是你需要做的:

    DateFormat utcFormat = new SimpleDateFormat(patternString);
    utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateFormat indianFormat = new SimpleDateFormat(patternString);
    indianFormat .setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    Date timestamp = utcFormat.parse(inputString);
    String output = indianFormat.format(timestamp);

【讨论】:

  • GMT 和 UTC 的定义略有不同,但在实践中总体效果是 de minimis
  • 不应该是indianFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));吗?
【解决方案2】:

tl;博士

OffsetDateTime.of( 
    LocalDate.now( ZoneOffset.UTC ) , 
    LocalTime.of( 18 , 30 ), 
    ZoneOffset.UTC 
).atZoneSameInstant( ZoneId.of( "Asia/Kolkata" ) )

详情

现代方法使用 java.time 类。

LocalDate 的形式获取UTC 中的当前日期,不带时间,不带时区或偏移量。

LocalDate localDate = LocalDate.now( ZoneOffset.UTC );

将每个用户输入的时间指定为LocalTime,不带日期,不带时区或偏移量。

LocalTime localTime = LocalTime.of( 18 , 30 );

将它们与一个为零的offset-from-UTCUTC 本身作为常量ZoneOffset.UTC 放在一起,得到一个OffsetDateTime

OffsetDateTime odt = OffsetDateTime.of( localDate , localTime, ZoneOffset.UTC );

将时区应用为ZoneId 以获得印度时间的ZonedDateTime。或者 IST 是指爱尔兰标准时间吗?伊朗标准时间?

continent/region 的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。

ZoneId z = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = odt.atZoneSameInstant( z );

看到这个code live at IdeOne.com

localDate.toString(): 2017-02-13

localTime.toString(): 18:30

odt.toString(): 2017-02-13T18:30Z

zdt.toString(): 2017-02-14T00:00+05:30[亚洲/加尔各答]


关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

从哪里获得 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

【讨论】:

    【解决方案3】:

    嗯,joda-time 更容易。试试这样的

    DateTime dt = new DateTime(<year>,<month>,<day>, <hour>,<minute>, <second>, <millisecond>);
    DateTime dtIST = dt.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("IST");
    

    请注意这里不推荐使用三个字母的缩写,并且应该像“America/Los_Angeles”一样引用时区是指 PST。我现在没有时间获取 IST 的对应信息,但应该留给读者作为练习!

    更新:正如 Basil Bourque 在 cmets 中所说,Joda-Time 处于维护模式。请改用 java.time。

    【讨论】:

    • 不推荐使用三字母形式的原因是它不明确(例如,我确定 BST 代表英国夏令时间 - 即 GMT+DST - 和巴西标准时间)。 “大陆/城市”形式没有这些问题。
    • 更新:Joda-Time 项目现在位于maintenance mode,并建议迁移到java.time 类。
    【解决方案4】:

    当我添加以下代码时,它对我有用。

    DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
                                utcFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
                                DateFormat indianFormat = new SimpleDateFormat("dd-HH-mm");
                                utcFormat.setTimeZone(TimeZone.getTimeZone("IST"));
                                Date timestamp = utcFormat.parse("2019-04-26-19-00");
                                String istTime = indianFormat.format(timestamp);
    

    【讨论】:

      【解决方案5】:

      如果您正在寻找印度时区,请执行此操作

      “格林威治标准时间+5:30”

      val sdf = SimpleDateFormat("dd-MM-yyyy HH:mm:ss")
      sdf.timeZone = TimeZone.getTimeZone("GMT+5:30")
      

      【讨论】:

        猜你喜欢
        • 2012-12-25
        • 2012-10-24
        • 2018-08-16
        • 1970-01-01
        • 1970-01-01
        • 2020-05-02
        • 2016-12-30
        • 2014-06-07
        • 2012-09-18
        相关资源
        最近更新 更多