当前位置:网站首页>解决报错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"));
边栏推荐
- Babbitt | Metaverse Daily Must Read: Seven consecutive quarters of losses, Meta Metaverse division Q2 loss of $ 2.8 billion, Zuckerberg said this situation may continue for years ...
- STC8h1k28六个基本实验
- NFTScan 与 PANews 联合发布多链 NFT 数据分析报告
- Recall i2i
- js选择多张图片对比功能插件
- 解析正则表达式的语法(二)
- leetcode53 -- 最大数组和
- 管理层换血,魏建军求变,长城能告别“命悬一线”吗?
- Database Project 01 Documentation: Database Skills Needed for Software Testing
- 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

Swagger

抗HER2/neu受体拟肽修饰的紫杉醇自蛋白纳米粒/环境敏感型多糖纳米粒的制备,

4G无线模块 电力通信模块

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

Out of breath!After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...

js选择多张图片对比功能插件

华中农大团队提出:一种基于异构网络的方法,可自动提取元路径,预测药物-靶标相互作用

Query term weights, search term weighting

leetcode94 -- 二叉树的中序遍历
随机推荐
The difference between Loadrunner and Jmeter is the same
[网络知识]交换STP
浅析无人机发展趋势以及如何实现EasyDSS+无人机视频推流?
js选择多张图片对比功能插件
浅谈智能家居应用及传输方式
巴比特 | 元宇宙每日必读:连续七个季度出现亏损,Meta元宇宙部门Q2亏损28 亿美元,扎克伯格称这种情况可能会持续数年...
不堆概念、换个角度聊多线程并发编程
DTSE Tech Talk丨第2期:解读云原生技术下,SaaS应用技术架构设计
[Network] Routing Routing Policy
When to use UserCF and when to use ItemCF?
Loadrunner与Jmeter区别与相同
直播实录 | 37 手游如何用 StarRocks 实现用户画像分析
Quantitative primary - akshare get the stock code, the minimalist strategy
Arduino框架下轻量级ssd1306 I2C屏幕驱动库
large number factorial calculation
[High Concurrency] I used multithreading to optimize the massive data proofreading system under the billion-level traffic e-commerce business, and the performance was directly improved by 200%!!(The w
PHP使用PDO连接sql server
[C语言刷题篇]链表运用讲解
【Translation】Device Manager—Intel NIC Properties Setting Advanced Options Function
CRM如何帮助企业营销获客