Java YearMonth 不会解析,但只能在我的电脑上?

Eth*_*LTB 1 java parsing java-time java-11

我一直在从事一个项目,需要我以这种方式解析字符串,我已经分离出了引发错误的小部分,


import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

public class TestingYearMonth {

    public static void main(String[] args) {
        
           YearMonth yearMonth = YearMonth.parse("Feb-17", DateTimeFormatter.ofPattern("MMM-yy"));
            System.out.println(yearMonth.getMonth() + " " + yearMonth.getYear());
        
    }
}
Run Code Online (Sandbox Code Playgroud)

我的老师运行完全相同的代码,它返回输出没有问题。但是当我运行它时出现以下错误

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Feb-17' could not be parsed at index 0
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    at java.base/java.time.YearMonth.parse(YearMonth.java:295)
    at com.ethanbradley.assignment6.TestingYearMonth.main(TestingYearMonth.java:10)
Run Code Online (Sandbox Code Playgroud)

我们检查了一下,我的 jdk 很好(jdk 11,亚马逊 coretto 版本),我真的不明白为什么这不起作用,请帮助哦明智的互联网人们......

Bas*_*que 6

指定一个Locale

\n

您的 JVM 的默认区域设置可能不会将 \xe2\x80\x9cFeb\xe2\x80\x9d 识别为二月。

\n

指定 aLocale以确定解析月份名称时使用的人类语言和文化规范。

\n
Locale locale = Locale.US ; \nDateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM-yy" , locale ) ;\nYearMonth yearMonth = YearMonth.parse( "Feb-17" , f );\n
Run Code Online (Sandbox Code Playgroud)\n

请参阅在 IdeOne.com 上实时运行的代码

\n

ISO 8601

\n

使用本地化文本进行数据交换是不明智的。我建议您向数据发布者介绍用于将日期时间值作为文本交换的ISO 8601标准格式。

\n

年月的标准格式是 YYYY-MM。例子:2017-02

\n

java.time类默认使用 ISO 8601 格式

\n
YearMonth.parse( "2017-02" )\n
Run Code Online (Sandbox Code Playgroud)\n