Java 准确的日期到字符串的转换(没有本地 GMT 偏移量)

Java exact Date to String conversion (no local GMT offset)

我正在尝试完成一项非常简单的任务。 将以纪元秒为单位的时间(long myTime)转换为字符串(但不进行任何调整)。

DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Instant AA = Instant.ofEpochSecond(myTime);
String reportDate = df.format(Date.from(AA));

"reportDate" 自动调整为 Windows 的 GMT 偏移量。 我需要 exact 即时到字符串的转换。 我是 Java 的新手 ....

具体设置时区

df.setTimeZone(TimeZone.getTimeZone("GMT"));

这会将偏移量设置为您需要的任何值,这样它将在任何服务器上输出相同的日期时间。