报错:Unsupported field: HourOfDay

摘要:
错误:不支持字段:HourOfDay是一个有趣的低级错误。代码如下:LocalDatenow=LocalDate。now();Stringyear=now.format(DateTimeFormatter.Pattern(“yyyy”));Stringhour=now.format(DateTimeFormatter.Pattern(“MM-dd HH”));

报错:Unsupported field: HourOfDay

这个错误就比较搞笑也比较低级了.

代码如下

LocalDate now = LocalDate.now();
String year = now.format(DateTimeFormatter.ofPattern("yyyy"));
String hour = now.format(DateTimeFormatter.ofPattern("MM-dd-HH"));

报错如下

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
	at java.time.LocalDate.get0(LocalDate.java:680)
	at java.time.LocalDate.getLong(LocalDate.java:659)
	at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
	at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2540)
	at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
	at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
	at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
	at java.time.LocalDate.format(LocalDate.java:1691)
	at com.feiyangshop.recommendation.HdfsHandler.main(HdfsHandler.java:21)

我还追踪到了jdk源码中查看.

private int get0(TemporalField field) {
        switch ((ChronoField) field) {
            case DAY_OF_WEEK: return getDayOfWeek().getValue();
            case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
            case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1;
            case DAY_OF_MONTH: return day;
            case DAY_OF_YEAR: return getDayOfYear();
            case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead");
            case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1;
            case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1;
            case MONTH_OF_YEAR: return month;
            case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead");
            case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year);
            case YEAR: return year;
            case ERA: return (year >= 1 ? 1 : 0);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }

最后把异常抛出的代码在这.

说明我在now.format(DateTimeFormatter.ofPattern("MM-dd-HH"));HH是不在case之中的.

为什么case中只有年月日,没有小时和分秒呢?

emmmmm.

答案就是:我用错类了

应该使用LocalDateTime这个类.这个类包含时分秒.

LocalDateTime now = LocalDateTime.now();
String year = now.format(DateTimeFormatter.ofPattern("yyyy"));
String hour = now.format(DateTimeFormatter.ofPattern("MM-dd-HH"));

FIN

免责声明:文章转载自《报错:Unsupported field: HourOfDay》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux 查看登录日志及登录失败用户的ip-lastbR语言:规划求解优化ROI下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

如何使用 window.open() 下载文件: 在本页面打开并防止浏览器拦截

从接口请求返回的内容有两种类型:直接返回文件内容、返回url和使用窗口。open()直接打开请求地址或返回的url,然后将请求地址或所返回的url直接分配给窗口的href1。直接返回文件内容:打开请求api地址downPkg(){letmywin=window.open('',mywin.location.href=apiPath;}2。返回url时:...

Java生鲜电商平台-优惠券系统设计详解

Java Fresh Food电子商务平台-优惠券系统的设计详细说明了优惠券的名称、面额、类型、有效期、使用范围、相互排斥以及作为电子商务最常用的营销手段的平台。用户在收到优惠券之前不知道优惠券的面值和使用限额,但只有在收到之后才知道优惠券的面额和使用限额。用户可在收到优惠券后3天内使用;商户A发放的优惠券只能在购买商户A的商品时使用,一台设备只能使用一张新...

使用 @Query 注解实现查询

2")ListfindByPriceRange;/***参数绑定@Param注解注入参数*/@QueryPersonreadPersonByLastNameAndEmailThroughName;//使用like@Query("selectpfromPersonpwherelastNamelike?...

从Excel中导入数据时,提示“未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序”的解决办法

具体下载地址:HTTP://www.microsoft.com/downloads/details.aspxFAMILYID=c06b8369-60dd-4b64-A44B-84b371ede16d&displayLang=ZH-CN对于一些早期用户,如果连接字符串中使用的是“Microsoft.Jet.OLEDB.4.0”,由于喷气项目已经停止,该项目不再...

winform中 跨线程启动UI

C#的winform程序中,是不可以从UI窗口主线程之外的线程去直接操作窗口控件的。确切的解释是,不能从创建控件的线程以外的线程去处理控件的操作,比如修改属性等。方法二,通过Control.Invoke调用委托的方式来执行。...

ios 苹果和百度地图的相关使用

同时由于苹果使用的是高德,不会像谷歌地图一样在国内乌龟一样的访问速度,确实做一些地图相关的东西,非常有吸引力。只是实现了显示一个百度地图的view。百度地图使用的是Objective-C++,这意味这必须要有一个.mm文件。...