以下似乎无法正常工作。例如,对于值“2014-04-10 12:58:23.0”和“2014-04-10 00:58:23.0”,它会产生相同的答案。
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Long timestamp = dateFormat.parse(date).getTime()/1000L;
最佳答案
您的输入字符串似乎采用 24 小时时间格式,而格式字符串指定 12 小时时间格式。尝试使用 H
而不是 h
。
来源:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
关于java - 如何在Java中安全地将时间转换为unix时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31507301/