Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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 OffsetDateTime.parse的Threeten DateTimeParseException(字符串,dateTimeFormatter)_Java_Threetenbp - Fatal编程技术网

Java OffsetDateTime.parse的Threeten DateTimeParseException(字符串,dateTimeFormatter)

Java OffsetDateTime.parse的Threeten DateTimeParseException(字符串,dateTimeFormatter),java,threetenbp,Java,Threetenbp,我只想制作一个DateTimeFormatter用于OffsetDateTime解析器。但是我得到了DateTimeParseException: final DateTimeFormatter ISO_LOCAL_DATE; ISO_LOCAL_DATE = new DateTimeFormatterBuilder() .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral('-') .append

我只想制作一个
DateTimeFormatter
用于
OffsetDateTime
解析器。但是我得到了
DateTimeParseException

final DateTimeFormatter ISO_LOCAL_DATE;
ISO_LOCAL_DATE = new DateTimeFormatterBuilder()
    .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
    .appendLiteral('-')
    .appendValue(MONTH_OF_YEAR, 2)
    .appendLiteral('-')
    .appendValue(DAY_OF_MONTH, 2)
    .appendLiteral('T')
    .appendValue(HOUR_OF_DAY,2)
    .appendLiteral(':')
    .appendValue(MINUTE_OF_HOUR,2)
    .appendLiteral(':')
    .appendValue(SECOND_OF_MINUTE,2)
    .toFormatter().withResolverStyle(ResolverStyle.STRICT).withChronology(IsoChronology.INSTANCE);

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
OffsetDateTime.parse("2012-03-06T00:00:00",ISO_LOCAL_DATE);
我调查了一个类似的问题,但也没有任何线索:/
上面的代码有什么问题?与
格式化程序
或Threeten lib有关?

您没有在输入数据中指定偏移量

这是带有偏移量的日期时间示例:

2012-03-06T00:00+01:00
ZoneDateTime
示例:

2012-03-06T00:00+02:00[Europe/Paris]
Europe/Berlin
——在这里可以被视为
ZoneId
。但对于一年中的不同时间(夏季/冬季),每个分区可能有不同的偏移量

ZoneId
ZoneOffset
之间没有一对一的映射:

您可以指定
ZoneId
而不是指定
ZoneOffset
,偏移量将自动确定

然后可以获取
ZonedDateTime
,并将其转换为
OffsetDateTime

这将使用本地日期时间和偏移量创建偏移日期时间。 区域ID将被忽略

为您的案例指定
ZoneId
的修复程序:

public class Main {
    private static final DateTimeFormatter ISO_LOCAL_DATE = new DateTimeFormatterBuilder()
            .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
            .appendLiteral('-')
            .appendValue(MONTH_OF_YEAR, 2)
            .appendLiteral('-')
            .appendValue(DAY_OF_MONTH, 2)
            .appendLiteral('T')
            .appendValue(HOUR_OF_DAY,2)
            .appendLiteral(':')
            .appendValue(MINUTE_OF_HOUR,2)
            .appendLiteral(':')
            .appendValue(SECOND_OF_MINUTE,2)
            .toFormatter()
            .withResolverStyle(ResolverStyle.STRICT)
            .withChronology(IsoChronology.INSTANCE)
            .withZone(ZoneId.systemDefault()); // or whatever you have

    public static void main(String[] args) {
        ZonedDateTime zonedDateTime = ZonedDateTime.parse("2012-03-06T00:00:00", ISO_LOCAL_DATE);
        System.out.println(zonedDateTime);
        System.out.println(zonedDateTime.toOffsetDateTime());
    }
}
近似输出:

2012-03-06T00:00+01:00[Europe/City]
2012-03-06T00:00+01:00
修复的第二个选项-将
offsetId()
添加到解析器生成器中,并为输入字符串指定偏移部分:

public class Main {
    private static final DateTimeFormatter ISO_LOCAL_DATE = new DateTimeFormatterBuilder()
            .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
            .appendLiteral('-')
            .appendValue(MONTH_OF_YEAR, 2)
            .appendLiteral('-')
            .appendValue(DAY_OF_MONTH, 2)
            .appendLiteral('T')
            .appendValue(HOUR_OF_DAY,2)
            .appendLiteral(':')
            .appendValue(MINUTE_OF_HOUR,2)
            .appendLiteral(':')
            .appendValue(SECOND_OF_MINUTE,2)
            .appendOffsetId()
            .toFormatter()
            .withResolverStyle(ResolverStyle.STRICT)
            .withChronology(IsoChronology.INSTANCE);

    public static void main(String[] args) {
        OffsetDateTime offsetDateTime = OffsetDateTime.parse("2012-03-06T00:00:00+02:00", ISO_LOCAL_DATE);
        System.out.println(offsetDateTime);
    }
}
输出:

2012-03-06T00:00+02:00
2012-03-06T00:00+02:00
2012-03-06T00:00+02:00
2012-03-06T00:00+02:00
您可以指定自己的偏移模式,而不是
。appendOffsetId()
类似:

.appendOffset("+HH:mm", "Z")
顺便说一句,有很多标准的
DateTimeFormatter
s,您可以使用它们来分析
OffsetDateTime

public class Main {
    public static void main(String[] args) {
        String offsetStringTime = "2012-03-06T00:00:00+02:00";
        OffsetDateTime offsetDateTime = OffsetDateTime.parse(offsetStringTime);
        OffsetDateTime offsetDateTime2 = OffsetDateTime.parse(offsetStringTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        OffsetDateTime offsetDateTime3 = OffsetDateTime.parse(offsetStringTime, DateTimeFormatter.ISO_ZONED_DATE_TIME);
        System.out.println(offsetDateTime);
        System.out.println(offsetDateTime2);
        System.out.println(offsetDateTime3);
    }
}
输出:

2012-03-06T00:00+02:00
2012-03-06T00:00+02:00
2012-03-06T00:00+02:00
2012-03-06T00:00+02:00

你应该分享你的StackTrace它似乎在模仿预定义的
ISO\u LOCAL\u DATE\u TIME
,并给它命名为
ISO\u LOCAL\u DATE
。那太令人困惑了。
java中的“Local”。time
names的意思是“没有时区或偏移量”,这可能会给你一个提示。请告诉我,你想要的结果是什么,为什么?可能是卓越的复制品的近似复制品!你说得对。但我真正需要的是一个有效的格式化程序,用于OffsetDateTime解析器,这是我力所能及的,我只能为它设置一个格式化程序。有可能从ZoneDateTime获取格式化程序吗?!由于ISO_LOCAL_日期对OffsetDateTime解析器无效。@user737862添加了一个示例,请参考更新的答案。@user737862我可能在重复编辑过的答案中已有的内容,但只需将格式设置为
DateTimeFormatter.ISO_OFFSET_DATE_TIME
。没有理由为它构建自己的格式化程序。