Deb*_*was 9 java 2-digit-year jodatime java-time
我希望从我的项目中删除Joda-Time库.
我想把两位数转换成全年.Joda-Time的以下代码可以实现此目的.以下是joda-time的以下代码
DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormat.forPattern("yy");
int year = LocalDate.parse("99"", TWO_YEAR_FORMATTER).getYear();
System.out.println(year);
Run Code Online (Sandbox Code Playgroud)
产出:1999年
这是我期望的输出,这在我的情况下是有意义的.但是,当我使用java.time API尝试相同的过程时,它会生成DatetimeParseException.以下是java.time API的以下代码:
DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormatter.ofPattern("yy");
int year = LocalDate.parse("99", TWO_YEAR_FORMATTER).getYear();
System.out.println(year);
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
Exception in thread "main" java.time.format.DateTimeParseException: Text '99' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2099},ISO of type java.time.format.Parsed
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at scratch.main(scratch.java:10)
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {Year=2099},ISO of type java.time.format.Parsed
at java.base/java.time.LocalDate.from(LocalDate.java:396)
at java.base/java.time.format.Parsed.query(Parsed.java:235)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
... 2 more
Run Code Online (Sandbox Code Playgroud)
我没能看到堆栈跟踪的原因.如果有人可以帮助我理解以下场景并且还解释如何使用Java 8时间API将两位数年份转换为全年,那将是很好的.
问题是你无法将一年自己解析为LocalDate.LocalDate需要更多信息.
您可以使用parseformatter 的方法,它将为您提供a TemporalAccessor,然后从中获取year字段:
int year = TWO_YEAR_FORMATTER.parse("99").get(ChronoField.YEAR);
System.out.println(year);
Run Code Online (Sandbox Code Playgroud)
解决两者之间的差异:这是两个不同的API.是的,它们非常相似,并且java.timeJodaTime的设计决定告知了包装,但它从来没有打算成为它的替代品.
如果您想更改枢轴年份,请参阅此答案(默认情况下,"99"将解析为2099而不是1999年).
您无法创建,LocalDate因为String您提供的内容不提供填写所有必填字段所需的信息.
您可以创建一个Year虽然
Year parsed = Year.parse("99", TWO_YEAR_FORMATTER);
int year = parsed.getValue();
Run Code Online (Sandbox Code Playgroud)
java.time似乎没有Joda-Time那样提供月份和月份的默认值。该消息说它不能LocalDate仅从一年中获得a ,或者相反,它缺少一个月和一天(不过,您可以提供自己的默认值,如Mikhail Kholodkov的答案所示)。通常,此行为在这里对我们有帮助:它提醒我们提供所需的所有值,并从代码中清楚表明是否使用了任何默认值以及使用了哪个默认值。
只需使用的Year类java.time。首先声明
public static final DateTimeFormatter TWO_YEAR_FORMATTER = new DateTimeFormatterBuilder()
.appendValueReduced(ChronoField.YEAR, 2, 2, 1950)
.toFormatter();
Run Code Online (Sandbox Code Playgroud)
然后使用
int year = Year.parse("99", TWO_YEAR_FORMATTER).getValue();
System.out.println(year);
Run Code Online (Sandbox Code Playgroud)
输出量
1999年
在我输入1950的位置插入您想要的基值,以指定距该年99年(含)的年份。如果您希望年份是过去的年份(包括今年),则可以使用:
private static final Year base = Year.now(ZoneId.of("Asia/Kolkata")).minusYears(99);
public static final DateTimeFormatter TWO_YEAR_FORMATTER = new DateTimeFormatterBuilder()
.appendValueReduced(ChronoField.YEAR, 2, 2, base.getValue())
.toFormatter();
Run Code Online (Sandbox Code Playgroud)
顺便说一句,不要相信我,但我认为Joda-Time使用当年减去30年作为基数或支点。如果是这样,则在最后一个代码段中使用30而不是99是最兼容的(因此也会给您您所期望的1999)。
| 归档时间: |
|
| 查看次数: |
1184 次 |
| 最近记录: |