2021-01-26指定された日付が存在する年/月/週の最初の日、最後の日、ある日を取得

32200 ワード

統合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()); } }