当前位置: 首页 > 知识库问答 >
问题:

无法在索引0处分析Java LocalDateTime文本异常

夹谷岳
2023-03-14
private DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
private String caseStartDate = dateFormat.format(LocalDateTime.now());

LocalDateTime localdatetime = LocalDateTime.parse(caseStartDate);

共有1个答案

颛孙哲
2023-03-14

您需要在LocalDateTime::Parse中使用格式,如下所示:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

class Main {
    public static void main(String[] args) {
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
        String caseStartDate = dateFormat.format(LocalDateTime.now());
        System.out.println(caseStartDate);
        LocalDateTime localdatetime = LocalDateTime.parse(caseStartDate, dateFormat);
        System.out.println(localdatetime);
    }
}

产出:

01/05/2020 09:13
2020-05-01T09:13

另外,请参阅如何重写LocalDateTimetoString()方法:

@Override
public String toString() {
    return date.toString() + 'T' + time.toString();
}
 类似资料: