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 {
1Jan(1, "January", "一月", "一"), 2Feb(2, "February", "二月", "二"), 3Mar(3, "March", "三月", "三"), 4Apr(4, "April", "四月", "四"), 5May(5, "May", "五月", "五"), 6Jun(6, "June", "六月", "六"), 7Jul(7, "July", "七月", "七"), 8Aug(8, "August", "八月", "八"), 9Sep(9, "September", "九月", "九"), 10Oct(10, "October", "十月", "十"), 11Nov(11, "November", "十一月", "十一"), 12Dec(12, "December", "十二月", "十二"),; /\*\* \* 序号 \*/ 13private int code; /\*\* \* 英文全称 \*/ 14private String fullNameEn; /\*\* \* 中文全称 \*/ 15private String fullNameCn; /\*\* \* 中文简称 \*/ 16private String shortNameCn; private MonthNameEnum(int code, String fullNameEn, String fullNameCn, String shortNameCn) { this.code = code; this.fullNameEn = fullNameEn; this.fullNameCn = fullNameCn; this.shortNameCn = shortNameCn; 17} /\*\* \* 根据code查询月份名称枚举 18 \* @param code 19 \* @return 20 \*/ 21public static MonthNameEnum getByCode(int code){ if(code >=1 && code <= 12){ for(MonthNameEnum monthNameEnum : MonthNameEnum.values()){ if(monthNameEnum.getCode() == code){ return monthNameEnum; 22 } 23 } 24 } return null; 25} /\*\* \* 根据code查询月份英文简称 26 \* @param code 27 \* @return 28 \*/ 29public static String getShortNameEnByCode(int code){ 30 MonthNameEnum monthNameEnum \= getByCode(code); return monthNameEnum != null ? monthNameEnum.name() : null; 31} /\*\* \* 根据code查询月份英文全称 32 \* @param code 33 \* @return 34 \*/ 35public static String getFullNameEnByCode(int code){ 36 MonthNameEnum monthNameEnum \= getByCode(code); return monthNameEnum != null ? monthNameEnum.getFullNameEn() : null; 37} /\*\* \* 根据code查询月份中文全称 38 \* @param code 39 \* @return 40 \*/ 41public static String getFullNameCnByCode(int code){ 42 MonthNameEnum monthNameEnum \= getByCode(code); return monthNameEnum != null ? monthNameEnum.getFullNameCn() : null; 43} /\*\* \* 根据code查询月份中文 44 \* @param code 45 \* @return 46 \*/ 47public static String getShortNameCnByCode(int code){ 48 MonthNameEnum monthNameEnum \= getByCode(code); return monthNameEnum != null ? monthNameEnum.getShortNameCn() : null; 49} public int getCode() { return code; 50} public String getFullNameEn() { return fullNameEn; 51} public String getFullNameCn() { return fullNameCn; 52} public String getShortNameCn() { return shortNameCn; 53}
}
应用代码
/** * 获取月, 比如 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);
1 System.out.println(DateTimeCalculatorUtil.getMonth(date)); 2 System.out.println(DateTimeCalculatorUtil.getMonthEnLong(date)); 3 System.out.println(DateTimeCalculatorUtil.getMonthEnShort(date)); 4 System.out.println(DateTimeCalculatorUtil.getMonthCnLong(date)); 5 System.out.println(DateTimeCalculatorUtil.getMonthCnShort(date)); 6}
输出结果:
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 {
1Mon(1, "Monday", "星期一"), 2Tue(2, "Tuesday", "星期二"), 3Wed(3, "Wednesday", "星期三"), 4Thu(4, "Thursday", "星期四"), 5Fri(5, "Friday", "星期五"), 6Sat(6, "Saturday", "星期六"), 7Sun(7, "Sunday", "星期日"),; /\*\* \* 序号 \*/ 8private int code; /\*\* \* 英文全称 \*/ 9private String fullNameEn; /\*\* \* 中文 \*/ 10private String nameCn; private WeekNameEnum(int code, String fullNameEn, String nameCn) { this.code = code; this.fullNameEn = fullNameEn; this.nameCn = nameCn; 11} /\*\* \* 根据code查询星期名称枚举 12 \* @param code 13 \* @return 14 \*/ 15public static WeekNameEnum getByCode(int code){ if(code >=1 && code <= 12){ for(WeekNameEnum monthNameEnum : WeekNameEnum.values()){ if(monthNameEnum.getCode() == code){ return monthNameEnum; 16 } 17 } 18 } return null; 19} /\*\* \* 根据code查询星期英文简称 20 \* @param code 21 \* @return 22 \*/ 23public static String getShortNameEnByCode(int code){ 24 WeekNameEnum monthNameEnum \= getByCode(code); return monthNameEnum != null ? monthNameEnum.name() : null; 25} /\*\* \* 根据code查询星期英文全称 26 \* @param code 27 \* @return 28 \*/ 29public static String getFullNameEnByCode(int code){ 30 WeekNameEnum monthNameEnum \= getByCode(code); return monthNameEnum != null ? monthNameEnum.getFullNameEn() : null; 31} /\*\* \* 根据code查询星期中文名称 32 \* @param code 33 \* @return 34 \*/ 35public static String getNameCnByCode(int code){ 36 WeekNameEnum monthNameEnum \= getByCode(code); return monthNameEnum != null ? monthNameEnum.getNameCn() : null; 37} public int getCode() { return code; 38} public String getFullNameEn() { return fullNameEn; 39} public String getNameCn() { return nameCn; 40}
}
应用代码:
/** * 获取星期值 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);
1 System.out.println(DateTimeCalculatorUtil.getDayOfWeek(date)); 2 System.out.println(DateTimeCalculatorUtil.getDayOfWeekEnLong(date)); 3 System.out.println(DateTimeCalculatorUtil.getDayOfWeekEnShort(date)); 4 System.out.println(DateTimeCalculatorUtil.getDayOfWeekCn(date)); 5}
输出:
Thu Feb 27 16:09:00 CST 2020 4 Thursday Thu 星期四