Gow*_*esa 0 java datetime exception java-time localdate
I have a variable MINDATE in MyConstants file. You can see the declaration below.
public static final LocalDateTime MINDATE = LocalDateTime.of(LocalDate.of(2011, 1, 1), LocalTime.MIDNIGHT);
Run Code Online (Sandbox Code Playgroud)
I am consuming this variable in another class just by using MyConstants.MINDATE
then I get the following exception
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.cw.na.vos.DateTest.main(DateTest.java:14)
Caused by: java.lang.IllegalArgumentException: Unknown pattern letter: T
at java.time.format.DateTimeFormatterBuilder.parsePattern(Unknown Source)
at java.time.format.DateTimeFormatterBuilder.appendPattern(Unknown Source)
at java.time.format.DateTimeFormatter.ofPattern(Unknown Source)
at com.cw.na.vos.MyConstants.<clinit>(MyConstants.java:228)
... 1 more
Run Code Online (Sandbox Code Playgroud)
I am unable to understand the reason behind it.
public class DateTest {
static final LocalDateTime minD = LocalDateTime.of(LocalDate.of(2011, 1, 1), LocalTime.MIDNIGHT);
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.of(LocalDate.of(2011, 1, 1), LocalTime.MIDNIGHT);
System.out.println(minD); // success
System.out.println(ldt); //success
System.out.println(MyConstants.MINDATE); //ExceptionInInitializerError
}
}
Run Code Online (Sandbox Code Playgroud)
If I create the same variable in the class locally then it works but when I access similar LocalDateTime variable from different class then it throws an exception.
Need Help.
I will have to guess a little bit, but I think I know what your problem is. Assume you have for example:
public class MyConstants {
public static final LocalDateTime MINDATE
= LocalDateTime.of(LocalDate.of(2011, 1, 1), LocalTime.MIDNIGHT);
public static final DateTimeFormatter FORMATTER
= DateTimeFormatter.ofPattern("uuuu-MM-ddTHH:mm");
}
Run Code Online (Sandbox Code Playgroud)
Now when I do like you do:
System.out.println(MyConstants.MINDATE);
Run Code Online (Sandbox Code Playgroud)
I get an exception with a stacktrace that looks like yours:
Exception in thread "main" java.lang.ExceptionInInitializerError
at ovv.so.datetime.format.DateTest.main(DateTest.java:6)
Caused by: java.lang.IllegalArgumentException: Unknown pattern letter: T
at java.base/java.time.format.DateTimeFormatterBuilder.parsePattern(DateTimeFormatterBuilder.java:1800)
at java.base/java.time.format.DateTimeFormatterBuilder.appendPattern(DateTimeFormatterBuilder.java:1697)
at java.base/java.time.format.DateTimeFormatter.ofPattern(DateTimeFormatter.java:564)
at ovv.so.datetime.format.MyConstants.<clinit>(MyConstants.java:13)
... 1 more
Run Code Online (Sandbox Code Playgroud)
If I am guessing correctly, somewhere in MyConstants you are specifying a format pattern with a T in it, like I do above. The T in a format is a characteristic of ISO 8601 date-time formats. The T is a literal, not a format pattern letter like u, y, M, etc., so when you put it in the format pattern, it causes the exception.
First and best solution is if you can avoid writing your own format pattern altogether. The ISO 8601 formats are built in as DateTimeFormat.ISO_LOCAL_DATE_TIME, etc. Look for constants that start with ISO_, there are a couple of handfuls.
Second best quote the T in the format pattern:
public static final DateTimeFormatter FORMATTER
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm");
Run Code Online (Sandbox Code Playgroud)
Now your program runs and prints:
2011-01-01T00:00
From the documentation of ExceptionInInitializerError:
An
ExceptionInInitializerErroris thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.
静态变量(和常量)的初始化程序在加载该类时执行,这是在您第一次使用该类中的东西时发生的,在这种情况下,这是我们第一次引用MyConstants.MINDATE。幸运的是,此类错误通常与原因,引起该错误的原始异常相关联,因此,该原因以及发生原因的地方是用于调试的有趣信息。在您的情况下,它位于的第228行中MyConstants.java,在我的最小示例中,它位于第13行。因此,在这里可以查看和了解我们是否可以理解该消息
java.lang.IllegalArgumentException:未知的模式字母:T
| 归档时间: |
|
| 查看次数: |
274 次 |
| 最近记录: |