将日期转换为Scala中的时间戳 [英] Convert Date to Timestamp in Scala

查看:1145
本文介绍了将日期转换为Scala中的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala中,我将Date转换为Timestamp。我目前正在这样做:

In Scala, I am converting Date to Timestamp. I am currently doing this with:

val date = new java.util.Date()
new java.sql.Timestamp(new org.joda.time.DateTime(date).getMillis)

有没有这样做的方式? Java通知的响应也是相关的。

Is there a slicker way of doing this? Java-informed responses would also be relevant.

推荐答案

为什么不像使用 Date.getTime()

Why not something as simple as using Date.getTime()?

new java.sql.Timestamp(date.getTime)

您不需要Joda时间。 Scala在这里并不重要,除非您需要隐式转换:

You don't need Joda time for this. Scala isn't really relevant here, unless you need an implicit conversion:

//import once, use everywhere
implicit def date2timestamp(date: java.util.Date) = 
    new java.sql.Timestamp(date.getTime)

val date = new java.util.Date

//conversion happens implicitly
val timestamp: java.sql.Timestamp = date

这篇关于将日期转换为Scala中的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆