[英]Error in converting Date to String
Date date = new Date();
try {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Timestamp timestamp=new Timestamp(date.getTime());
id=Long.parseLong(timestamp.toString());
在執行上面的代碼時,我得到了這個錯誤:
java.lang.NumberFormatException: For input string: "2018-08-29 16:35:31.753"
at
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
現在怎么了?
當您嘗試通過Timestamp.toString()
獲得long值時,會發生錯誤,因為它返回2018-08-29 16:35:31.753
,這不是有效的long。
只需使用:
long id = timestamp.getTime();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.