When the system time to package compilation environment is date 1970,the jar will run error:
Exception in thread "main" java.time.DateTimeException: Invalid value for MonthOfYear (valid
values 1 - 12): 0
at java.time.temporal.ValueRange.checkValidValue(ValueRange.java:311)
at java.time.temporal.ChronoField.checkValidValue(ChronoField.java:703)
at java.time.LocalDate.of(LocalDate.java:267)
at java.time.LocalDateTime.of(LocalDateTime.java:336)
at org.springframework.boot.loader.jar.CentralDirectoryFileHeader.decodeMsDosFormatDateTime(CentralDirectoryFileHeader.java:127)
at org.springframework.boot.loader.jar.CentralDirectoryFileHeader.getTime(CentralDirectoryFileHeader.java:116)
at org.springframework.boot.loader.jar.JarEntry.<init>(JarEntry.java:58)
at org.springframework.boot.loader.jar.JarFileEntries.getEntry(JarFileEntries.java:316)
at org.springframework.boot.loader.jar.JarFileEntries.access$400(JarFileEntries.java:48)
at org.springframework.boot.loader.jar.JarFileEntries$EntryIterator.next(JarFileEntries.java:366)
at org.springframework.boot.loader.jar.JarFileEntries$EntryIterator.next(JarFileEntries.java:350)
at org.springframework.boot.loader.jar.JarFile$2.nextElement(JarFile.java:204)
at org.springframework.boot.loader.jar.JarFile$2.nextElement(JarFile.java:195)
at org.springframework.boot.loader.archive.JarFileArchive$EntryIterator.next(JarFileArchive.java:189)
at org.springframework.boot.loader.archive.JarFileArchive$EntryIterator.next(JarFileArchive.java:174)
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:85)
at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:69)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
I find this is a bub, in CentralDirectoryFileHeader.decodeMsDosFormatDateTime when date is 1970, then date is 0,
like date is
“Thu Jan 01 08:00:00 GMT+08:00 1970”
long datetime is 0
so
private long decodeMsDosFormatDateTime(long datetime) {
LocalDateTime localDateTime = LocalDateTime.of(
(int) (((datetime >> 25) & 0x7f) + 1980), (int) ((datetime >> 21) & 0x0f),
(int) ((datetime >> 16) & 0x1f), (int) ((datetime >> 11) & 0x1f),
(int) ((datetime >> 5) & 0x3f), (int) ((datetime << 1) & 0x3e));
return localDateTime.toEpochSecond(
ZoneId.systemDefault().getRules().getOffset(localDateTime)) * 1000;
}
then year is 1980 ,month is 0 for jdk check month is error 0,so throw “Invalid value for MonthOfYear (valid values 1 - 12): 0”
Comment From: mbhave
@philwebb noticed that the month in LocalDateTime.of is between 1 to 12 whereas the deprecated Date constructor uses 0 to 11.
Comment From: nosan
FYI @philwebb, @mbhave
getTime method in CentralDirectoryFileHeader can be changed on:
long getTime() {
long datetime = Bytes.littleEndianValue(this.header, this.headerOffset + 12, 4);
int year = (int) (((datetime >> 25) & 0x7f) + 1980);
int month = (int) Math.min(Math.max(((datetime >> 21) & 0x0f), 1), 12);
int day = (int) Math.min(Math.max(((datetime >> 16) & 0x1f), 1), 31);
int hour = (int) Math.min(Math.max(((datetime >> 11) & 0x1f), 0), 23);
int minute = (int) Math.min(Math.max(((datetime >> 5) & 0x3f), 0), 59);
int second = (int) Math.min(Math.max(((datetime << 1) & 0x3e), 0), 59);
return ZonedDateTime.of(year, month, day, hour, minute, second, 0, ZoneId.systemDefault()).toInstant()
.toEpochMilli();
}
Comment From: philwebb
Thanks, we should do something like that but probably also extract the Math.min(Math.max(...)) calls to a util method.
Comment From: philwebb
Closing in favor of PR #19595. Thanks @nosan!
Comment From: venkatasreekanth
running into same issue, is there a work around?
Comment From: wilkinsona
@venkatasreekanth Please don't leave the same comment on multiple issues.
Comment From: Arpit2601
@wilkinsona we are using spring-boot-2.1.12 and facing the same issue. Is there a work around to solve this, or is updating spring-boot the only solution?
Comment From: wilkinsona
@Arpit2601 Please see https://github.com/spring-projects/spring-boot/pull/19595#issuecomment-974173112.