Java日期时间API系列26

Java8中为年月新增了类YearMonth,可以用来表示卡片过期时间等问题。

1.YearMonth

默认格式为:2007-12

1.1 部分源码

* * @implSpec * This class is immutable and thread-safe. * * @since 1.8 */ public final class YearMonth implements Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable { /** * Serialization version. */ private static final long serialVersionUID = 4183400860270640070L; /** * Parser. */ private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder() .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral('-') .appendValue(MONTH_OF_YEAR, 2) .toFormatter(); /** * The year. */ private final int year; /** * The month-of-year, not null. */ private final int month;

通过源码可以看出使用final修饰YearMonth,YearMonth是线程安全类,同时实现了Temporal, TemporalAdjuster, Comparable<YearMonth>, Serializable接口,有属性读取,设置和加减等功能。

 1.2 创建和基本使用

YearMonth yearMonth = YearMonth.now(); YearMonth yearMonth2 = YearMonth.of(2020, 4);

1 System.out.println(yearMonth); 2 System.out.println(yearMonth2); 3 YearMonth yearMonth3 \= YearMonth.parse("2020-05"); 4 System.out.println(yearMonth3); 5 6 YearMonth yearMonth4 \= yearMonth3.plusMonths(1); 7 System.out.println(yearMonth4);

输出:

2020-03 2020-04 2020-05 2020-06

2.应用

2.1 转换

如Date转YearMonth,YearMonth转Date等

/** * Date转YearMonth * @param date * @return */ public static YearMonth toYearMonth(Date date){ LocalDate localDate = toLocalDate(date); return YearMonth.of(localDate.getYear(), localDate.getMonthValue()); } /** * LocalDateTime转YearMonth * @param localDateTime * @return */ public static YearMonth toYearMonth(LocalDateTime localDateTime){ LocalDate localDate = toLocalDate(localDateTime); return YearMonth.of(localDate.getYear(), localDate.getMonthValue()); } /** * LocalDate转YearMonth * @param localDate * @return */ public static YearMonth toYearMonth(LocalDate localDate){ Objects.requireNonNull(localDate, "localDate"); return YearMonth.of(localDate.getYear(), localDate.getMonthValue()); } /** * Instant转YearMonth * @param instant * @return */ public static YearMonth toYearMonth(Instant instant){ LocalDate localDate = toLocalDate(instant); return YearMonth.of(localDate.getYear(), localDate.getMonthValue()); } /** * ZonedDateTime转YearMonth * @param zonedDateTime * @return */ public static YearMonth toYearMonth(ZonedDateTime zonedDateTime){ LocalDate localDate = toLocalDate(zonedDateTime); return YearMonth.of(localDate.getYear(), localDate.getMonthValue()); } /** * YearMonth转Date * 注意dayOfMonth范围:1到31之间,最大值根据月份确定特殊情况,如2月闰年29,非闰年28 * 如果要转换为当月最后一天,可以使用下面方法:toDateEndOfMonth(YearMonth) * @param yearMonth * @param dayOfMonth * @return */ public static Date toDate(YearMonth yearMonth, int dayOfMonth) { Objects.requireNonNull(yearMonth, "yearMonth"); return toDate(yearMonth.atDay(dayOfMonth)); } /** * YearMonth转Date,转换为当月第一天 * @param yearMonth * @return */ public static Date toDateStartOfMonth(YearMonth yearMonth) { return toDate(yearMonth, 1); } /** * YearMonth转Date,转换为当月最后一天 * @param yearMonth * @return */ public static Date toDateEndOfMonth(YearMonth yearMonth) { Objects.requireNonNull(yearMonth, "yearMonth"); return toDate(yearMonth.atEndOfMonth()); } /** * YearMonth转LocalDate * 注意dayOfMonth范围:1到31之间,最大值根据月份确定特殊情况,如2月闰年29,非闰年28 * 如果要转换为当月最后一天,可以使用下面方法:toLocalDateEndOfMonth(YearMonth) * @param yearMonth * @param dayOfMonth * @return */ public static LocalDate toLocalDate(YearMonth yearMonth, int dayOfMonth) { Objects.requireNonNull(yearMonth, "yearMonth"); return yearMonth.atDay(dayOfMonth); } /** * YearMonth转LocalDate,转换为当月第一天 * @param yearMonth * @return */ public static LocalDate toLocalDateStartOfMonth(YearMonth yearMonth) { return toLocalDate(yearMonth, 1); } /** * YearMonth转LocalDate,转换为当月最后一天 * @param yearMonth * @return */ public static LocalDate toLocalDateEndOfMonth(YearMonth yearMonth) { Objects.requireNonNull(yearMonth, "yearMonth"); return yearMonth.atEndOfMonth(); }

测试代码:

@Test public void yearMonthConverterTest(){ System.out.println("===================yearMonthConverterTest====================="); Date date = new Date(); System.out.println(date); YearMonth yearMonth = DateTimeConverterUtil.toYearMonth(date); System.out.println(yearMonth);

1 Date date1 \= DateTimeConverterUtil.toDate(yearMonth, 15); //转换为当月第一天 2 Date date2 = DateTimeConverterUtil.toDateStartOfMonth(yearMonth); //转换为当月最后一天 3 Date date3 = DateTimeConverterUtil.toDateEndOfMonth(yearMonth); 4 System.out.println(date1); 5 System.out.println(date2); 6 System.out.println(date3); 7 8 LocalDate LocalDate1 \= DateTimeConverterUtil.toLocalDate(yearMonth, 15); //转换为当月第一天 9 LocalDate LocalDate2 = DateTimeConverterUtil.toLocalDateStartOfMonth(yearMonth); //转换为当月最后一天 10 LocalDate LocalDate3 = DateTimeConverterUtil.toLocalDateEndOfMonth(yearMonth); 11 System.out.println(LocalDate1); 12 System.out.println(LocalDate2); 13 System.out.println(LocalDate3); 14}

输出:

===================yearMonthConverterTest===================== Fri Mar 20 23:41:41 CST 2020 2020-03 Sun Mar 15 00:00:00 CST 2020 Sun Mar 01 00:00:00 CST 2020 Tue Mar 31 00:00:00 CST 2020 2020-03-15 2020-03-01 2020-03-31

2.2 计算

获取起始日期中间的日期列表,获取指定月份的日期列表,判断是否过期等等

/** * 获取指定区间的时间列表,包含起始 * @param startInclusive * @param endInclusive * @return */ public static List<LocalDateTime> getLocalDateTimeList(LocalDateTime startInclusive, LocalDateTime endInclusive){ Objects.requireNonNull(startInclusive, "startInclusive"); Objects.requireNonNull(endInclusive, "endInclusive"); if(startInclusive.isAfter(endInclusive)){ throw new DateTimeException("startInclusive must before or equal endInclusive!"); } List<LocalDateTime> localDateTimeList = new ArrayList<LocalDateTime>(); long days = betweenTotalDays(startInclusive, endInclusive)+1; for(long i=0; i<days; i++){ localDateTimeList.add(startInclusive.plusDays(i)); } return localDateTimeList; } /** * 获取指定区间的时间列表,包含起始 * @param startInclusive * @param endInclusive * @return */ public static List<LocalDate> getLocalDateList(LocalDate startInclusive, LocalDate endInclusive){ return getLocalDateTimeList(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endInclusive)).stream() .map(localDateTime -> localDateTime.toLocalDate()).collect(Collectors.toList()); } /** * 获取指定区间的时间列表,包含起始 * @param startInclusive * @param endInclusive * @return */ public static List<Date> getDateList(Date startInclusive, Date endInclusive){ return getLocalDateTimeList(DateTimeConverterUtil.toLocalDateTime(startInclusive), DateTimeConverterUtil.toLocalDateTime(endInclusive)).stream() .map(localDateTime -> DateTimeConverterUtil.toDate(localDateTime)).collect(Collectors.toList()); } /** * 获取指定年月的所有日期列表 * @param YearMonth * @return */ public static List<LocalDate> getLocalDateList(YearMonth yearMonth){ Objects.requireNonNull(yearMonth, "yearMonth"); List<LocalDate> localDateList = new ArrayList<LocalDate>(); long days = yearMonth.lengthOfMonth(); LocalDate localDate = DateTimeConverterUtil.toLocalDateStartOfMonth(yearMonth); for(long i=0; i<days; i++){ localDateList.add(plusDays(localDate, i)); } return localDateList; } /** * 获取指定年月的所有日期列表 * @param yearMonthStr yyyy-MM * @return */ public static List<LocalDate> getLocalDateList(String yearMonthStr){ Objects.requireNonNull(yearMonthStr, "yearMonthStr"); YearMonth yearMonth = YearMonth.parse(yearMonthStr); return getLocalDateList(yearMonth); } /** * 获取指定年月的所有日期列表 * @param yearMonth * @return */ public static List<LocalDateTime> getLocalDateTimeList(YearMonth yearMonth){ return getLocalDateList(yearMonth).stream() .map(localDate -> DateTimeConverterUtil.toLocalDateTime(localDate)).collect(Collectors.toList()); } /** * 获取指定年月的所有日期列表 * @param yearMonthStr yyyy-MM * @return */ public static List<LocalDateTime> getLocalDateTimeList(String yearMonthStr){ return getLocalDateList(yearMonthStr).stream() .map(localDate -> DateTimeConverterUtil.toLocalDateTime(localDate)).collect(Collectors.toList()); } /** * 获取指定年月的所有日期列表 * @param yearMonthStr yyyy-MM * @return */ public static List<Date> getDateList(String yearMonthStr){ return getLocalDateList(yearMonthStr).stream().map(localDate -> DateTimeConverterUtil.toDate(localDate)) .collect(Collectors.toList()); } /** * 判断是否过期,(输入年月小于当前年月) * @param yearMonth * @return */ public static boolean isExpiry(YearMonth yearMonth){ Objects.requireNonNull(yearMonth, "yearMonth"); if(yearMonth.isBefore(YearMonth.now())){ return true; } return false; } /** * 判断是否过期,(输入年月小于当前年月) * @param yearMonthStr yyyy-MM * @return */ public static boolean isExpiry(String yearMonthStr){ Objects.requireNonNull(yearMonthStr, "yearMonthStr"); YearMonth yearMonth = YearMonth.parse(yearMonthStr); return isExpiry(yearMonth); }

测试代码:

/** * yearMonth测试 */ @Test public void yearMonthTest(){ //是否过期 System.out.println(DateTimeCalculatorUtil.isExpiry("2020-03")); //获取指定年月的所有日期列表 List<Date> dateList = DateTimeCalculatorUtil.getDateList("2020-03"); dateList.stream().forEach(date->{ System.out.println(DateTimeFormatterUtil.formatToDateStr(date)); });

1 System.out.println("========================"); //获取指定区间的时间列表,包含起始 2 List<Date> dateList2 = DateTimeCalculatorUtil.getDateList(dateList.get(0), dateList.get(dateList.size()-1)); 3 dateList2.stream().forEach(date\->{ 4 System.out.println(DateTimeFormatterUtil.formatToDateStr(date)); 5 }); 6 7}

输出:

false 2020-03-01 2020-03-02 2020-03-03 2020-03-04 2020-03-05 2020-03-06 2020-03-07 2020-03-08 2020-03-09 2020-03-10 2020-03-11 2020-03-12 2020-03-13 2020-03-14 2020-03-15 2020-03-16 2020-03-17 2020-03-18 2020-03-19 2020-03-20 2020-03-21 2020-03-22 2020-03-23 2020-03-24 2020-03-25 2020-03-26 2020-03-27 2020-03-28 2020-03-29 2020-03-30 2020-03-31

2020-03-01 2020-03-02 2020-03-03 2020-03-04 2020-03-05 2020-03-06 2020-03-07 2020-03-08 2020-03-09 2020-03-10 2020-03-11 2020-03-12 2020-03-13 2020-03-14 2020-03-15 2020-03-16 2020-03-17 2020-03-18 2020-03-19 2020-03-20 2020-03-21 2020-03-22 2020-03-23 2020-03-24 2020-03-25 2020-03-26 2020-03-27 2020-03-28 2020-03-29 2020-03-30 2020-03-31

源代码地址:https://github.com/xkzhangsan/xk-time

点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

一篇文章带你了解JavaScript日期

日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用

皕杰报表(关于日期时间时分秒显示不出来)

在使用皕杰报表设计器时,数据据里面是日期型,但当你web预览时候,发现有日期时间类型的数据时分秒显示不出来,只有年月日能显示出来,时分秒显示为0:00:00。1.可以使用tochar解决,数据集用selecttochar(flowdate,"yyyyMMddHH:mm:ss")fromtablename2.也可以把数据库日期类型date改成timestamp