且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java:将字符串转换为时间戳

更新时间:2022-01-18 04:01:34

按照以下步骤获得正确的结果:

Follow these steps for a correct result:

try {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
    Date parsedDate = dateFormat.parse(yourString);
    Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch(Exception e) { //this generic but you can control another types of exception
    // look the origin of excption 
}

请注意.parse(String) 可能会抛出 ParseException.