日期时间解析异常;无法在索引 4 处解析文本“2018-10-18 00:00:00”

我很难找到解析日期的正确方法。


我收到的日期是以下格式的字符串:'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,但谁能告诉我如何解决?


慕盖茨4494581
浏览 800回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java