java元帅

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class test3 {
    /**
     * 获取当前日期上一季度 开始时间
     *
     * @return
     */
    public static Date getStartQuarter(Date date) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(date);
        startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);
        return startCalendar.getTime();
    }
    /**
     * 获取当前日期上一季度 结束时间
     *
     * @return
     */
    public static Date getLastQuarter(Date date) {
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(date);
        endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);

        return endCalendar.getTime();
    }



    /**
     * 最小时间
     *
     * @param calendar
     */
    private static void setMinTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    /**
     * 最大时间
     *
     * @param calendar
     */
    private static void setMaxTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }


    /**
     * 计算某日期所在季度开始日期
     * 季度划分:1、2、3, 4、5、6, 7、8、9, 10、11、12
     */
    public static Date getSeasonEndDate (Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, (month + 3) / 3 * 3);
        calendar.set(Calendar.DATE, 1);
        return new Date(calendar.getTime().getTime() - 24 * 60 * 60 *1000);
    }
    /**
     * 计算某日期所在季度结束日期
     * 季度划分:1、2、3, 4、5、6, 7、8、9, 10、11、12
     */
    public static Date getSeasonStartDate (Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month / 3 * 3);
        calendar.set(Calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 当前年的上一年
     * @return
     */
    public static String getYearBefore() {
        SimpleDateFormat formats = new SimpleDateFormat("yyyy");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1);
        Date date = c.getTime();
        return formats.format(date);
    }

    /**
     * 获取当月(开始时间)
     *
     * @return
     */
    public static String getCurrentMonthStartDate() {

        // 获取当前年份、月份、日期
        Calendar cale = null;
        // 获取当月第一天
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String firstday;
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = format.format(cale.getTime());
        return firstday;
    }


    /**
     * 获取当月(结束时间)
     *
     * @return
     */
    public static String getCurrentMonthEndDate() {

        // 获取当前年份、月份、日期
        Calendar cale = null;
        // 获取当月最后一天
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String lastday;
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        lastday = format.format(cale.getTime());
        return lastday;
    }

    /**
     * 获取当季
     *
     * @return
     */
    public static DateRange getThisQuarter() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);

        return new DateRange(startCalendar.getTime(), endCalendar.getTime());
    }
    public static void main(String[] args) throws ParseException {


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat f = new SimpleDateFormat("yyyy");
        String str = "2022-01-01";
        Date parse = format.parse(str);
        System.out.println(format.format(parse));
//        Date startQuarter = getStartQuarter(new Date());
        Date startQuarter = getStartQuarter(parse);
        System.out.println("获取当前日期上一季度 开始时间"+format.format(startQuarter));
//        Date lastQuarter = getLastQuarter(new Date());
        Date lastQuarter = getLastQuarter(parse);
        System.out.println("获取当前日期上一季度 结束时间:"+format.format(lastQuarter));

        System.out.println("计算某日期所在季度开始日期:"+format.format(getSeasonStartDate(new Date())));
        System.out.println("计算某日期所在季度结束日期:"+format.format(getSeasonEndDate(new Date())));
        System.out.println("当前年的上一年:"+getYearBefore());
        System.out.println("获取当月(开始时间):"+getCurrentMonthStartDate());
        System.out.println("获取当月(结束时间):"+getCurrentMonthEndDate());
        DateRange thisQuarter = getThisQuarter();
        System.out.println("获取当季:"+thisQuarter.getStart()+"----"+thisQuarter.getEnd());
    }

}

 

posted on 2022-09-14 13:44  java元帅  阅读(39)  评论(0编辑  收藏  举报