您在这里基本上要做的就是依靠
Date#toString()已经具有固定模式的文件。要将Java
Date对象转换为其他人类可读的String模式,您需要
SimpleDateFormat#format()。
private String modifyDateLayout(String inputDate) throws ParseException{ Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate); return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);}顺便说一句,“不可解析的日期”异常在这里 只能
由抛出
SimpleDateFormat#parse()。这意味着
inputDate不在预期的模式中
"yyyy-MM-dd HH:mm:ss z"。您可能需要修改模式以匹配
inputDate的实际模式。
更新: 好的,我做了一个测试:
public static void main(String[] args) throws Exception { String inputDate = "2010-01-04 01:32:27 UTC"; String newDate = new Test().modifyDateLayout(inputDate); System.out.println(newDate);}正确打印:
03.01.2010 21:32:27
(我在GMT-4上)
更新2: 根据您的编辑,您确实
ParseException对此有所了解。那么,最可疑的部分就是的时区
UTC。在您的Java环境中实际上
知道
吗?您正在使用什么Java版本和什么OS版本?检查
TimeZone.getAvailableIDs()。
UTC两者之间必须有一个。



