我正在尝试将DateTime从C#解析为Java。这是来自 java 调用"2018-08-02T14:24:40.040353"
的字符串。当我尝试解析它时,我收到以下错误Unparseable date: "2018-08-02T14:24:40.040353"
这是解析日期的方法
public static String dateFormater(String dateFromJSON, String
expectedFormat, String oldFormat) {
SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
Date date = null;
String convertedDate = null;
try {
date = dateFormat.parse(dateFromJSON);
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat(expectedFormat);
convertedDate = simpleDateFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return convertedDate;
}
你的日期看起来像一个ISO_LOCAL_DATE_TIME
,如果你使用的是Java8你可以使用java.time API并使用默认格式的LocalDateTime
,如下所示:
String dateString = "2018-08-02T14:24:40.040353";
LocalDateTime ldt = LocalDateTime.parse(dateString);
ldt.toString((:
2018-08-02T14:24:40.040353