【问题标题】:Converting String hours and minutes in to UTC in java在java中将字符串小时和分钟转换为UTC
【发布时间】:2015-10-13 08:58:38
【问题描述】:

我在字符串中有小时和分钟,我想将其转换为 UTC 时区 下面是我的代码,但我的小时和分钟有误,请帮助我....谢谢

    Calendar time = Calendar.getInstance(TimeZone.getTimeZone(Utils.merchantTimeZone));
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");
    System.out.println("Raw time====" + rawTime);
    time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(rawTime.substring(0, 2)));
    time.set(Calendar.MINUTE, Integer.parseInt(rawTime.substring(2,4)));

    sdf.format(time);
    rawTime = time.get(Calendar.HOUR) +""+ time.get(Calendar.MINUTE);

【问题讨论】:

  • 你想要什么,你得到什么?
  • 提供错误日志和输入值
  • 查看joda-time android库,它有很多转换日期和时间的现成函数....github.com/dlew/joda-time-android
  • 当我将 0830(Asia/Calcutta) 传递给 rawTime 时,我得到了 1400,这不是正确的 UTC 时间。我想要正确的 UTC 时间
  • 是否可以将 23:00(亚洲/加尔各答)转换为 UTC 小时

标签: android


【解决方案1】:

日历时间 = Calendar.getInstance(TimeZone.getTimeZone(Utils.merchantTimeZone));

TimeZone 已替换为 ZoneId

ZoneId z = ZoneId.of( "America/Edmonton" ) ; // Or `Africa/Tunis`, `Europe/Paris`, etc.

Calendar 类已替换为 ZonedDateTime。拨打now捕捉当前时刻。

ZonedDateTime zdt = ZonedDateTime.now( z ) ;

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");

最好自动本地化。要进行本地化,请指定:

  • FormatStyle 确定字符串的长度或缩写。
  • Locale 确定:
    • 人类语言用于翻译日名、月名等。
    • 文化规范决定缩写、大写、标点、分隔符等问题。

代码:

FormatStyle style = FormatStyle.LONG ; 
Locale locale = new Locale( "en" , "IN" ) ;  // English in India.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( style ).withLocale( locale ) ;
String output = zdt.format( f ) ;

2019 年 7 月 26 日晚上 7:28:36 GMT-06:00

如果您希望对格式模式进行硬编码,请在 StackOverflow 中搜索 DateTimeFormatter.ofPattern。这已经讲过很多次了。

rawTime = time.get(Calendar.HOUR) +""+ time.get(Calendar.MINUTE);

如果您只想要没有日期和时区的时间部分,请提取LocalTime

LocalTime lt = zdt.toLocalTime() ;

如果您只需要小时和分钟而不需要秒和小数秒,truncate

LocalTime lt = zdt.toLocalTime().truncatedTo( ChronoUnit.MINUTES ) ;

我想把它转换成 UTC 时区

您的这部分问题不清楚。如果您想从分区时刻调整以在 UTC 中看到同一时刻,只需转换为 OffsetDateTime 对象并使用 ZoneOffset.UTC 常量调整为 UTC。

OffsetDateTime odt = zdt.toOffsetDateTime() ;
OffsetDateTime odtUtc = odt.withOffsetSameInstant( ZoneOffset.UTC ) ;

带有时区的ZonedDateTime 和带有UTC 偏移量的OffsetDateTime 之间有什么区别?偏移量只是几个小时-分钟-秒,仅此而已。时区更多。时区是特定地区的人们使用的偏移量的过去、现在和未来变化的历史。

当我将 0830(亚洲/加尔各答)传递给 rawTime 时,我得到了 1400,这不是正确的 UTC 时间

显然您想指定日期的时间。

首先获取今天的日期,例如。

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
LocalDate ld = LocalDate.now( z ) ;           // Current date as seen in India right now.

指定您的时间。

LocalTime lt = LocalTime.of( 8 , 30 ) ;

组合所有三个部分以获得ZonedDateTime。如果该时间在该区域的该日期无效,ZonedDateTime 将进行调整。

ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;

zdt.toString(): 2019-07-27T08:30+05:30[亚洲/加尔各答]

要在 UTC 中查看同一时刻,请提取 Instant。根据定义,Instant 类始终采用 UTC。

Instant instant = zdt.toInstant() ;

instant.toString(): 2019-07-27T03:00:00Z

注意一天中的时间。在这一天,印度比世界标准时间早五个半小时。因此,UTC 时间凌晨 3 点的时间减少了 5.5 小时。

是否可以将 23:00(亚洲/加尔各答)转换为 UTC 小时

是的,类似于上面的代码。在这里,我们也可以调用ZonedDateTime::with

ZonedDateTime
.now( 
    ZoneId.of( "Asia/Kolkata" ) 
)
.with( 
    LocalTime.of( 23 , 0 ) 
)
.toInstant() 
.toString()

2019-07-27T17:30:00Z

同样,在这一天,印度比 UTC 早五个半小时。因此,从晚上 11 点开始往回拨 5.5 小时表示下午 5:30。


此处看到的类内置于 Java 8 及更高版本以及 Android 26 及更高版本。

  • 大多数 java.time 功能在 ThreeTen-Backport 项目中向后移植到 Java 6 和 Java 7。
  • ThreeTenABP 中进一步适用于早期的Android (How to use ThreeTenABP…

【讨论】:

    【解决方案2】:

    我建议您在代码中添加以下行:

    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    

    作为资源,我正在寻找Affe给出的以下线程答案:点击here

    编辑班级时区documentation。另外,这里是 Jenkov here

    的一个非常好的教程

    编辑 2:读者注意:TimeZone 类现在是旧版,被ZoneIdZoneOffset 取代。感谢Basil Bourque

    【讨论】:

    • 读者注意:TimeZone 类现在是遗留的,被ZoneIdZoneOffset 取代。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2020-01-28
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多