将 UTC 时间 HH:mm:ss 转换为设备时间 HH:mm:ss 并在可用时添加日保存时间

库什克查图维杜拉

我有一个格式 (HH: mm: ss) 的 UTC 字符串,我需要将字符串值转换为相同格式的设备时间 (HH: mm: ss) 并且如果可用还添加日间节约时间我曾尝试转换通过此代码将 UTC 字符串转换为设备时间,但由于(节省时间),我延迟了 1 小时。

String utcTimeString = "12:15:00"
SimpleDateFormat formatter = new SimpleDateFormat("HH: mm: ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = formatter.parse(utcTimeString);

SimpleDateFormat dateFormatter = new SimpleDateFormat("HH: mm: ss"); 
dateFormatter.setTimeZone(TimeZone.getDefault());
utcDateString = dateFormatter.format(value);

我的时区是 GMT-4

预期输出:08:15:00;给定输出:07:15:00;

德哈尔

您可以使用java.time现代日期时间 API,并将时间解析String为某个时刻,即Instant.
然后使用ZonedDateTime对象来应用某个时区,这可能以不同的方式完成,我在这里向您展示其中之一:

public static void main(String[] args) {
    // the source is just a time, but to correctly convert it, you need a date
    String utcTime = "12:15:00";
    // take today's date
    LocalDate today = LocalDate.now();
    // create a parseable date time String
    String parseableDateTime = today.format(DateTimeFormatter.ISO_DATE) + "T" + utcTime + "Z";
    // then create an Instant parsing the date time String
    Instant instant = Instant.parse(parseableDateTime);

    // get the ZoneId of UTC in order to have the time in UTC
    ZoneId utc = ZoneId.of("UTC");
    // do the same with your ZoneOffset of -4 Hours
    ZoneId gmtMinusFour = ZoneId.ofOffset("GMT", ZoneOffset.ofHours(-4));

    // create a UTC ZonedDateTime of the instant and the UTC ZoneID 
    ZonedDateTime utcZdt = ZonedDateTime.ofInstant(instant, utc);
    // then use that ZonedDateTime to convert it to a time with your ZoneId
    ZonedDateTime gmtMinusFourZdt = utcZdt.withZoneSameInstant(gmtMinusFour);

    // finally print both ZonedDateTimes in order to compare them
    System.out.println("UTC time is:\t\t" 
            + utcZdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    System.out.println("GMT-4 time is:\t\t"
            + gmtMinusFourZdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));

    // then just get the time part of the converted ZonedDateTime
    LocalTime localTime = gmtMinusFourZdt.toLocalTime();
    // and print it
    System.out.println("Converted time is:\t" 
            + localTime.format(DateTimeFormatter.ISO_TIME));
}

输出:

UTC time is:        2019-09-24T12:15:00Z[UTC]
GMT-4 time is:      2019-09-24T08:15:00-04:00[GMT-04:00]
Converted time is:  08:15:00

可能有更好的解决方案,但无论如何我希望这会有所帮助......

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

格式时间:hh:mm:ss

将时间从{yyyy-mm-dd hh:mm:ss}转换为{yyyy-mm-dd hh}

将 hh:mm:ss 格式的时间转换为秒或分钟

将时间转换为分钟HH:MM:SS格式

JAVA将分钟转换为默认时间[hh:mm:ss]

将整数转换为日期时间(dd:hh:mm:ss)

将地理时间格式转换为HH:MM:SS

将数字转换为时间格式 HH:mm:ss

使用moment.js将时间(HH:mm:ss)转换为(HH:mm)

将格式为HH:MM AM / PM的时间转换为HH:MM:SS

解析时间hh:mm:ss的时间

C#将字符串“yyyy-MM-dd hh:mm:ss 'UTC'”转换为日期时间

Python:如何将UTC时间戳从mm / dd / yyyy hh:mm:ss AM更改为dd / mm / yyyy hh:mm:ss AM

将时间格式“dd/MM/yyyy hh:mm:ss”转换为“yyyy-MM-ddThh:mm:ss.SSSz”

如何在PHP中将HH:MM:SS时间转换为天数:HH:MM:SS

将 SPSS 中的时间从 hhmm:ss 转换为 hh:mm:ss (TIME8)

操作上个别时间:HH:MM:SS

更改时间格式HH:MM:SS

以hh:mm:ss显示绘图直方图时间

数组 按时间排序 hh:mm:ss

Coldfusion日期转换将yyyy-mm-dd:hh:mm:ss转换为日期和时间

将具有时间值 (hh:mm:ss) 的整列转换为文本时间 (hh:mm)

将日期时间转换为MM / dd / yyyy HH:MM:SS AM / PM

将字符串转换为日期时间dd / MM / yyyy hh:mm:ss tt

将日期时间 (yyyy-mm-dd hh:mm:ss) 格式转换为 EXCEL 中的秒

将MM / DD / YYYY HH:MM:SS转换为Unix时间戳(SQL)

在python中将时间(HH:MM:SS)转换为分钟

如何将 yyyy-MM-dd-hh.mm.ss 中的日期字段转换为 Hive 中的时间戳字段(yyyy-MM-dd hh:mm:ss))

将时间hh:mm:ss转换为Angular 2中的GMT时间格式