Search code examples
scalaclassapache-sparkcasedate-format

How to convert Date column format in case class of Scala?


I am using Scala spark.I have two similar CSV files with 10 columns.One difference is with the Date column format. 1st file Date format yyyy-MM-dd 2nd file Date format dd-MM-yyyy Objective is to: create seperate schema rdd for each file and finally merge both the Rdds.

For the first case class, I have used Date.valueOf [java.sql.Date] in the case class mapping.No issues here..

Am finding issue with the 2nd file Date format.. I have used the same Date.valueOf mapping..but it's throwing error in the date format...

How can I map the date format in the second file as like the 1st format yyyy-MM-dd? Please assist


Solution

  • Use java.util.Date:

    val sDate1="31/12/1998"
    val date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1)  
    import java.text.SimpleDateFormat
    

    Result:

    sDate1: String = 31/12/1998
    date1: java.util.Date = Thu Dec 31 00:00:00 CET 1998
    

    to change the output format as a common string format.

    val date2=new SimpleDateFormat("yyyy/MM/dd")
    date2.format(date1)
    

    Result:

    res1: String = 1998/12/31