Convert Java String to sql.Timestamp

前端 未结 5 898

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:01

    Here's the intended way to convert a String to a Date:

    String timestamp = "2011-10-02-18.48.05.123";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd-kk.mm.ss.SSS");
    Date parsedDate = df.parse(timestamp);
    

    Admittedly, it only has millisecond resolution, but in all services slower than Twitter, that's all you'll need, especially since most machines don't even track down to the actual nanoseconds.

提交回复
热议问题