Java 将指定区域的长历元时间转换为UTC长历元时间
我需要将多区域变量的长历元时间转换为UTC的长历元时间 在joda时代,我一直在尝试做以下事情:Java 将指定区域的长历元时间转换为UTC长历元时间,java,Java,我需要将多区域变量的长历元时间转换为UTC的长历元时间 在joda时代,我一直在尝试做以下事情: long getUTCLong(long timestamp, String timeZone) { DateTimeZone zone = DateTimeZone.forID(timeZone); DateTime dt = new DateTime(timestamp, zone); dt.getMillis(); } 但这是行不通的。如何使用新的java.time功
long getUTCLong(long timestamp, String timeZone) {
DateTimeZone zone = DateTimeZone.forID(timeZone);
DateTime dt = new DateTime(timestamp, zone);
dt.getMillis();
}
但这是行不通的。如何使用新的java.time功能实现这一点尽管问题代码使用了Joda time,但实际上它要求使用java time API解决问题,因此如下所示: 静态长getUTCLonglong时间戳,字符串时区{ 返回Instant.ofepochmillitestamp//自1970-01-01以毫秒为单位处理时间戳 .atZoneZoneOffset.UTC.toLocalDateTime//获取没有时区的纯日期/时间 .atZoneZoneId.oftimeZone//将日期/时间标记为在给定时区中 .toInstant//转换为UTC .toEpochMilli;//获取1970-01-01以来的历元时间(毫秒) }
尽管问题代码使用了Joda时间,但实际上它要求使用Java时间API来解决问题,所以这里是: 静态长getUTCLonglong时间戳,字符串时区{ 返回Instant.ofepochmillitestamp//自1970-01-01以毫秒为单位处理时间戳 .atZoneZoneOffset.UTC.toLocalDateTime//获取没有时区的纯日期/时间 .atZoneZoneId.oftimeZone//将日期/时间标记为在给定时区中 .toInstant//转换为UTC .toEpochMilli;//获取1970-01-01以来的历元时间(毫秒) }