Scala / Spark

How to write current date timestamp to log file in Scala[Code Snippet]

Scala doesn’t have its own library for Dates and timestamps, so we need to depend on Java libraries.
Here is the quick method to get current datetimestamp and format it as per your required format. Please note that all the code syntaxes are in Scala, this can be used while writing Scala application.

import java.sql.Timestamp
def getCurrentdateTimeStamp: Timestamp ={
val today:java.util.Date = Calendar.getInstance.getTime
val timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val now:String = timeFormat.format(today)
val re = java.sql.Timestamp.valueOf(now)
re
}

import java.sql.Timestamp
getCurrentdateTimeStamp: java.sql.Timestamp
getCurrentdateTimeStamp
res0: java.sql.Timestamp = 2018-03-18 07:48:00.0

Share This Post

An Ambivert, music lover, enthusiast, artist, designer, coder, gamer, content writer. He is Professional Software Developer with hands-on experience in Spark, Kafka, Scala, Python, Hadoop, Hive, Sqoop, Pig, php, html,css. Know more about him at www.24tutorials.com/sai

Lost Password

Register

24 Tutorials