Search code examples
scalasimpledateformattimestamp-with-timezone

Converting String timestamp to Scala Timestamp with Timezone throws Unparseable exception


I am working with Timestamps in String format. And trying to convert them to a Timestamp Value. It is throwing an exception for me.

Can anyone tell me what am I doing wrong here:

val s = "2017-12-14T09:54:52.662-06:00"
val format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

val ts = new Timestamp(format.parse(s).getTime)

Throws: java.text.ParseException: Unparseable date: "2017-12-14T09:54:52.662-06:00"

Solution

  • That's because you use wrong pattern. Try to print format.format(new Date()) and you will see.

    Right pattern:

    val format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")