【发布时间】:2019-02-17 07:59:34
【问题描述】:
我很难找到解析日期的正确方法。
我收到以下格式的字符串形式的日期:'2018-10-18 00:00:00'
我需要将其转换为 18/10/2018 并存储在变量 startDate 中
然后我需要一个新变量来保存 endDate 变量,以便将日期向前滚动一周。
我的代码:
public String getStartDate(String startDate){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localStartDate = LocalDate.parse(startDate, formatter);
String startDateFormatted = localStartDate.format(formatter);
return startDateFormatted;
}
public LocalDate getEndDate(String startDate) {
LocalDate localEndDate = LocalDate.parse(getStartDate(startDate)).plusDays(7);
return localEndDate;
}
我的错误是:
java.time.format.DateTimeParseException: Text '2018-10-18 00:00:00' could
not be parsed at index 4
索引 4 建议使用“-”字符。不确定用于删除原始字符串中的 ISO 时间格式的格式化程序模式
我现在正在阅读 Javadocs,但谁能告诉我如何解决?
【问题讨论】:
-
首先您需要使用正确的格式化程序对其进行解析,然后将其格式化为您想要的最终格式。