问题
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);
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
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
}
}
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.
回答1:
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");
}
Now when I do like you do:
System.out.println(MyConstants.MINDATE);
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
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");
Now your program runs and prints:
2011-01-01T00:00
From the documentation of ExceptionInInitializerError
:
An
ExceptionInInitializerError
is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.
The initializers for static variables (and constants) are executed when the class is loaded, which happens the first time you use something from that class, in this case the first time we refer to MyConstants.MINDATE
. Fortunately such an error is typically linked with a cause, the original exception that caused it, so the cause and where that cause happened are the interesting pieces of information for debugging. In your case it was in line 228 of MyConstants.java
, in my minimal example it was line 13. So that is where to look and see if we can make sense of the message
java.lang.IllegalArgumentException: Unknown pattern letter: T
来源:https://stackoverflow.com/questions/52126124/final-static-localdatetime-retrieving-from-another-class-is-throwing-java-lang-e