Search code examples
scaladatetimeuser-defined-functionsdatetime-formattime-format

Convert time string into timestamp/date time in scala


I am receiving time data into my source as a csv file in the format (HHMMSSHS). I am not sure about what HS in the format stands for. example data will be like 15110708.

I am creating table in databricks table with received columns and data. I want to convert this field to time while processing in scala. I am using UDF to do formating on any data on the go. But for this i am totally stuck while writing a UDF for parsing only time.

The final output should be 15:11:07:08 or any time format suitable for this string.

I tried with java.text.SimpleDateFormat and faced issue with unparsable string.

Is there any way to convert the above given string to a time format?

I am storing this value as acolumn in databricks notebook table. Is there any other format other than string to save only time values?


Solution

  • Have you tried?:

    import java.time.LocalTime
    
    val dtf : DateTimeFormatter = DateTimeFormatter.ofPattern("HHmmssSS")
      
    val localTime = udf { str : String =>
      LocalTime.parse(str, dtf).toString
    }
    

    that gives:

    +---------+------------+
    |Timestamp|converted   |
    +---------+------------+
    |15110708 |15:11:07.080|
    |15110708 |15:11:07.080|
    +---------+------------+