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
。没有理由为它构建自己的格式化程序。