如何使用Java 8 time API将两位数字年份转换为全年 [英] How to convert two digit year to full year using Java 8 time API

查看:157
本文介绍了如何使用Java 8 time API将两位数字年份转换为全年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从我的项目中删除Joda-Time库.

I wish to remove the Joda-Time library from my project.

我正在尝试将两位数字年份转换为全年.以下来自Joda-Time的代码可以实现此目的.以下是joda-time的以下代码

I am trying to convert a two digit year to full year. The following code from Joda-Time can fulfil the purpose. Below is the following code of joda-time

DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormat.forPattern("yy");
int year = LocalDate.parse("99"", TWO_YEAR_FORMATTER).getYear();
System.out.println(year);

产出:1999年

这是我期望的输出,在我的情况下这是有意义的.但是,当我使用java.time API尝试相同的过程时,它会产生DatetimeParseException.下面是java.time API的以下代码:

This is the output that I expect and that makes sense in my situation. However, when I try the same procedure with java.time API, it produces a DatetimeParseException. Below is the following code of java.time API:

DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormatter.ofPattern("yy");
int year = LocalDate.parse("99", TWO_YEAR_FORMATTER).getYear();
System.out.println(year);

堆栈跟踪:

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

我看不到堆栈跟踪的原因.如果有人可以帮助我了解以下情况,并说明如何使用Java 8时间API将两位数字的年份转换为全年,那将是很好的选择.

I failed to see the reason of the stacktrace. It would be nice if someone could help me out to understand the following scenario and also explain how to convert a two digit year to full year using Java 8 time API.

推荐答案

问题是您无法单独将一年解析为LocalDate. LocalDate需要更多的信息.

The problem is that you can't parse a year on its own into a LocalDate. A LocalDate needs more information than that.

您可以使用格式化程序的parse方法,这将为您提供

You can use the parse method of the formatter, which will give you a TemporalAccessor, and then get the year field from that:

int year = TWO_YEAR_FORMATTER.parse("99").get(ChronoField.YEAR);
System.out.println(year);

解决两者之间的差异:这是两个不同的API.是的,它们非常相似,并且java.time程序包是根据JodaTime的设计决策而告知的,但它绝非意在替代它.

Addressing the discrepancy between the two: these are two distinct APIs. Yes, they are very similar, and the java.time package was informed by design decisions of JodaTime, but it was never intended to be a drop-in replacement for it.

请参见此答案如果您想更改枢轴年份(默认情况下,"99"将解析为2099,而不是1999).

See this answer if you would like to change the pivot year (by default '99' will resolve to 2099 and not 1999).

这篇关于如何使用Java 8 time API将两位数字年份转换为全年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆