如何使用ZoneDateTime或Java 8将任何日期时间转换为UTC
我正在尝试使用如何使用ZoneDateTime或Java 8将任何日期时间转换为UTC,java,date,java-8,zoneddatetime,Java,Date,Java 8,Zoneddatetime,我正在尝试使用ZonedDateTime将日期06-12-2015 02:10:10 PM从默认区域转换为UTC LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); ZonedDateTime utc = ZonedDateTime.of(localDateTime, ZoneOffset.UTC); 但是utc返回2015-12-06T14:10:10
ZonedDateTime
将日期06-12-2015 02:10:10 PM
从默认区域转换为UTC
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
ZonedDateTime utc = ZonedDateTime.of(localDateTime, ZoneOffset.UTC);
但是utc
返回2015-12-06T14:10:10Z
而不是06-12-2015 09:10:10AM
如何将日期从默认区域转换为UTC?给出的答案将当前时间转换为UTC。您可以在第二个参数为UTC
(即时知道本地偏移)的情况下使用。大概
String source = "06-12-2015 02:10:10 PM";
String pattern = "MM-dd-yyyy hh:mm:ss a";
DateFormat sdf = new SimpleDateFormat(pattern);
try {
Date date = sdf.parse(source);
ZonedDateTime zdt = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC"));
System.out.println(zdt.format(DateTimeFormatter.ofPattern(pattern)));
} catch (ParseException e) {
e.printStackTrace();
}
我得到(对应于我的本地区域偏移)
2015年12月6日巴基斯坦时间下午02:10:10=UTC时间2015年12月6日上午09:10:10
有很多方法可以做到这一点
LocalDateTime
➡️ 将其与您的时区组合,以获得ZonedDateTime
➡️ 转换为Instant
➡️ 使用和UTC时区转换为ZoneDateTime
LocalDateTime
➡️ 将其与您的时区组合,以获得ZonedDateTime
➡️ 转换为Instant
➡️ 使用和UTC时区转换为ZoneDateTime
*无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。
如果我这样做
Date date1=Date.from(zdt.toInstant())
它返回原始时间06-12-2015 02:10:10 PM
为什么?为什么要将SimpleDateFormat与java.time代码混合使用?为什么不让java.time做解析呢?@BasilBourque OP的代码包括date.toInstant()
,我认为这就是问题所在。通过大量示例代码,得到了非常好的研究答案
06-12-2015 06:10:10 PM
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "06-12-2015 02:10:10 PM";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Asia/Karachi") for the demo. Change it to
// ZoneId.systemDefault()
Instant instant = ldt.atZone(ZoneId.of("Asia/Karachi")).toInstant();
ZonedDateTime zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 06-12-2015 09:10:10 AM
}
}
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "06-12-2015 02:10:10 PM";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Asia/Karachi") for the demo. Change it to
// ZoneId.systemDefault()
Instant instant = ldt.atZone(ZoneId.of("Asia/Karachi")).toInstant();
ZonedDateTime zdtUtc = ZonedDateTime.ofInstant(instant, ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 06-12-2015 09:10:10 AM
}
}
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "06-12-2015 02:10:10 PM";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
// Using ZoneId.of("Asia/Karachi") for the demo. Change it to
// ZoneId.systemDefault()
ZonedDateTime zdtPak = ldt.atZone(ZoneId.of("Asia/Karachi"));
ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc.format(dtf)); // 06-12-2015 09:10:10 AM
}
}
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "06-12-2015 02:10:10 PM";
// Using ZoneId.of("Asia/Karachi") for the demo. Change it to
// ZoneId.systemDefault()
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("M-d-u h:m:s a", Locale.ENGLISH)
.withZone(ZoneId.of("Asia/Karachi"));
ZonedDateTime zdtPak = ZonedDateTime.parse(strDateTime, dtfInput);
ZonedDateTime zdtUtc = zdtPak.withZoneSameInstant(ZoneId.of("Etc/UTC"));
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm:ss a", Locale.ENGLISH);
System.out.println(zdtUtc.format(dtfOutput)); // 06-12-2015 09:10:10 AM
}
}