java获取给定日期当前周当前月当前季度的第一天和最后一天

实现方式分别采用jdk7的Calendar和jdk8的LocalDate

Calendar实现
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 根据给定时间获取周、月度、季度、年度开始结束时间
 * @author wsp
 */
public class DateCalendarUtil {

    /**
     * @Description: 获取月份第一天
     * @Author: wsp
     **/
    public static Calendar getMonthStart(Date date) { // 月份开始
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(date);
        //set方法将给定日历字段设置为给定值
        //下面代码作用是将日历设置为给定日期月份天数的第一天
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        return startCalendar;
    }

    /**
     * @Description: 获取月份最后一天
     * @Author: wsp
     **/
    public static Calendar getMonthEnd(Date date) { // 月份结束
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(date);
        //getActualMaximum方法用来返回给定日历字段属性的最大值
        //endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) 代码作用为返回日历该月份的最大值
        //set方法,将该日历的给定月份设置为最大天数
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return endCalendar;
    }

    /**
     * @Description: 获取季度第一天
     * 1.根据给定日期计算当前季度的第一个月份
     * 2.设置日历的月份为当前季度的第一个月份
     * 3.最后设置日历月份天数为第一天即可
     * @Author: wsp
     **/
    public static Calendar getQuarterStart(Date date) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(date);
        //get方法:获取给定日历属性的值,如 endCalendar.get(Calendar.MONTH) 获取日历的月份
        //计算季度数:由于月份从0开始,即1月份的Calendar.MONTH值为0,所以计算季度的第一个月份只需 月份 / 3 * 3
        startCalendar.set(Calendar.MONTH, (((int) startCalendar.get(Calendar.MONTH)) / 3) * 3);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        return startCalendar;
    }

    /**
     * @Description: 获取季度最后一天
     * @Author: wsp
     **/
    public static Calendar getQuarterEnd(Date date) { // 季度结束
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(date);
        //计算季度数:由于月份从0开始,即1月份的Calendar.MONTH值为0,所以计算季度的第三个月份只需 月份 / 3 * 3 + 2
        endCalendar.set(Calendar.MONTH, (((int) endCalendar.get(Calendar.MONTH)) / 3) * 3 + 2);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return endCalendar;
    }

    /**
     * @Description: 获取年份第一天
     * @Author: wsp
     **/
    public static Calendar getYearStart(Date date) { // 年份开始
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(date);
        startCalendar.set(Calendar.DAY_OF_YEAR, 1);
        return startCalendar;
    }

    /**
     * @Description: 获取年份最后一天
     * @Author: wsp
     **/
    public static Calendar getYearEnd(Date date) { // 年份结束
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(date);
        endCalendar.set(Calendar.DAY_OF_YEAR, endCalendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        return endCalendar;
    }

    /**
     * @Description: 获取当前周第一天(周一)
     * @Author: wsp
     **/
    public static Date getWeekStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //获取传入日期属于星期几,根据星期几进行不同处理
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        //周日:需要减去6天为周一。(当传入日期为周日时,若我们直接设置日历天数为周一,则日期会变为下一周的周一,而非当前周)
        if (dayOfWeek == 1) {
            calendar.add(Calendar.DAY_OF_MONTH, -6);
        } else {
            //周二 至 周六:直接获取周一即可
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        }
        return calendar.getTime();
    }

    /**
     * @Description: 获取当前周最后一天(周日)
     * @Author: wsp
     **/
    public static Date getWeekEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getWeekStart(date));
        calendar.add(Calendar.DAY_OF_MONTH, 6);
        return calendar.getTime();
    }

    public static void main(String[] args) throws ParseException {
        String dateStr = "2021-10-17";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateStr);

        String startMonthDay = sdf.format(getMonthStart(date).getTime());
        String endMonthDay = sdf.format(getMonthEnd(date).getTime());
        System.out.printf("月份的第一天:%s, 月份的最后一天:%s%n", startMonthDay, endMonthDay);

        String startQuarterDay = sdf.format(getQuarterStart(date).getTime());
        String endQuarterDay = sdf.format(getQuarterEnd(date).getTime());
        System.out.printf("季度第一天:%s, 季度最后一天:%s%n", startQuarterDay, endQuarterDay);

        String startYearDay = sdf.format(getYearStart(date).getTime());
        String endYearDay = sdf.format(getYearEnd(date).getTime());
        System.out.printf("年份第一天:%s, 年份最后一天:%s%n", startYearDay, endYearDay);

        String startWeekDay = sdf.format(getWeekStart(date));
        String endWeekDay = sdf.format(getWeekEnd(date));
        System.out.printf("当前周周一:%s, 当前周周日:%s%n", startWeekDay, endWeekDay);
    }
}
输出:
月份的第一天:2021-10-01, 月份的最后一天:2021-10-31
季度第一天:2021-10-01, 季度最后一天:2021-12-31
年份第一天:2021-01-01, 年份最后一天:2021-12-31
当前周周一:2021-10-11, 当前周周日:2021-10-17
LocalDate实现
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
public class DateUtil {

    /**
     * @Description: 获取季度第一天
     * @Author: wsp
     **/
    public static LocalDate getQuarterStart(LocalDate localDate) {
        //设置日期天数为第一天
        localDate = localDate.withDayOfMonth(1);
        long month = localDate.getMonth().getValue();
        if (1 <= month && month <= 3) {
            localDate = localDate.withMonth(1);
        } else if (4 <= month && month <= 6) {
            localDate = localDate.withMonth(4);
        } else if (7 <= month && month <= 9) {
            localDate = localDate.withMonth(7);
        } else {
            localDate = localDate.withMonth(10);
        }
        return localDate;
    }

    /**
     * @Description: 获取季度最后一天
     * @Author: wsp
     **/
    private static LocalDate getQuarterEnd(LocalDate localDate) {
        //设置日期天数为最后一天
        localDate = localDate.withDayOfMonth(localDate.lengthOfMonth());
        long month = localDate.getMonth().getValue();
        if (1 <= month && month <= 3) {
            localDate = localDate.withMonth(3);
        } else if (4 <= month && month <= 6) {
            localDate = localDate.withMonth(6);
        } else if (7 <= month && month <= 9) {
            localDate = localDate.withMonth(9);
        } else {
            localDate = localDate.withMonth(12);
        }
        return localDate;
    }

    public static void main(String[] args) {
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.of(2021, 10, 17);

        //周
        LocalDate startWeekDay = localDate.with(ChronoField.DAY_OF_WEEK, 1);
        LocalDate endWeekDay = localDate.with(ChronoField.DAY_OF_WEEK, 7);
        System.out.printf("当前周周一:%s, 当前周周日:%s%n", format.format(startWeekDay), format.format(endWeekDay));

        //月份
        LocalDate startMonthDay = localDate.with(ChronoField.DAY_OF_MONTH, 1);
        //LocalDate endMonthDay = localDate.with(ChronoField.DAY_OF_MONTH, localDate.lengthOfMonth());
        LocalDate endMonthDay = localDate.withDayOfMonth(localDate.lengthOfMonth());
        System.out.printf("月份的第一天:%s, 月份的最后一天:%s%n", format.format(startMonthDay), format.format(endMonthDay));

        //季度
        LocalDate startQuarterDay = getQuarterStart(localDate);
        LocalDate endQuarterDay = getQuarterEnd(localDate);
        System.out.printf("季度第一天:%s, 季度最后一天:%s%n", format.format(startQuarterDay), format.format(endQuarterDay));

        //年份
        LocalDate startYearDay = localDate.with(ChronoField.DAY_OF_YEAR, 1);
        LocalDate endYearDay = localDate.with(ChronoField.DAY_OF_YEAR, localDate.lengthOfYear());
        System.out.printf("年份第一天:%s, 年份最后一天:%s%n", format.format(startYearDay), format.format(endYearDay));
    }

}
输出:
当前周周一:2021-10-11, 当前周周日:2021-10-17
月份的第一天:2021-10-01, 月份的最后一天:2021-10-31
季度第一天:2021-10-01, 季度最后一天:2021-12-31
年份第一天:2021-01-01, 年份最后一天:2021-12-31
总结:

​ 可以看到使用jdk8的LocalDate类简单很多,也更容易理解。

你可能感兴趣的:(java,开发语言,后端)