报错: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的更多相关文章

  1. JDK1.8环境下依然报错 Unsupported major.minor version 52.0

    JDK1.8环境下依然报错 Unsupported major.minor version 52.0 在配置elasticsearch-rtf全文搜索引擎时,按照Github上项目readme.md来 ...

  2. SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found."

    SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'c ...

  3. 使用xadmin搜索search_fields报错:Related Field got invalid lookup: icontains

    一.问题描述 使用xadmin实现Django后台功能时,使用search_fields = [字段名,字段名],在搜索的时候报错Related Field got invalid lookup: i ...

  4. 从高版本JDK换成低版本JDK报错Unsupported major.minor version 52.0的解决方案

    从高版本JDK换成低版本JDK报错Unsupported major.minor version 52.0 java.lang.UnsupportedClassVersionError: PR/Sor ...

  5. 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '

    没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...

  6. Java报错--Unsupported major.minor version 52.0

    遇到一个Java相关的报错: ... java.lang.UnsupportedClassVersionError: ... : Unsupported major.minor version 52. ...

  7. Fish 下报错 Unsupported use of '||'. In fish, please use 'COMMAND; or COMMAND'.

    在用fish激活virualenv虚拟环境时,使用命令: source ./venv/bin/activate 报错 ./venv/bin/activate (line 23): Unsupporte ...

  8. 2. springboot启动报错:Field userMapper in com.service.UserService required a bean of type 'com.dao.UserMapper' that could not be found.

    报错信息: 2018-06-25 14:26:17.103  WARN 49752 --- [  restartedMain] ationConfigEmbeddedWebApplicationCon ...

  9. oracle 导入报错:field in data file exceeds maximum length

    今天用sqlldr导入数据时候报错: " Record 1: Rejected - Error on table ks_test, column khname.Field in data f ...

随机推荐

  1. Makefile目标文件搜索(VPATH和vpath

    转载:http://c.biancheng.net/view/7051.html 我们都知道一个工程文件中的源文件有很多,并且存放的位置可能不相同(工程中的文件会被放到不同的目录下),所以按照之前的方 ...

  2. Ubuntu virtualenv 创建 python3 虚拟环境 激活 退出

    首先默认安装了virtualenv 创建python3虚拟环境 your-name@node-name:~/virtual_env$ virtualenv -p /usr/bin/python3 py ...

  3. 『学了就忘』Linux基础命令 — 28、别名和常用快捷键

    目录 1.别名 2.常用快捷键 1.别名 别名也是Shell中的命令. 命令的别名,就是命令的小名,主要是用于照顾管理员使用习惯的. 命令格式: # 查询系统中命令别名 [root@localhost ...

  4. Linux内核内存检测工具KASAN

    KASAN 是 Kernel Address Sanitizer 的缩写,它是一个动态检测内存错误的工具,主要功能是检查内存越界访问和使用已释放的内存等问题.KASAN 集成在 Linux 内核中,随 ...

  5. 2021 羊城杯WriteUP

    比赛感受 题目质量挺不错的,不知道题目会不会上buu有机会复现一下,躺了个三等奖,发下队伍的wp Team BinX from GZHU web Checkin_Go 源码下载下来发现是go语言写的 ...

  6. java注解@Transactional事务类内调用不生效问题及解决办法

    @Transactional 内部调用例子 在 Spring 的 AOP 代理下,只有目标方法由外部调用,目标方法才由 Spring 生成的代理对象来管理,这会造成自调用问题.若同一类中的其他没有@T ...

  7. 环境(6)Linux文件系统二

    一:计算机间的数据传输 windows---linux : lrzsz  :需要手动安装 yum  install  lrzsz  -y   ;   rz  将文件从window上传到linux  : ...

  8. 9组-Ahlpa-6/3

    一.基本情况 队名:不行就摆了吧 组长博客:https://www.cnblogs.com/Microsoft-hc/p/15546622.html 小组人数: 8 二.冲刺概况汇报 卢浩玮 过去两天 ...

  9. Python基础(@property)

    class Point(object): # def get_score(self): # return self.score # def set_score(self,value): # if no ...

  10. java 邮件 接收与发送

    ... package com.e6soft; import java.io.BufferedReader; import java.io.FileOutputStream; import java. ...