Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Date to Timestamp in Scala

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)

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

like image 874
will Avatar asked Nov 13 '12 21:11

will


People also ask

How to Convert date to Timestamp in Spark scala?

Syntax – to_timestamp() This function has two signatures, the first signature takes just one argument and the argument should be in Timestamp format MM-dd-yyyy HH:mm:ss. SSS , when the format is not in this format, it returns null. Related: Refer to Spark SQL Date and Timestamp Functions for all Date & Time functions.

How to Convert date into Timestamp in Spark SQL?

Spark Timestamp consists of value in the format “yyyy-MM-dd HH:mm:ss. SSSS” and date format would be ” yyyy-MM-dd”, Use to_date() function to truncate time from Timestamp or to convert the timestamp to date on Spark DataFrame column.

How do you use dates in Scala?

So to use date in scala we can use java library by importing packages. After this we can use its function to deal with date and time in scala, we can parse, format, and create the date from string object as well.

How to Convert string into Timestamp in Spark SQL?

Use <em>to_timestamp</em>() function to convert String to Timestamp (TimestampType) in PySpark. The converted time would be in a default format of MM-dd-yyyy HH:mm:ss. SSS , I will explain how to use this function with a few examples.


2 Answers

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

new java.sql.Timestamp(date.getTime)

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
like image 95
Tomasz Nurkiewicz Avatar answered Oct 23 '22 00:10

Tomasz Nurkiewicz


If you don't want to even construct a Date object you could simply use this:

new java.sql.Timestamp(System.currentTimeMillis())

I would think this would be a tiny bit more efficient than using a new Date(). But if you already have a date object you want to get the date from this will work for you.

new java.sql.Timestamp(data.getTime())
like image 33
myyk Avatar answered Oct 23 '22 00:10

myyk