Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用LocalDateTime分析DateTime时,无法从TemporalAccessor获取LocalDateTime异常_Java_Datetime_Java 8 - Fatal编程技术网

Java 使用LocalDateTime分析DateTime时,无法从TemporalAccessor获取LocalDateTime异常

Java 使用LocalDateTime分析DateTime时,无法从TemporalAccessor获取LocalDateTime异常,java,datetime,java-8,Java,Datetime,Java 8,分析DateTime时获取异常。这是我错过的东西吗 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("0DDDHHmmss"); DateTimeFormatter.ofPattern("0DDDHHmmss"); LocalDateTime date = LocalDateTime.parse("0365231109", formatter).withYear(2016); 以下是我得到的例外 Exception in t

分析DateTime时获取异常。这是我错过的东西吗

DateTimeFormatter formatter =  DateTimeFormatter.ofPattern("0DDDHHmmss");
DateTimeFormatter.ofPattern("0DDDHHmmss");
LocalDateTime date = LocalDateTime.parse("0365231109", formatter).withYear(2016);
以下是我得到的例外

Exception in thread "main" java.time.format.DateTimeParseException: Text '0365231109' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfYear=365},ISO resolved to 23:11:09 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at AutocomFDParser.main(AutocomFDParser.java:204)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfYear=365},ISO resolved to 23:11:09 of type java.time.format.Parsed
    at java.time.LocalDateTime.from(LocalDateTime.java:461)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {DayOfYear=365},ISO resolved to 23:11:09 of type java.time.format.Parsed
    at java.time.LocalDate.from(LocalDate.java:368)
    at java.time.LocalDateTime.from(LocalDateTime.java:456)

您没有在输入字符串中指定要转换为LocalDateTime的年份。

LocalDateTime
必须与年份关联。

因此会引发以下异常:

无法从临时助理获取LocalDate:{DayOfYear=365},ISO 已解析为23:11:09,类型为java.time.format.Parsed

您可以设置一个假日期作为输入,并使用以下内容覆盖它:
withYear(2016)

String stringInput = "02000365231109";
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("0yyyyDDDHHmmss");
LocalDateTime date2 = LocalDateTime.parse(stringInput, formatter2).withYear(2016);
如果无法直接修改输入,则可以在调用
parse()
方法之前创建格式正确的新字符串:

String stringInput = "0365231109";
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("0yyyyDDDHHmmss");
stringInput = "02000" + stringInput.substring(1);
LocalDateTime date2 = LocalDateTime.parse(stringInput, formatter2).withYear(2016);

首先你的约会时间模式更像是一个而不是一个。您可以使用从创建。例如:

List<TemporalField> fields = Arrays.asList(DAY_OF_YEAR, HOUR_OF_DAY
        , MINUTE_OF_HOUR, SECOND_OF_MINUTE);

Duration duration = DateTimeFormatter.ofPattern("0DDDHHmmss").parse("0366231109")
    .query(temporal -> fields.stream().reduce(
            Duration.ZERO,
            (it, field) -> it.plus(field.getFrom(temporal), field.getBaseUnit()),
            Duration::plus
    ));
LocalDateTime start=LocalDateTime.of(Year.of(2016).atDay(1),LocalTime.of(0, 0, 0));
LocalDateTime result = start.plus(duration);

System.out.println(result);
输出 “2017-01-01T23:11:09”


不客气。可以更具体一点。嗨,先生。第一轮投票。当
dayOfYears>366
时,使用您的方法将失败。OP日期模式更像是
持续时间
而不是
*日期时间
@holi java我同意使用持续时间实例和你的答案可以根据你解释OP问题的方式来确定,但这并不能清楚地说明OP需求的一部分。