Convert Java String to sql.Timestamp

前端 未结 5 903

Got a String coming in with this format: YYYY-MM-DD-HH.MM.SS.NNNNNN The Timestamp is coming from a DB2 database. I need to parse it into a java.sql.Timestamp and NOT lose a

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 14:04

    I believe you need to do this:

    1. Convert everythingButNano using SimpleDateFormat or the like to everythingDate.
    2. Convert nano using Long.valueof(nano)
    3. Convert everythingDate to a Timestamp with new Timestamp(everythingDate.getTime())
    4. Set the Timestamp nanos with Timestamp.setNano()

    Option 2 Convert to the date format pointed out in Jon Skeet's answer and use that.

提交回复
热议问题