Java中的字符串到时间戳转换差异错误[重复]

问题描述 投票:1回答:1

我有以下价值:2018-01-16-18.56.57.300000

它传递给方法参数:“value”。

private Timestamp getPossibleTimestampI(String value) {

   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss.SSS");
    Date parsedDate;
    Timestamp timestamp=null; 
    try {
        parsedDate = dateFormat.parse(value);
        timestamp = new java.sql.Timestamp(parsedDate.getTime());
    } catch (ParseException e1) {
        e1.printStackTrace();
    }

return timestamp;
}

我得到的Timestamp对象的值为2018-01-16 19:01:57.0,与原始字符串值相比大约多5分钟。

为什么会发生这种情况,如何纠正我的转换?

java string timestamp date-conversion sql-timestamp
1个回答
0
投票

在时间2018-01-16-18.56.57.300000中,您的300000毫秒正在转换为分钟

which is 300000/60000 = 5 minutes
© www.soinside.com 2019 - 2024. All rights reserved.