如何在scala中获得任何纪元的开始?

时间:2015-12-23 13:30:57

标签: java scala epoch

例如:

 epochtime = 1450575628  [Sun, 20 Dec 2015 01:40:28]

我的输出应为:1450569600 [Sun, 20 Dec 2015 00:00:00]

如何为任何随机的epochtime编写scala脚本?

2 个答案:

答案 0 :(得分:1)

java.time

使用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();