奇怪org.threeten.bp.DateTimeException抛出? [英] Weird org.threeten.bp.DateTimeException thrown?

查看:303
本文介绍了奇怪org.threeten.bp.DateTimeException抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code的工作就好了。今天突然我开始这个例外 - org.threeten.bp.DateTimeException:现场DAYOFMONTH无法打印作为值1872095944最大宽度为2
这是我简单的code:

My code was working just fine. Today suddenly I started getting this exception - org.threeten.bp.DateTimeException: Field DayOfMonth cannot be printed as the value 1872095944 max width is 2 This is my simple code :

LocalDateTime date = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd - MM - uuuu");
    String sDate = date.format(formatter);//EXCEPTION THROWN HERE

为什么这个问题突然?_爱
修改搜索结果
这似乎是一个中间的问题。它有时会崩溃和其他时间正常工作。没有线索,发生了什么。 A

Why this problem suddenly?

EDIT

This seems to be an intermediate problem. It crashes sometimes and works fine on other times. No clues as to what is happening. A

推荐答案

这是不太格式化的问题(这里只是一个症状),而是出了问题怎么的一个实例 LocalDateTime 创建。根本原因很简单,就是 LocalDateTime.now()似乎产生的完全摆脱在某些罕见的情况下边界的某一天的一个月。这个问题可能与此问题。

It is less a formatting problem (here just a symptom) but rather a problem how an instance of LocalDateTime is created. Root cause is simply that LocalDateTime.now() seems to produce a day-of-month completely out of bounds in some rare cases. This problem is probably related to this issue on the issue tracker of threeten-bp.

LocalDate.ofEpochDay(X)有时会返回一个错误的或非法的结果
  而不是抛出一个异常的x的值很大。对于
  例如,LocalDate.ofEpochDay(9223371671611556645L)返回日期
  与d.getDayOfMonth(负值),而不是抛出一个
  DateTimeException。

LocalDate.ofEpochDay(x) sometimes returns a wrong or illegal result instead of throwing an exception for large values of x . For instance, LocalDate.ofEpochDay(9223371671611556645L) returns a date with a negative value for d.getDayOfMonth() instead of throwing a DateTimeException .

请记住,该方法 NOW()必须做一个划时代的转换在后台,最后调用 LocalDate.ofEpochDay(...) 。所以,如果你的时钟米利斯自Unix纪元产生一个不同寻常的时代价值,那么这可能会影响 NOW(),太。而你的格式化仅仅通过有效地调用获取从 LocalDateTime 的某一天的月 getDayOfMonth()(真的通过现场在 TemporalAccessor访问)。该人士$ ​​C $ C问题:

Keep in mind that the method now() must do an epoch conversion in the background and finally call LocalDate.ofEpochDay(...). So if your clock produces an unusal epoch value in millis since Unix epoch then this can affect now(), too. And your formatter just fetches the day-of-month from your LocalDateTime by effectively calling getDayOfMonth() (really via field access in TemporalAccessor). The source code in question:

281     public static LocalDate ofEpochDay(long epochDay) { 
282         long zeroDay = epochDay + DAYS_0000_TO_1970; 
283         // find the march-based year 
284         zeroDay -= 60;  // adjust to 0000-03-01 so leap day is at end of four year cycle 
285         long adjust = 0; 
286         if (zeroDay < 0) { 
287             // adjust negative years to positive for calculation 
288             long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1; 
289             adjust = adjustCycles * 400; 
290             zeroDay += -adjustCycles * DAYS_PER_CYCLE; 
291         } 
292         long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE; 
293         long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400); 
294         if (doyEst < 0) { 
295             // fix estimate 
296             yearEst--; 
297             doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400); 
298         } 
299         yearEst += adjust;  // reset any negative year 
300         int marchDoy0 = (int) doyEst; 
301 

302         // convert march-based values back to january-based 
303         int marchMonth0 = (marchDoy0 * 5 + 2) / 153; 
304         int month = (marchMonth0 + 2) % 12 + 1; 
305         int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1; 
306         yearEst += marchMonth0 / 10; 
307 

308         // check year now we are certain it is correct 
309         int year = YEAR.checkValidIntValue(yearEst); 
310         return new LocalDate(year, month, dom); 
311     } 

最有趣,最可疑的是,的实际上只有一年进行验证,而不是一个月或一个月中某一天的。的确,看看包含四个部分这一离奇的结果由负字符(???)分隔:

Most interesting and suspicious is the fact that only the year is validated, but not the month or day-of-month. And indeed, have a look at this bizarre result containing four parts separated by minus-chars (???):

LocalDate d = LocalDate.ofEpochDay(9223371671611556645L);
System.out.println(d); // -999999999-02-0-30
System.out.println(d.getDayOfMonth()); // -30

显然,库code被打破了一些外来这可能是由你的时钟不幸产生划时代的天数。 我还测试了在Java中-8相同的code,具有相同的错误的结果。

边注:

通过我的图书馆 Time4J 帮助我分析,鉴于测试输入上面将产生每年远界在threeten-BP定义的,太(范围为-999999999至999999999):

By help of my library Time4J I have analyzed that given test input above would yield a year far out of bounds as defined in threeten-bp, too (range is -999999999 until +999999999):

PlainDate date = PlainDate.of(9223371671611556645L, EpochDays.UNIX);
// java.lang.IllegalArgumentException: Year out of range: 25252733927766555

我不太清楚你能做些什么来解决这个问题。

I am not quite sure what you can do to solve the problem.

第一件事肯定是记录你的时钟产生的所有投入,其中涉及到threeten-BP所观察到的错误行为,并做一些研究,为什么你的时钟去有时疯了。

关于threeten-BP(和Java-8!),你可以只是希望threeten-BP-项目组将尽快修复它的bug(或者说甲骨文!)。产生这些问题的输入可能是错的呢,所以你最好应该捕捉异常,并与额外的消息记录它的时钟是错误的(因为根本原因)。

About the bug in threeten-bp (and Java-8!), you can just hope that the threeten-bp-project team will soon fix it (or rather Oracle!). The input causing the problems is probably wrong anyway so you should best catch the exception and log it with the extra message that the clock is wrong (as root cause).

这篇关于奇怪org.threeten.bp.DateTimeException抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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