我想将以毫秒为单位的日期转换为日期,但不能正确转换。
public static void main(String[] args) {
LocalDate today = LocalDateTime.now().toLocalDate();
System.out.println("Today: ----------" + today);
long todayLong = today.atStartOfDay(ZoneId.systemDefault()).toEpochSecond();
System.out.println("todayLong: ---------- " + todayLong);
LocalDate todayBack = Instant.ofEpochMilli(todayLong).atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println("todayBack: ---------- "+todayBack);
}
这是输出:
今天:---------- 2017-06-27
今天长:---------- 1498507200
今天返回:---------- 1970-01-18
最佳答案
您需要使用ofEpochSecond
而不是ofEpochMilli
,因为您的todayLong
是second
,而不是milisecond
LocalDate todayBack = Instant.ofEpochSecond(todayLong).atZone(ZoneId.systemDefault()).toLocalDate();