2021-01-26 获取指定日期所在年/月/周中的第一天、最后一天、某天

整合hutool


    cn.hutool
    hutool-all
    5.4.0


测试

package cn.mb.test;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import cn.mb.util.DateTimeUtil;

/**
 * 

* DateUtil测试类 *

* * @author: bb * @createDate: 2021/1/26 */
public class DateUtilTest { public static void main(String[] args) { // 当前日期 DateTime today = DateUtil.date(); DateTime yesterday1 = DateUtil.yesterday(); DateTime yesterday2 = DateUtil.offsetDay(today, -1); System.out.println("昨天:" + yesterday1 + ", " + yesterday2); DateTime tomorrow1 = DateUtil.tomorrow(); DateTime tomorrow2 = DateUtil.offsetDay(today, 1); System.out.println("明天:" + tomorrow1 + ", " + tomorrow2); DateTime lastWeekToday1 = DateUtil.lastWeek(); DateTime lastWeekToday2 = DateUtil.offsetWeek(today, -1); System.out.println("上周今天:" + lastWeekToday1 + ", " + lastWeekToday2); DateTime lastMonthToday1 = DateUtil.lastMonth(); DateTime lastMonthToday2 = DateUtil.offsetMonth(today, -1); System.out.println("上月今天:" + lastMonthToday1 + ", " + lastMonthToday2); DateTime beginOfYear = DateUtil.beginOfYear(today); System.out.println("年第一天:" + beginOfYear); DateTime endOfYear = DateUtil.endOfYear(today); System.out.println("年最后一天:" + endOfYear); DateTime beginOfMonth = DateUtil.beginOfMonth(today); System.out.println("月第一天:" + beginOfMonth); DateTime endOfMonth = DateUtil.endOfMonth(today); System.out.println("月最后一天:" + endOfMonth); DateTime beginOfWeek = DateUtil.beginOfWeek(today); System.out.println("周第一天:" + beginOfWeek); DateTime endOfWeek = DateUtil.endOfWeek(today); System.out.println("周最后一天:" + endOfWeek); DateTime dayOfWeek = DateTimeUtil.getDayOfWeek(today, Week.FRIDAY); System.out.println("周某天:" + dayOfWeek); DateTime dayOfMonth = DateTimeUtil.getDayOfMonth(today, 15); System.out.println("月某天:" + dayOfMonth); DateTime dayOfYear = DateTimeUtil.getDayOfYear(today, 150); System.out.println("年某天:" + dayOfYear); // 时间差 long 天数差 = DateUtil.between(DateUtil.yesterday(), DateUtil.tomorrow(), DateUnit.DAY); System.out.println("天数差:" + 天数差); long 小时差 = DateUtil.between(DateUtil.yesterday(), DateUtil.tomorrow(), DateUnit.HOUR); System.out.println("小时差:" + 小时差); // 判断闰年 boolean isLeapYear = DateUtil.isLeapYear(2021); System.out.println("2021是否为闰年:" + isLeapYear); } }

DateTimeUtil

package cn.mb.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Month;
import cn.hutool.core.date.Week;
import cn.hutool.core.util.ObjectUtil;

/**
 * 

* 日期工具类 *

* * @author: bb * @createDate: 2021/1/25 */
public class DateTimeUtil { /** *

* 获取指定日期所在周的周几日期(周日至周六 == 1至7) *

* * @param date 指定日期 * @param day 周几 * @return cn.hutool.core.date.DateTime 日期 * @author guohaibin * @date 2021-01-26 11:17:13 */
public static DateTime getDayOfWeek(DateTime date, Week day) { if (ObjectUtil.isNull(date) || ObjectUtil.isNull(day)) throw new IllegalArgumentException("date or day is null"); Week dateWeek = date.dayOfWeekEnum(); if (dateWeek == day) return date; if (day == Week.MONDAY) return DateUtil.beginOfWeek(date); if (day == Week.SUNDAY) return DateUtil.endOfWeek(date); return DateUtil.offsetDay(date, day.getValue() - dateWeek.getValue()); } /** *

* 获取指定日期所在月中的第几天(0-31) *

* * @param date 指定日期 * @param day 第几天(0~31,0代表该月最后一天) * @return cn.hutool.core.date.DateTime 日期 * @author guohaibin * @date 2021-01-26 11:40:34 */
public static DateTime getDayOfMonth(DateTime date, int day) { if (ObjectUtil.isNull(date)) throw new IllegalArgumentException("date is null"); if (day < 0 || day > 31) throw new IllegalArgumentException("day is between 0 and 31"); Month month = date.monthEnum(); int totalDayOfMonth = month.getLastDay(date.isLeapYear()); if (day > totalDayOfMonth) { throw new IllegalArgumentException(totalDayOfMonth + " days at most in " + month); } if (date.dayOfMonth() == day) return date; if (day == 0) return DateUtil.endOfMonth(date); if (day == 1) return DateUtil.beginOfMonth(date); return DateUtil.offsetDay(date, day - date.dayOfMonth()); } /** *

* 获取指定日期所在年中的第几天(0-366) *

* * @param date 指定日期 * @param day 第几天(0~366,0代表该年最后一天) * @return cn.hutool.core.date.DateTime 日期 * @author guohaibin * @date 2021-01-26 11:40:34 */
public static DateTime getDayOfYear(DateTime date, int day) { if (ObjectUtil.isNull(date)) throw new IllegalArgumentException("date is null"); if (day < 0 || day > 366) throw new IllegalArgumentException("day is between 0 and 366"); int lengthOfYear = DateUtil.lengthOfYear(date.year()); if (day > lengthOfYear) { throw new IllegalArgumentException(lengthOfYear + " days at most in " + date.year()); } if (day == 1) return DateUtil.beginOfYear(date); if (day == 0) return DateUtil.endOfYear(date); return DateUtil.offsetDay(date, day - date.dayOfYear()); } }

你可能感兴趣的:(日期)