Java 8 LocalDateTime 到丢失时区的日期
Java 8 LocalDateTime to Date Lost Timezone
我有这段代码,我系统的默认时区是 PDT。时区转换后,finalDate 显示 PDT 时间。如何让它在 "Asia/Singapore" 中显示最终日期?
String strDate = 201507081245;
DateTimeFormatter mx3DateFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
LocalDateTime localDateTime = LocalDateTime.parse(strDate, format);
Instant instant = localDateTime.atZone(ZoneId.of("Asia/Singapore")).toInstant();
Date finalDate = Date.from(instant);
java.util.Date
object is not specific to a particular time zone. Yet its toString
方法应用 JVM 当前的默认时区。
您可以使用 DateFormat
such as SimpleDateFormat
.
要求它打印特定时区的时间
有点像,
SimpleDateFormat dateFormat = new SimpleDateFormat("hhhhMMddHHmm");
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Singapore"));
System.out.println(dateFormat.format(finalDate));
我有这段代码,我系统的默认时区是 PDT。时区转换后,finalDate 显示 PDT 时间。如何让它在 "Asia/Singapore" 中显示最终日期?
String strDate = 201507081245;
DateTimeFormatter mx3DateFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
LocalDateTime localDateTime = LocalDateTime.parse(strDate, format);
Instant instant = localDateTime.atZone(ZoneId.of("Asia/Singapore")).toInstant();
Date finalDate = Date.from(instant);
java.util.Date
object is not specific to a particular time zone. Yet its toString
方法应用 JVM 当前的默认时区。
您可以使用 DateFormat
such as SimpleDateFormat
.
有点像,
SimpleDateFormat dateFormat = new SimpleDateFormat("hhhhMMddHHmm");
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Singapore"));
System.out.println(dateFormat.format(finalDate));