java将日期格式转换为数字日期
我有一个这样的日期:
Tue Mar 10 00:00:00 UTC 1987
它存储在对象日期中
Object tmp = solrDoc.getFieldValue("date_from")
我想把它转换成一个严格的数字格式,没有时间,时区等,比如
10.03.1987
这就是我迄今为止所尝试的:
DateFormat date = new SimpleDateFormat("dd.MM.yyyy");
date.format(tmp);
它返回:
"java.text.SimpleDateFormat@7147a660"
# 1 楼答案
您试图在
Object
上使用format
方法,但根据documentation,您需要将此方法传递给Date
。所以你实际上需要做的是把原始的String
解析成Date
,然后格式化例如,你可以这样做:
请注意,我必须在
formateToWrite
上设置TimeZone
以保持UTC如果您想要更多关于我用来解析您的原始
String
的SimpleDateFormat
的信息,请参考this SO answer