如何将ISO 8601格式的DateTime转换为Java中的另一个时区? [英] How to convert ISO 8601 formatted DateTime to another time zone in Java?

查看:81
本文介绍了如何将ISO 8601格式的DateTime转换为Java中的另一个时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的日期:
日期utc: 2018-06-06T16:30:00Z (采用UTC的ISO 8601)

日期ISO: 2018-06-06T11:30:00-05:00 (ISO 8601)

日期时代: 1528302600000 (Epoch/Unix时间戳)

My current date:
Date utc: 2018-06-06T16:30:00Z (ISO 8601 in UTC)
OR
Date iso: 2018-06-06T11:30:00-05:00 (ISO 8601)
OR
Date epoch: 1528302600000 (Epoch/Unix Timestamp)

我希望将上面的DateTime转换为其他时区(例如GMT + 5:30).我不确定从以上三种时间中会收到哪种时间格式.那么,我可以使用一个通用方法将上面的代码转换为另一个时区,并返回Java 8中的java.util.Date吗?

I wish to convert the above DateTime to some another time zone areas (like GMT+5:30). And I'm not sure which time format I'll receive from above three. So can I've a generic method which can convert above to some another time zone returning java.util.Date in Java 8?

我做了这样的事情,但是没有成功

I did Something like this, But it didn't worked out

public Date convertDateToLocalTZ(Date iso8601, ZoneId toZoneId) {
    Date dateTime = null;
    if (iso8601 != null && toZoneId != null) {
        Instant instant = iso8601.toInstant();
        LocalDateTime localDateTime = instant.atZone(toZoneId).toLocalDateTime();
        dateTime = Date.from(localDateTime.atZone(toZoneId).toInstant());
        return dateTime;
    }
    return dateTime;
}

推荐答案

由于标记了问题java-8,请使用

Since question is tagged java-8 use java.time API.

更新:对于添加了2018-06-06T11:30:00-05:00的问题的第4版.

UPDATE: For version 4 of question where 2018-06-06T11:30:00-05:00 was added.

要解析1528302600000,请将其解析为long,然后使用

To parse 1528302600000, you parse it into a long, then use Instant.ofEpochMilli().

要解析类似2018-06-06T11:30:00-05:00的格式,可以使用 ZonedDateTime .两者都可以解析2018-06-06T16:30:00Z.

To parse a format like 2018-06-06T11:30:00-05:00, you can using OffsetDateTime or ZonedDateTime. Both can also parse 2018-06-06T16:30:00Z.

要将时区专门更改为特定的偏移量(例如GMT+5:30),请使用 ZoneOffset ,例如 ZoneOffset.of("+05:30") ZoneId ,例如 ZoneId.of("GMT+05:30") .
注释1: GMT+5:30无效.
注释2:要更改区域的时区,以纪念夏令时,请使用例如ZoneId.of("Asia/Kolkata").

To change the time zone specifically to a particular offset like GMT+5:30, use ZoneOffset, e.g. ZoneOffset.of("+05:30"), or ZoneId, e.g. ZoneId.of("GMT+05:30").
Note 1: GMT+5:30 is not valid.
Note 2: To change to the time zone of a region, honoring Daylight Savings Time, use e.g. ZoneId.of("Asia/Kolkata").

要解析所有3种输入格式,甚至支持像2018-06-06T11:30-05:00[America/Chicago]这样的扩展格式,请使用ZonedDateTime,并对历元数进行特殊处理.

To parse all 3 input formats, and even support the extended format like 2018-06-06T11:30-05:00[America/Chicago], use ZonedDateTime, with special handling for the epoch number.

public static ZonedDateTime parseToZone(String text, ZoneId zone) {
    if (text.indexOf('-') == -1)
        return Instant.ofEpochMilli(Long.parseLong(text)).atZone(zone);
    return ZonedDateTime.parse(text).withZoneSameInstant(zone);
}

然后,调用者可以通过使用

The caller can then decide if only the offset, not the full time zone, should be used, by converting it to OffsetDateTime using toOffsetDateTime().

测试

ZoneId india = ZoneId.of("Asia/Kolkata");

System.out.println(parseToZone("2018-06-06T16:30:00Z", india));
System.out.println(parseToZone("2018-06-06T11:30:00-05:00", india));
System.out.println(parseToZone("1528302600000", india));

System.out.println(parseToZone("1528302600000", india).toOffsetDateTime());

输出

2018-06-06T22:00+05:30[Asia/Kolkata]
2018-06-06T22:00+05:30[Asia/Kolkata]
2018-06-06T22:00+05:30[Asia/Kolkata]
2018-06-06T22:00+05:30


原始答案

使用 ofEpochMilli() 1528302600000的a>方法.
然后使用
atZone() 转换为您所需的时区.

Use the parse() method with 2018-06-06T16:30:00Z.
Use the ofEpochMilli() method with 1528302600000.
Then use atZone() to convert to your desired time zone.

演示

Instant instant1 = Instant.parse("2018-06-06T16:30:00Z");
Instant instant2 = Instant.ofEpochMilli(1528302600000L);

ZoneId india = ZoneId.of("Asia/Kolkata");
ZonedDateTime date1 = instant1.atZone(india);
ZonedDateTime date2 = instant2.atZone(india);

System.out.println(instant1);
System.out.println(instant2);
System.out.println(date1);
System.out.println(date2);

输出

2018-06-06T16:30:00Z
2018-06-06T16:30:00Z
2018-06-06T22:00+05:30[Asia/Kolkata]
2018-06-06T22:00+05:30[Asia/Kolkata]

要以人工格式打印结果,请使用

To print the result in human format, use a DateTimeFormatter.

DateTimeFormatter indiaFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
                                                    .withLocale(Locale.forLanguageTag("en-IN"));
DateTimeFormatter hindiFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
                                                    .withLocale(Locale.forLanguageTag("hi-IN"));
System.out.println(date1.format(indiaFormatter));
System.out.println(date1.format(hindiFormatter));

输出

6 June 2018 at 10:00:00 PM IST
6 जून 2018 को 10:00:00 अपराह्न IST

这篇关于如何将ISO 8601格式的DateTime转换为Java中的另一个时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆