[英]How to compare the datetime of format "EEE MMM dd HH:mm:ss zzz yyyy" and "yyyy-MM-dd hh:mm:sss" in java?
[英]SimpleDateFormat appears to fail with “yyyy-MM-dd HH:mm:ss.SSS0”
以下為什么不工作? 似乎最后的字面零是原因......
final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS0");
format.setLenient(false);
String d = format.format(new Date());
System.out.println(format.parse(d));
我不知道為什么你需要在模式的末尾添加零(0),但是你應該在''中包含非模式字母以使它們工作:
final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'0'");
更多信息:
SimpleDateFormat
,文檔開頭的示例部分。 您的代碼存在問題
System.out.println(format.parse(d));
嘗試將超過3位數的毫秒解析為java.util.Date將導致異常。 解析String的模式應該是
"yyyy-MM-dd HH:mm:ss.SSS" //without the zero at the end, now your whole code will work...
如果你正在使用納秒或你的數據有它們,你可以忽略這些值,Java Date / Time API也不支持納秒(在文檔中他們甚至沒有提到它)。
如果你確實需要將納秒用於不同的目的,那么你應該堅持使用String而不是Date對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.