Java日期时间API系列22

Wesley13
• 阅读 637

  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接口,它有了加减月份等功能如下:

Java日期时间API系列22

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接口,它有了加减星期等功能如下:

Java日期时间API系列22

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

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Stella981 Stella981
3年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
Java日期时间API系列23
  有时候,往往需要统计某个时间区间的销量等问题,这就需要准确的起始时间,获取准确开始时间00:00:00,获取准确结束时间23:59:59。下面增加了一一些方法,获取当天起始时间,昨天起始时间,当前月第一天开始时间,当前月最后一天结束时间,上个月第一天开始时间,上个月最后一天结束时间,某个指定月的起始结束时间等等。其中月份最后一天往往因为月份不同和
Stella981 Stella981
3年前
MiniUI日期选择框MonthPicker英文修改为中文
  一、详细内容及解决方案 正常MIniUI的MonthPicker中的月份和星期默认是英文的,我百度搜索关于这个问题的博客少之又少,下面说下解决办法,非常简单。<inputid"date1"class"minimonthpicker"value""allowInput"false"showTodayButton"fa
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
京东云开发者 京东云开发者
1个月前
Java 枚举与策略模式、函数式接口的结合:实现高内聚低耦合的设计
作者:京东物流杨唯一一、Java枚举类Java枚举是一个特殊的类,一般表示一组常量,比如一年的4个季节,一年的12个月份,一个星期的7天,方向有东南西北等。我们在业务需求开发中,通常会使用枚举来定义业务上的一组常量,那除了简单地定义常量之外,我们如何利用枚