例如:
epochtime = 1450575628 [Sun, 20 Dec 2015 01:40:28]
我的输出应为:1450569600 [Sun, 20 Dec 2015 00:00:00]
如何为任何随机的epochtime编写scala脚本?
答案 0 :(得分:1)
使用Java 8及更高版本中内置的java.time
框架,可从Scala获得。
import java.time._
val dateFromEpoch = LocalDateTime
.ofInstant(Instant.ofEpochSecond(1450575628), ZoneId.of("UTC"))
.`with`(LocalTime.MIN)
dateFromEpoch.toEpochSecond(ZoneOffset.UTC) # Long = 1450569600
答案 1 :(得分:0)
您可以执行以下操作:
// construct a new Date object from the given time
Date date = new Date(epochtime * 1000);
// get the calendar and set its time
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// reset hours to zero (do the same for minutes and seconds)
cal.set(Calendar.HOUR, 0);
// construct Date object after modifications
Date newDate = cal.getTime();
System.out.println(newDate);
// now you can get epoch time from the last object
epochtime = newDate.getTime();