0

I tried to parsing time string to time data, But having problem to parsing millisecond part. Anyone know how to do this?

   val format = new SimpleDateFormat( "yyyy-MM-dd aa hh:mm:ss.SSSSSS")
    format.setLenient( false)

    Try(new Timestamp(format.parse( "2015-11-13 6:27:26.933000").getTime)) match {
        case Success(t) => Some(t)
        case Failure(_) =>
            // cannot convert. error here
    }
Rich
  • 15,048
  • 2
  • 66
  • 119
J.Done
  • 2,783
  • 9
  • 31
  • 58
  • 1
    Possible duplicate of [Java date parsing with microsecond or nanosecond accuracy](https://stackoverflow.com/questions/30135025/java-date-parsing-with-microsecond-or-nanosecond-accuracy) – michaJlS Sep 12 '17 at 09:16

1 Answers1

2
  • You use aa but don't provide it in the actual input, what causes a failure
  • S stands for milliseconds, and it ranges between 0 and 999, and you provided a bigger number, what causes a failure
  • you asked for hh, but gave in the input a single digit, but it doesn't cause a failure.

And working example:

val format = new SimpleDateFormat( "yyyy-MM-dd a hh:mm:ss.SSS")
format.setLenient( false)

Try(new Timestamp(format.parse("2015-11-13 PM 06:27:26.933").getTime)) match {
  case Success(t) => println(t.getTime)
  case Failure(err) => println(err)
}
michaJlS
  • 2,465
  • 1
  • 16
  • 22