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.
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.
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.
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.
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.
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
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With