铁马冰河2000

导航

日历工具CalendarUtil

==============================Calendar日历工具类

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CalendarUtil {
    
    /**
     * 获取开始日和结束日之前的天数(保留2位小数,超过2位直接舍弃)
     * @param startDate
     * @param endDate
     * @return
     */
    public static BigDecimal bigDeciBetweenDays(Date startDate, Date endDate){
        BigDecimal startTime = new BigDecimal(startDate.getTime());
        BigDecimal endTime = new BigDecimal(endDate.getTime());
        BigDecimal subtract = endTime.subtract(startTime);
        //BigDecimal.ROUND_DOWN 直接截取,不进位
        return subtract.divide(new BigDecimal(1000*60*60*24), 2, BigDecimal.ROUND_DOWN);
    }
    
    /**
     * 获取开始日和结束日之间的日期列表(包含开始和结束日)
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<Date> listBetweenDays(Date startDate, Date endDate){
        List<Date> dayList = new ArrayList<>();
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startDate);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);
        //包含开始日
        dayList.add(startDate);
        while(startCal.before(endCal)) {
            startCal.add(Calendar.DAY_OF_YEAR, 1);
            dayList.add(startCal.getTime());
        }
        return dayList;
    }
    
    /********************************************年&天yearDay***********************************************/
    
    /**
     * 当年的开始和结束日
     * @param date
     * @return
     */
    public static List<Date> startAndEndForYear(Date date){
        List<Date> dayList = new ArrayList<>();
        dayList.add(startDayForYear(date));
        dayList.add(endDayForYear(date));
        return dayList;
    }
    
    /**
     * 当年的结束日
     * @param date
     * @return
     */
    public static Date endDayForYear(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.setTime(date);
        instance.add(Calendar.YEAR, 1);
        instance.set(Calendar.DAY_OF_YEAR, 0);//设置前一年最后一天
        return instance.getTime();
    }
    
    /**
     * 当年的开始日
     * @param date
     * @return
     */
    public static Date startDayForYear(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.setTime(date);
        instance.add(Calendar.YEAR, 0);
        instance.set(Calendar.DAY_OF_YEAR, 1);//设置当年第一天
        return instance.getTime();
    }
    
    /**
     * 当年的第几天
     * @param date
     * @return
     */
    public static int dayForYear(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        return instance.get(Calendar.DAY_OF_YEAR);
    }
    
    /********************************************季度&天quarterDay***********************************************/
    
    /**
     * 当季度的开始和结束日
     * @param date
     * @return
     */
    public static List<Date> startAndEndForQuarter(Date date) {
        List<Date> dayList = new ArrayList<>();
        dayList.add(startDayForQuarter(date));
        dayList.add(endDayForQuarter(date));
        return dayList;
    }
    
    /**
     * 当季度的结束日
     * @param date
     * @return
     */
    public static Date endDayForQuarter(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.setTime(date);
        int intMonth = instance.get(Calendar.MONTH) + 1;
        if(intMonth>=1 && intMonth<=3) {
            instance.set(Calendar.MONTH, 3);
        }else if(intMonth>=4 && intMonth<=6) {
            instance.set(Calendar.MONTH, 6);
        }else if(intMonth>=7 && intMonth<=9) {
            instance.set(Calendar.MONTH, 9);
        }else if(intMonth>=10 && intMonth<=12) {
            instance.set(Calendar.MONTH, 12);
        }
        instance.set(Calendar.DAY_OF_MONTH, 0);//设置上季度最后一天
        return instance.getTime();
    }
    
    /**
     * 当季度的开始日
     * @param date
     * @return
     */
    public static Date startDayForQuarter(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.setTime(date);
        int intMonth = instance.get(Calendar.MONTH) + 1;
        if(intMonth>=1 && intMonth<=3) {
            instance.set(Calendar.MONTH, 0);
        }else if(intMonth>=4 && intMonth<=6) {
            instance.set(Calendar.MONTH, 3);
        }else if(intMonth>=7 && intMonth<=9) {
            instance.set(Calendar.MONTH, 6);
        }else if(intMonth>=10 && intMonth<=12) {
            instance.set(Calendar.MONTH, 9);
        }
        instance.set(Calendar.DAY_OF_MONTH, 1);//设置当月第一天
        return instance.getTime();
    }
    
    /********************************************月&天monthDay***********************************************/
    
    /**
     * 当月的开始和结束日
     * @param date
     * @return
     */
    public static List<Date> startAndEndForMonth(Date date){
        List<Date> dayList = new ArrayList<>();
        dayList.add(startDayForMonth(date));
        dayList.add(endDayForMonth(date));
        return dayList;
    }
    
    /**
     * 当月的结束日
     * @param date
     * @return
     */
    public static Date endDayForMonth(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.setTime(date);
        instance.add(Calendar.MONTH, 1);
        instance.set(Calendar.DAY_OF_MONTH, 0);//设置上月最后一天
        return instance.getTime();
    }
    
    /**
     * 当月的开始日
     * @param date
     * @return
     */
    public static Date startDayForMonth(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.setTime(date);
        instance.add(Calendar.MONTH, 0);
        instance.set(Calendar.DAY_OF_MONTH, 1);//设置当月第一天
        return instance.getTime();
    }
    
    /**
     * 当月的第几天
     * @param date
     * @return
     */
    public static int dayForMonth(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        return instance.get(Calendar.DAY_OF_MONTH);
    }
    
    /********************************************周&年weekDay***********************************************/
    
    /**
     * 本周的开始和结束日
     * @param date
     * @return
     */
    public static List<Date> startAndEndForWeek(Date date){
        List<Date> dayList = new ArrayList<>();
        dayList.add(startDayForWeek(date));
        dayList.add(endDayForWeek(date));
        return dayList;
    }
    
    /**
     * 本周的结束日
     * @param date
     * @return
     */
    public static Date endDayForWeek(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.setTime(date);
        int intWeek = dayForWeek(date);
        instance.add(Calendar.DAY_OF_WEEK, 7-intWeek);
        return instance.getTime();
    }
    
    /**
     * 本周的开始日
     * @param date
     * @return
     */
    public static Date startDayForWeek(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.setTime(date);
        int intWeek = dayForWeek(date);
        instance.add(Calendar.DAY_OF_WEEK, 1-intWeek);
        return instance.getTime();
    }
    
    /**
     * 根据日期获取本周第几天
     * @param date
     * @return
     */
    public static int dayForWeek(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        //Week,从周日作为第一天,开始计算,因此需要减一
        int intWeek = instance.get(Calendar.DAY_OF_WEEK)-1;
        return intWeek;
    }
}

==============================Calendar日历工具测试类

    /**
     * 获取开始日和结束日之前的天数(保留2位小数,超过2位直接舍弃)
     */
    @Test
    public void test_bigDeciBetweenDays() {
        Date startDate = DateParseUtil.parseDate("20211226", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date endDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        BigDecimal bigDeciBetweenDays = CalendarUtil.bigDeciBetweenDays(startDate, endDate);
        System.out.println(bigDeciBetweenDays);
    }
    
    /**
     * 获取开始日和结束日之间的日期列表(包含开始和结束日)
     */
    @Test
    public void test_listBetweenDays() {
        Date startDate = DateParseUtil.parseDate("20211226", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date endDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        List<Date> dayList = CalendarUtil.listBetweenDays(startDate, endDate);
        for(Date date : dayList) {
            System.out.println(DateFormatUtil.formatDate(date, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
        }
    }
    
    /********************************************年&天yearDay***********************************************/
    
    /**
     * 指定日期的本年开始和结束日
     */
    @Test
    public void test_startAndEndForYear() {
        Date curDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        List<Date> dayList = CalendarUtil.startAndEndForYear(curDate);
        for(Date date : dayList) {
            System.out.println(DateFormatUtil.formatDate(date, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
        }
    }
    
    /**
     * 指定日期的本年最后一天
     */
    @Test
    public void test_endDayForYear() {
        Date curDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date startDay = CalendarUtil.endDayForYear(curDate);
        System.out.println(DateFormatUtil.formatDate(startDay, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
    }
    
    /**
     * 指定日期的本年第一天
     */
    @Test
    public void test_startDayForYear() {
        Date curDate = DateParseUtil.parseDate("20211224", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date startDay = CalendarUtil.startDayForYear(curDate);
        System.out.println(DateFormatUtil.formatDate(startDay, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
    }
    
    /**
     * 根据日期获取本年第几天
     */
    @Test
    public void test_dayForYear() {
        Date curDate = new Date();
        int intMonth = CalendarUtil.dayForYear(curDate);
        System.out.println(intMonth);
    }
    
    /********************************************季度&天quarterDay***********************************************/
    
    /**
     * 指定日期的本季度开始和结束日
     */
    @Test
    public void test_startAndEndForQuarter() {
        Date curDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        List<Date> dayList = CalendarUtil.startAndEndForQuarter(curDate);
        for(Date date : dayList) {
            System.out.println(DateFormatUtil.formatDate(date, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
        }
    }
    
    /**
     * 指定日期的本季度最后一天
     */
    @Test
    public void test_endDayForQuarter() {
        Date curDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date startDay = CalendarUtil.endDayForQuarter(curDate);
        System.out.println(DateFormatUtil.formatDate(startDay, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
    }
    
    /**
     * 指定日期的本季度第一天
     */
    @Test
    public void test_startDayForQuarter() {
        Date curDate = DateParseUtil.parseDate("20211224", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date startDay = CalendarUtil.startDayForQuarter(curDate);
        System.out.println(DateFormatUtil.formatDate(startDay, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
    }
    
    /********************************************月&天monthDay***********************************************/
    
    /**
     * 指定日期的本月开始和结束日
     */
    @Test
    public void test_startAndEndForMonth() {
        Date curDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        List<Date> dayList = CalendarUtil.startAndEndForMonth(curDate);
        for(Date date : dayList) {
            System.out.println(DateFormatUtil.formatDate(date, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
        }
    }
    
    /**
     * 指定日期的本月最后一天
     */
    @Test
    public void test_endDayForMonth() {
        Date curDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date startDay = CalendarUtil.endDayForMonth(curDate);
        System.out.println(DateFormatUtil.formatDate(startDay, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
    }
    
    /**
     * 指定日期的本月第一天
     */
    @Test
    public void test_startDayForMonth() {
        Date curDate = DateParseUtil.parseDate("20211224", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date startDay = CalendarUtil.startDayForMonth(curDate);
        System.out.println(DateFormatUtil.formatDate(startDay, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
    }
    
    /**
     * 根据日期获取本月第几天
     */
    @Test
    public void test_dayForMonth() {
        Date curDate = new Date();
        int intMonth = CalendarUtil.dayForMonth(curDate);
        System.out.println(intMonth);
    }
    
    /********************************************周&年weekDay***********************************************/
    
    /**
     * 指定日期的当周开始和结束日
     */
    @Test
    public void test_startAndEndForWeek() {
        Date curDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        List<Date> dayList = CalendarUtil.startAndEndForWeek(curDate);
        for(Date date : dayList) {
            System.out.println(DateFormatUtil.formatDate(date, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
        }
    }
    
    /**
     * 指定日期的当周最后一天
     */
    @Test
    public void test_endDayForWeek() {
        Date curDate = DateParseUtil.parseDate("20211227", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date startDay = CalendarUtil.endDayForWeek(curDate);
        System.out.println(DateFormatUtil.formatDate(startDay, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
    }
    
    /**
     * 指定日期的当周第一天
     */
    @Test
    public void test_startDayForWeek() {
        Date curDate = DateParseUtil.parseDate("20211224", DateParseUtil.DATE_PATTERN_yyyyMMdd);
        Date startDay = CalendarUtil.startDayForWeek(curDate);
        System.out.println(DateFormatUtil.formatDate(startDay, DateFormatUtil.DATE_PATTERN_yyyyMMdd_HHmmss));
    }
    
    /**
     * 根据日期获取周几
     */
    @Test
    public void test_dayForWeek() {
        Date curDate = new Date();//周一
        int intWeek = CalendarUtil.dayForWeek(curDate);
        System.out.println(intWeek);
        //若 周一,则返回 1
    }

 

posted on 2021-12-28 23:09  铁马冰河2000  阅读(193)  评论(1编辑  收藏  举报