Java日期时间API系列22-----Jdk8中java.time包中的新的日期时间API类,Month月份和DayOfWeek星期的计算。
Java8中为月份和星期新增的了,Month和DayOfWeek,来处理月份和星期的特殊问题,这2个类都是枚举类,对Month、DayOfWeek源码说明和简单应用,月份英文,月份英文简称,月份中文,星期英文,星期英文简称,星期中文等。
1.Month
1.1 部分源码:
* @implSpec
* This is an immutable and thread-safe enum.
*
* @since 1.8
*/
public enum Month implements TemporalAccessor, TemporalAdjuster { /**
* The singleton instance for the month of January with 31 days.
* This has the numeric value of {@code 1}.
*/
JANUARY,
/**
* The singleton instance for the month of February with 28 days, or 29 in a leap year.
* This has the numeric value of {@code 2}.
*/
FEBRUARY,
/**
* The singleton instance for the month of March with 31 days.
* This has the numeric value of {@code 3}.
*/
MARCH,
/**
* The singleton instance for the month of April with 30 days.
* This has the numeric value of {@code 4}.
*/
APRIL,
从中可以看出包含了12个月,因为实现了TemporalAccessor和TemporalAdjuster接口,它既有了属性读取和设置,又有加减等功能如下:

2.应用
为了方便中文环境应用,加一个月份名称的枚举。
package com.xkzhangsan.time.enums; /**
* 月份名称枚举,包含英文全称,英文检查,中文全称
*
* @ClassName: MonthNameEnum
* @Description: MonthNameEnum
* @author xkzhangsan
* @date 2020年02月27日
*/
public enum MonthNameEnum { Jan(1, "January", "一月", "一"),
Feb(2, "February", "二月", "二"),
Mar(3, "March", "三月", "三"),
Apr(4, "April", "四月", "四"),
May(5, "May", "五月", "五"),
Jun(6, "June", "六月", "六"),
Jul(7, "July", "七月", "七"),
Aug(8, "August", "八月", "八"),
Sep(9, "September", "九月", "九"),
Oct(10, "October", "十月", "十"),
Nov(11, "November", "十一月", "十一"),
Dec(12, "December", "十二月", "十二"),; /**
* 序号
*/
private int code; /**
* 英文全称
*/
private String fullNameEn; /**
* 中文全称
*/
private String fullNameCn; /**
* 中文简称
*/
private String shortNameCn; private MonthNameEnum(int code, String fullNameEn, String fullNameCn, String shortNameCn) {
this.code = code;
this.fullNameEn = fullNameEn;
this.fullNameCn = fullNameCn;
this.shortNameCn = shortNameCn;
} /**
* 根据code查询月份名称枚举
* @param code
* @return
*/
public static MonthNameEnum getByCode(int code){
if(code >=1 && code <= 12){
for(MonthNameEnum monthNameEnum : MonthNameEnum.values()){
if(monthNameEnum.getCode() == code){
return monthNameEnum;
}
}
}
return null;
} /**
* 根据code查询月份英文简称
* @param code
* @return
*/
public static String getShortNameEnByCode(int code){
MonthNameEnum monthNameEnum = getByCode(code);
return monthNameEnum != null ? monthNameEnum.name() : null;
} /**
* 根据code查询月份英文全称
* @param code
* @return
*/
public static String getFullNameEnByCode(int code){
MonthNameEnum monthNameEnum = getByCode(code);
return monthNameEnum != null ? monthNameEnum.getFullNameEn() : null;
} /**
* 根据code查询月份中文全称
* @param code
* @return
*/
public static String getFullNameCnByCode(int code){
MonthNameEnum monthNameEnum = getByCode(code);
return monthNameEnum != null ? monthNameEnum.getFullNameCn() : null;
} /**
* 根据code查询月份中文
* @param code
* @return
*/
public static String getShortNameCnByCode(int code){
MonthNameEnum monthNameEnum = getByCode(code);
return monthNameEnum != null ? monthNameEnum.getShortNameCn() : null;
} public int getCode() {
return code;
} public String getFullNameEn() {
return fullNameEn;
} public String getFullNameCn() {
return fullNameCn;
} public String getShortNameCn() {
return shortNameCn;
} }
应用代码
/**
* 获取月, 比如 1
* @param date
* @return
*/
public static int getMonth(Date date){
return DateTimeConverterUtil.toLocalDateTime(date).getMonthValue();
} /**
* 获取月, 比如 1
* @param instant
* @return
*/
public static int getMonth(Instant instant){
return DateTimeConverterUtil.toLocalDateTime(instant).getMonthValue();
} /**
* 获取月, 比如 1
* LocalDateTime LocalDate ZonedDateTime 可以直接getMonthValue()
* @param localDateTime
* @return
*/
public static int getMonth(LocalDateTime localDateTime){
Objects.requireNonNull(localDateTime, "localDateTime");
return localDateTime.getMonthValue();
} /**
* 获取月英文全称, 比如 January
* January, February, March, April, May, June, July, August, September, October,
* November and December
* @param date
* @return
*/
public static String getMonthEnLong(Date date){
return MonthNameEnum.getFullNameEnByCode(getMonth(date));
} /**
* 获取月英文全称, 比如 January
* January, February, March, April, May, June, July, August, September, October,
* November and December
* @param instant
* @return
*/
public static String getMonthEnLong(Instant instant){
return MonthNameEnum.getFullNameEnByCode(getMonth(instant));
} /**
* 获取月英文全称, 比如 January
* January, February, March, April, May, June, July, August, September, October,
* November and December
* LocalDateTime LocalDate ZonedDateTime 可以直接.getMonth().toString()
* @param localDateTime
* @return
*/
public static String getMonthEnLong(LocalDateTime localDateTime){
Objects.requireNonNull(localDateTime, "localDateTime");
return MonthNameEnum.getFullNameEnByCode(getMonth(localDateTime));
} /**
* 获取月英文简称, 比如 Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
* @param date
* @return
*/
public static String getMonthEnShort(Date date){
return MonthNameEnum.getShortNameEnByCode(getMonth(date));
} /**
* 获取月英文简称, 比如 Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
* @param instant
* @return
*/
public static String getMonthEnShort(Instant instant){
return MonthNameEnum.getShortNameEnByCode(getMonth(instant));
} /**
* 获取月英文简称, 比如 Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
* @param localDateTime
* @return
*/
public static String getMonthEnShort(LocalDateTime localDateTime){
Objects.requireNonNull(localDateTime, "localDateTime");
return MonthNameEnum.getShortNameEnByCode(getMonth(localDateTime));
} /**
* 获取月份中文全称, 比如一月
* @param date
* @return
*/
public static String getMonthCnLong(Date date){
return MonthNameEnum.getFullNameCnByCode(getMonth(date));
} /**
* 获取月份中文全称, 比如一月
* @param instant
* @return
*/
public static String getMonthCnLong(Instant instant){
return MonthNameEnum.getFullNameCnByCode(getMonth(instant));
} /**
* 获取月份中文全称, 比如一月
* @param localDateTime
* @return
*/
public static String getMonthCnLong(LocalDateTime localDateTime){
Objects.requireNonNull(localDateTime, "localDateTime");
return MonthNameEnum.getFullNameCnByCode(getMonth(localDateTime));
} /**
* 获取月份中文全称, 比如一
* @param date
* @return
*/
public static String getMonthCnShort(Date date){
return MonthNameEnum.getShortNameCnByCode(getMonth(date));
} /**
* 获取月份中文全称, 比如一
* @param instant
* @return
*/
public static String getMonthCnShort(Instant instant){
return MonthNameEnum.getShortNameCnByCode(getMonth(instant));
} /**
* 获取月份中文全称, 比如一
* @param localDateTime
* @return
*/
public static String getMonthCnShort(LocalDateTime localDateTime){
Objects.requireNonNull(localDateTime, "localDateTime");
return MonthNameEnum.getShortNameCnByCode(getMonth(localDateTime));
}
测试代码:
/**
* 获取月份信息,包括英文全称简称,中文等
*/
@Test
public void dateGetMonthTest(){
Date date = new Date();
System.out.println(date); System.out.println(DateTimeCalculatorUtil.getMonth(date));
System.out.println(DateTimeCalculatorUtil.getMonthEnLong(date));
System.out.println(DateTimeCalculatorUtil.getMonthEnShort(date));
System.out.println(DateTimeCalculatorUtil.getMonthCnLong(date));
System.out.println(DateTimeCalculatorUtil.getMonthCnShort(date));
}
输出结果:
Thu Feb 27 16:01:59 CST 2020
2
February
Feb
二月
二
2.DayOfWeek
2.1 部分源码
* @implSpec
* This is an immutable and thread-safe enum.
*
* @since 1.8
*/
public enum DayOfWeek implements TemporalAccessor, TemporalAdjuster { /**
* The singleton instance for the day-of-week of Monday.
* This has the numeric value of {@code 1}.
*/
MONDAY,
/**
* The singleton instance for the day-of-week of Tuesday.
* This has the numeric value of {@code 2}.
*/
TUESDAY,
/**
* The singleton instance for the day-of-week of Wednesday.
* This has the numeric value of {@code 3}.
*/
WEDNESDAY,
/**
* The singleton instance for the day-of-week of Thursday.
* This has the numeric value of {@code 4}.
*/
THURSDAY,
/**
* The singleton instance for the day-of-week of Friday.
* This has the numeric value of {@code 5}.
*/
FRIDAY,
/**
* The singleton instance for the day-of-week of Saturday.
* This has the numeric value of {@code 6}.
*/
SATURDAY,
/**
* The singleton instance for the day-of-week of Sunday.
* This has the numeric value of {@code 7}.
*/
SUNDAY;
从中可以看出包含了星期一到星期日,因为实现了TemporalAccessor和TemporalAdjuster接口,它它既有了属性读取和设置,又有加减等功能如下:

2.2 应用
为了方便中文环境应用,加一个星期名称的枚举。
package com.xkzhangsan.time.enums; /**
* 星期名称枚举,包含英文全称,英文检查,中文
* Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday
* @ClassName: WeekNameEnum
* @Description: WeekNameEnum
* @author xkzhangsan
* @date 2020年02月27日
*/
public enum WeekNameEnum { Mon(1, "Monday", "星期一"),
Tue(2, "Tuesday", "星期二"),
Wed(3, "Wednesday", "星期三"),
Thu(4, "Thursday", "星期四"),
Fri(5, "Friday", "星期五"),
Sat(6, "Saturday", "星期六"),
Sun(7, "Sunday", "星期日"),; /**
* 序号
*/
private int code; /**
* 英文全称
*/
private String fullNameEn; /**
* 中文
*/
private String nameCn; private WeekNameEnum(int code, String fullNameEn, String nameCn) {
this.code = code;
this.fullNameEn = fullNameEn;
this.nameCn = nameCn;
} /**
* 根据code查询星期名称枚举
* @param code
* @return
*/
public static WeekNameEnum getByCode(int code){
if(code >=1 && code <= 12){
for(WeekNameEnum monthNameEnum : WeekNameEnum.values()){
if(monthNameEnum.getCode() == code){
return monthNameEnum;
}
}
}
return null;
} /**
* 根据code查询星期英文简称
* @param code
* @return
*/
public static String getShortNameEnByCode(int code){
WeekNameEnum monthNameEnum = getByCode(code);
return monthNameEnum != null ? monthNameEnum.name() : null;
} /**
* 根据code查询星期英文全称
* @param code
* @return
*/
public static String getFullNameEnByCode(int code){
WeekNameEnum monthNameEnum = getByCode(code);
return monthNameEnum != null ? monthNameEnum.getFullNameEn() : null;
} /**
* 根据code查询星期中文名称
* @param code
* @return
*/
public static String getNameCnByCode(int code){
WeekNameEnum monthNameEnum = getByCode(code);
return monthNameEnum != null ? monthNameEnum.getNameCn() : null;
} public int getCode() {
return code;
} public String getFullNameEn() {
return fullNameEn;
} public String getNameCn() {
return nameCn;
} }
应用代码:
/**
* 获取星期值 1-7,星期一到星期日
* @param date
* @return
*/
public static int getDayOfWeek(Date date){
return DateTimeConverterUtil.toLocalDateTime(date).getDayOfWeek().getValue();
} /**
* 获取星期值 1-7,星期一到星期日
* @param localDateTime
* @return
*/
public static int getDayOfWeek(LocalDateTime localDateTime){
Objects.requireNonNull(localDateTime, "localDateTime");
return localDateTime.getDayOfWeek().getValue();
} /**
* 获取星期值 1-7,星期一到星期日
* @param localDate
* @return
*/
public static int getDayOfWeek(LocalDate localDate){
Objects.requireNonNull(localDate, "localDate");
return localDate.getDayOfWeek().getValue();
} /**
* 获取星期值 1-7,星期一到星期日
* @param instant
* @return
*/
public static int getDayOfWeek(Instant instant){
return DateTimeConverterUtil.toLocalDateTime(instant).getDayOfWeek().getValue();
} /**
* 获取星期英文全称,比如Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday
* @param date
* @return
*/
public static String getDayOfWeekEnLong(Date date){
return WeekNameEnum.getFullNameEnByCode(getDayOfWeek(date));
} /**
* 获取星期英文全称,比如Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday
* @param localDateTime
* @return
*/
public static String getDayOfWeekEnLong(LocalDateTime localDateTime){
Objects.requireNonNull(localDateTime, "localDateTime");
return WeekNameEnum.getFullNameEnByCode(getDayOfWeek(localDateTime));
} /**
* 获取星期英文全称,比如Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday
* @param localDate
* @return
*/
public static String getDayOfWeekEnLong(LocalDate localDate){
Objects.requireNonNull(localDate, "localDate");
return WeekNameEnum.getFullNameEnByCode(getDayOfWeek(localDate));
} /**
* 获取星期英文全称,比如Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday
* @param instant
* @return
*/
public static String getDayOfWeekEnLong(Instant instant){
return WeekNameEnum.getFullNameEnByCode(getDayOfWeek(instant));
} /**
* 获取星期英文简称,比如Mon
* @param date
* @return
*/
public static String getDayOfWeekEnShort(Date date){
return WeekNameEnum.getShortNameEnByCode(getDayOfWeek(date));
} /**
* 获取星期英文简称,比如Mon
* @param localDateTime
* @return
*/
public static String getDayOfWeekEnShort(LocalDateTime localDateTime){
Objects.requireNonNull(localDateTime, "localDateTime");
return WeekNameEnum.getShortNameEnByCode(getDayOfWeek(localDateTime));
} /**
* 获取星期英文简称,比如Mon
* @param localDate
* @return
*/
public static String getDayOfWeekEnShort(LocalDate localDate){
Objects.requireNonNull(localDate, "localDate");
return WeekNameEnum.getShortNameEnByCode(getDayOfWeek(localDate));
} /**
* 获取星期英文简称,比如Mon
* @param instant
* @return
*/
public static String getDayOfWeekEnShort(Instant instant){
return WeekNameEnum.getShortNameEnByCode(getDayOfWeek(instant));
} /**
* 获取星期中文,比如星期一
* @param date
* @return
*/
public static String getDayOfWeekCn(Date date){
return WeekNameEnum.getNameCnByCode(getDayOfWeek(date));
} /**
* 获取星期中文,比如星期一
* @param localDateTime
* @return
*/
public static String getDayOfWeekCn(LocalDateTime localDateTime){
Objects.requireNonNull(localDateTime, "localDateTime");
return WeekNameEnum.getNameCnByCode(getDayOfWeek(localDateTime));
} /**
* 获取星期中文,比如星期一
* @param localDate
* @return
*/
public static String getDayOfWeekCn(LocalDate localDate){
Objects.requireNonNull(localDate, "localDate");
return WeekNameEnum.getNameCnByCode(getDayOfWeek(localDate));
} /**
* 获取星期中文,比如星期一
* @param instant
* @return
*/
public static String getDayOfWeekCn(Instant instant){
return WeekNameEnum.getNameCnByCode(getDayOfWeek(instant));
}
测试代码:
/**
* 获取星期信息,包括英文全称简称,中文等
*/
@Test
public void dateGetWeekTest(){
Date date = new Date();
System.out.println(date); System.out.println(DateTimeCalculatorUtil.getDayOfWeek(date));
System.out.println(DateTimeCalculatorUtil.getDayOfWeekEnLong(date));
System.out.println(DateTimeCalculatorUtil.getDayOfWeekEnShort(date));
System.out.println(DateTimeCalculatorUtil.getDayOfWeekCn(date));
}
输出:
Thu Feb 27 16:09:00 CST 2020
4
Thursday
Thu
星期四
源代码地址:https://github.com/xkzhangsan/xk-time
Java日期时间API系列22-----Jdk8中java.time包中的新的日期时间API类,Month月份和DayOfWeek星期的计算。的更多相关文章
- 在swt中获取jar包中的文件 uri is not hierarchical
uri is not hierarchical 学习了:http://blog.csdn.net/zdsdiablo/article/details/1519719 在swt中获取jar包中的文件: ...
- 2020年Java多线程与并发系列22道高频面试题(附思维导图和答案解析)
前言 现在不管是大公司还是小公司,去面试都会问到多线程与并发编程的知识,大家面试的时候这方面的知识一定要提前做好储备. 关于多线程与并发的知识总结了一个思维导图,分享给大家 1.Java中实现多线程有 ...
- API接口自动化之3 同一个war包中多个接口做自动化测试
同一个war包中多个接口做自动化测试 一个接口用一个测试类,每个测试用例如下,比如下面是4个测试用例,每个详细的测试用例中含有请求入参,返回体校验,以此来判断每条测试用例是否通过 一个war包中,若含 ...
- Andriod项目开发实战(1)——如何在Eclipse中的一个包下建新包
最开始是想将各个类分门别类地存放在不同的包中,所以想在项目源码包中新建几个不同功能的包eg:utils.model.receiver等,最后的结果应该是下图左边这样的: 很明显建立项目后的架构是上 ...
- Mac 如何导出ipa文件中Assets.car包中的切图
在之前 获取 AppStore 中 应用 的 IPA 包文件(Mac OS 13+)中获取到应用的 IPA 包,可以取出应用的部分图片(如 Logo),如果项目工程中把图片添加到 Assets.xca ...
- JDK中的Atomic包中的类及使用
引言 Java从JDK1.5开始提供了java.util.concurrent.atomic包,方便程序员在多线程环境下,无锁的进行原子操作.原子变量的底层使用了处理器提供的原子指令,但是不同的CPU ...
- 【转】Eclipse中查看jar包中的源码
(简单的方式:通过jd-gui来进行反编译,最简单!,参考我的另一篇博文, 地址:http://www.cnblogs.com/gmq-sh/p/4277991.html) Java Decompil ...
- Package.json中dependencies依赖包中^符号和~符号前缀的区别
刚git了webpack的包发现package.json里面dependencies依赖包的版本号前面的符号有两种,一种是~,一种是^,如下图标记: 然后搜了下在stackoverflow上找到一个比 ...
- Java8系列 (六) 新的日期和时间API
概述 在Java8之前, 我们一般都是使用 SimpleDateFormat 来解析和格式化日期时间, 但它是线程不安全的. @Test public void test() { SimpleDate ...
- Java8 新的日期和时间API(笔记)
LocalDate LocalTime Instant duration以及Period 使用LocalDate和LocalTime //2017-03-20 LocalDate date = Loc ...
随机推荐
- Python 在PDF中添加、替换、或删除图片
PDF文件中的图片可以丰富文档内容,提升用户的阅读体验.除了在PDF中添加图片外,有时也需要替换或删除其中的图片,以改进视觉效果或更新信息.本文将提供以下三个示例,介绍如何使用Python 操作PDF ...
- 【POI】Excel数据导入
Postman请求方式: Controller接口代码: /** * /partImport/part/importUpload * @param importFile * @return */ @P ...
- 【Scala】05 对象特性Part2
特质重复继承关系 父类特质 A 子类特质B 继承 A 子类特质C 继承A 类D 继承了 B 又实现了 C class D extends B with C 继承顺序是 D 继承 C 继承 B 继承 A ...
- 买二手NVIDIA网卡被坑记录:某宝的咸鱼二手交易网站上购入NVIDIA Mellanox ConnectX-3 网卡 居然不支持Windows 11操作系统 —— 老二手40Gbps的NVIDIA网卡已经不被新操作系统支持
原本是打算去大连的人工智能计算中心去做技术负责人的,不过考虑到工作性质再考虑到自己的一些现实情况也就放弃了这个职位(比较在大连理工大学的博士学位还没有读下来,还是有所牵挂的).同时,由于自己已经退出了 ...
- “refer to”和“refer to as”在英语中的用法有所不同
"refer to"和"refer to as"在英语中的用法有所不同,具体区别如下: Refer to "Refer to"意为" ...
- 2024全球数字经济大会:大模型时代下DataOps驱动企业数智化升级
7月5日,以"开源生态筑基础,数字经济铸未来"为主题的2024全球数字经济大会在北京成功举办,来自全国各地的专家学者.企业代表.数据库行业从业人士及众多开源开发者,共聚一堂,共同探 ...
- 手把手教你ubuntu下移植MJPG-streamer
一.嵌入式视频图像开源库 在嵌入式系统中,常用的视频图像处理开源系统有:luvcview.cheese.motion.mjpg-streamer或者ffmpeg,其中: • luvcview: 基于V ...
- WCF实例管理
实例管理是对WCF使用的一系列技术的总称,通过它可以将客户端的请求绑定到服务实例上,并根据客户端请求的类型以确定服务实例的管理方式.由于应用程序在可扩展,性能,吞吐量,事物与对垒调用等方面存在巨大的差 ...
- 使用 SpanMetrics Connector 将 OpenTelemetry 跟踪转换为指标
原文:https://last9.io/blog/convert-opentelemetry-traces-to-metrics-using-spanconnector/ 如果您已经实施了跟踪但缺乏强 ...
- 【Python自动化】之运用Git+jenkins集成来运行展示pytest+allure测试报告
目录: 一.安装allure 二.生成allure报告 三.结合jenkins来集成pytest+allure 四.结合Git集成Jenkins+Pytest+Allure测试报告 五.附录 一.安装 ...