Scala将DateTime值转换为时间戳

时间:2018-11-21 15:23:33

标签: scala datetime

也许这是一个重复的问题,我试图找到一个解决方案,但是没有办法。

问题:将DateTime实例转换为时间戳。

我的DateTime实例创建为:

<h2 class="example">A heading with class="example"</h2>

<p>Click the button to add a background color to the first element in the document with class="example".</p>

<button id="btn">Try it</button>

使用这个日期的println,我得到了:

    import org.joda.time.DateTime
    val start = (new DateTime).withYear(2016)
       .withMonthOfYear(12)
       .withDayOfMonth(1)
       .withMinuteOfHour(0)
       .withHourOfDay(0)

我需要的是像下面的大纪元时间戳: enter image description here

3 个答案:

答案 0 :(得分:3)

IMHO计算纪元时间戳最快的方法之一是:

scala> :paste
// Entering paste mode (ctrl-D to finish)

def epochDayForYear(year: Int): Long =
  365L * year + (((year + 3) >> 2) - (if (year < 0) {
    val century = year * 1374389535L >> 37 // divide int by 100 (a sign correction is not required)
    century - (century >> 2)
  } else ((year + 99) * 1374389535L >> 37) - ((year + 399) * 1374389535L >> 39))) // divide int by 100 and by 400 accordingly (a sign correction is not required)

def dayOfYearForYearMonth(year: Int, month: Int): Int =
  ((month * 1050835331877L - 1036518774222L) >> 35).toInt - // == (367 * month - 362) / 12
    (if (month <= 2) 0
    else if (isLeap(year)) 1
    else 2)

def isLeap(year: Int): Boolean = (year & 3) == 0 && {
  val century = (year * 1374389535L >> 37).toInt - (year >> 31) // divide int by 100
  century * 100 != year || (century & 3) == 0
}

def secondOfDay(hour: Int, month: Int, day: Int): Int = 
  hour * 3600 + month * 60 + day

val (year, month, day, hour, minute, second, timeZoneOffsetHour, timeZoneOffsetMinute) = 
  (2016, 12, 1, 0, 0, 2, 0, 0)

val epochSecond = 
  (epochDayForYear(year) + (dayOfYearForYearMonth(year, month) + day - 719529)) * 86400 + 
  secondOfDay(hour + timeZoneOffsetHour, minute + timeZoneOffsetMinute, second) // where 719528 is days 0000 to 1970

// Exiting paste mode, now interpreting.

epochDayForYear: (year: Int)Long
dayOfYearForYearMonth: (year: Int, month: Int)Int
isLeap: (year: Int)Boolean
secondOfDay: (hour: Int, month: Int, day: Int)Int
year: Int = 2016
month: Int = 12
day: Int = 1
hour: Int = 0
minute: Int = 0
second: Int = 2
timeZoneOffsetHour: Int = 0
timeZoneOffsetMinute: Int = 0
epochSecond: Long = 1480550402          

顺便说一句,大部分代码都是从jsoniter-scala项目中借来的。

答案 1 :(得分:0)

首先:Scala被编译成Java,所以当您问有关Java库的问题时,可以使用其中的示例。

第二:您的应用程序中是否有默认时区?我会将其放在一个合理的位置(即,当您启动应用程序时/在某些配置中)

DateTimeZone.setDefault(DateTimeZone.UTC);

现在您可以像这样创建一个新的java.sql.Timestamp:

import java.sql.Timestamp;
import java.time.Instant

  val start = (new DateTime).withYear(2016)
   .withMonthOfYear(12)
   .withDayOfMonth(1)
   .withMinuteOfHour(0)
   .withHourOfDay(0)

  val timestamp = new Timestamp(start.getMillis()) : Timestamp

  // To get the epoch timestamp, take a look at java.time.Instant

  val epochTimestamp =  timestamp.toInstant() : Instant

此处有更多信息:https://www.jeejava.com/conversion-of-joda-date-time-to-sql-timestamp-and-vice-versa/

答案 2 :(得分:0)

使用java.time。这有帮助吗?

scala> val x = java.time.LocalDateTime.ofEpochSecond(System.currentTimeMillis/1000,0,java.time.ZoneOffset.UTC)
x: java.time.LocalDateTime = 2018-11-21T18:41:29

scala> java.time.LocalDateTime.parse("2018-11-21T18:41:29.123").toEpochSecond(java.time.ZoneOffset.UTC)
res41: Long = 1542825689

scala> java.time.LocalDateTime.parse("2018-11-21T18:41:29.123").format(java.time.format.DateTimeFormatter.ofPattern("eeee, MMMM dd, yyyy hh:mm:ss a"))
res61: String = Wednesday, November 21, 2018 06:41:29 PM

scala>

此链接https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html可能有助于了解格式选项中使用的符号