获取指定日期的前一天 后一天、前一周 后一周、前一年 后一年等

    /**
     * 获得指定日期的前一天
     * @param currentTime
     * @return
     * @throws Exception
     */
    public static  long getSpecifiedDayBefore(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date = new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.DATE, - 1);
        return c.getTime().getTime();
    }

    /**
     * 获得指定日期的后一天
     * @param currentTime
     * @return
     * @throws Exception
     */
    public static  long getSpecifiedDayAfter(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date = new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.DATE, + 1);
        return c.getTime().getTime();
    }

    /**
     * 获得指定日期的前一周
     * @param currentTime
     * @return
     */
    public static  long getSpecifiedWeekBefore(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date= new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.DATE, - 7);
        return c.getTime().getTime();
    }

    /**
     * 获得指定日期的后一周
     * @param currentTime
     * @return
     */
    public static  long getSpecifiedWeekAfter(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date= new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.DATE, + 7);
        return c.getTime().getTime();
    }

    /**
     * 获得指定日期的前一月
     * @param currentTime
     * @return
     */
    public static  long getSpecifiedMonthBefore(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date= new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.MONTH, +1);
        return c.getTime().getTime();
    }

    /**
     * 获得指定日期的前一月
     * @param currentTime
     * @return
     */
    public static  long getSpecifiedMonthAfter(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date= new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.MONTH, +1);
        return c.getTime().getTime();
    }

    /**
     * 获得指定日期的前一季度
     * @param currentTime
     * @return
     */
    public static  long getSpecifiedQuarterBefore(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date= new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.MONTH, -3);
        return c.getTime().getTime();
    }

    /**
     * 获得指定日期的后一季度
     * @param currentTime
     * @return
     */
    public static  long getSpecifiedQuarterAfter(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date= new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.MONTH, +3);
        return c.getTime().getTime();
    }

    /**
     * 获得指定日期的前一年
     * @param currentTime
     * @return
     */
    public static  long getSpecifiedYearBefore(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date= new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.YEAR, -1);
        return c.getTime().getTime();
    }

    /**
     * 获得指定日期的后一年
     * @param currentTime
     * @return
     */
    public static  long getSpecifiedYearAfter(long currentTime){
        Calendar c = Calendar.getInstance();
        Date date= new Date(currentTime);
        c.setTime(date);
        c.add(Calendar.YEAR, +1);
        return c.getTime().getTime();
    }

 

你可能感兴趣的:(java基础)