当前位置:网站首页>解决报错Unsupported field: HourOfDay
解决报错Unsupported field: HourOfDay
2022-07-29 17:21:00 【ᅟᅟᅟᅟᅟ】
报错: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"));
边栏推荐
猜你喜欢
传统渲染农场和云渲染农场选择哪个好?
不堆概念、换个角度聊多线程并发编程
脉冲风采|Committer 专访——腾讯工程师张大伟喊你吃“螃蟹”啦
一键搭建博客:如何使用WordPress插件搭建专属博客
The Huazhong Agricultural University team proposes: a heterogeneous network-based method that can automatically extract meta-paths and predict drug-target interactions
「硬核」labelme 图片中显示标签
4G无线模块 电力通信模块
UNIX Environment Advanced Programming Chapter 3
阿里最新发布的《Alibaba分布式系统速成笔记》PDF版,供下载
RocketQA: across batches negative sampling (cross - batch negatives), the denoising of strong negative cases of sampling (denoised hard negative from) and data to enhance (data augment
随机推荐
译文推荐 | 调试 BookKeeper 协议 - 无界 Ledger
Piotr`s Ants
Exchange the STP/network knowledge
澜舟孟子轻量化预训练模型技术实践
JupyterNotebook安装插件管理包过程中报错( AttributeError module ‘tornado.web‘ has no attribute ‘asynchronous‘ )
js选择多张图片对比功能插件
巴比特 | 元宇宙每日必读:连续七个季度出现亏损,Meta元宇宙部门Q2亏损28 亿美元,扎克伯格称这种情况可能会持续数年...
Arduino框架下轻量级ssd1306 I2C屏幕驱动库
回帖免责声明-转载
【微信小程序】零基础学 | 小程序语法
休息天的早晨感觉不到在休息
leetcode94 -- 二叉树的中序遍历
字典树(Trie)
hihoCoder#1037 : 数字三角形(DP)
不堆概念、换个角度聊多线程并发编程
Domino服务器SSL证书安装指南
“我“眼中的测试/开发程序员,预想与现实的碰撞......
[网络]WAN技术组播
"Record" MMDetection Introduction
The two armies clash