Calendar获取想要的日期

时间:2025-01-26 15:57:34
获取当前天的前n天: getTodayPreNDay(int n)
取得月第一天:getFirstDateOfMonth(Date date)
取得月最后一天:getLastDateOfMonth(Date date)
获取当前的季度:getSeason(Date date)
当前季度都包含哪些月份:Date[] getSeasonDate(Date date)  月初1号
当前月有多少天:getDayOfMonth(Date date)
当前月已经过了多少天:getPassDayOfMonth(Date date)
当前月还剩多少天:getRemainDayOfMonth(Date date)
季度的第一天:getFirstDateOfSeason(Date date)
季度的最后一天:etLastDateOfSeason(Date date)
季度一共多少天:getDayOfSeason(Date date)
季度已经过了多少天:getPassDayOfSeason(Date date)
季度还剩下多少天:getRemainDayOfMonth(Date date)
public static void calendar() {
    Calendar calendar = ();
    //(, 1);  //一年后的今天为基准(add具有累加效果)
    //(, 1);// 同理换成下个月的今天(add具有累加效果)
    (, 2000);  //直接覆盖年这个字段,其他的去默认值
    ("现在是" + () + "年");
    (2018, 7, 1);//直接覆盖年月日这个字段,其他的去默认值
    int year = (); // 获取年
    int month = () + 1;// 获取月,这里需要需要月份的范围为0~11,因此获取月份的时候需要+1才是当前月份值
    int day = (Calendar.DAY_OF_MONTH); // 获取日
    int hour = (); // 获取时
    // int hour = (Calendar.HOUR_OF_DAY); // 24小时表示
    int minute = ();// 获取分
    int second = ();// 获取秒
    int weekday = (Calendar.DAY_OF_WEEK);// 星期,英语国家星期从星期日开始计算(从星期天开始算的,比如,今天是星期三,返回4)
    ("现在是" + year + "年" + month + "月" + day + "日" + hour
            + "时" + minute + "分" + second + "秒" + "星期" + weekday);
}

 

public class TestCalendar {
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd hh:mm:ss";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM = "yyyy_MM";
    public static final String YYYYMM = "yyyyMM";


    public static void main(String[] args) throws Exception{
        (formatDateToStr(getTodayPreNDay(0)));
        (formatDateToStr(getTodayPreNDay(1)));
        (formatDateToStr(getTodayPreNDay(31)));
        (getMonthInt(new Date()));
        (getYearInt(new Date()));
        (formatDateToStr(getFirstDateOfMonth(new Date())));//月初
        (formatDateToStr(getLastDateOfMonth(new Date())));//月末

    }

    /**
     * 取得季度第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDateOfSeason(Date date) {
        return getFirstDateOfMonth(getSeasonDate(date)[0]);
    }

    /**
     * 取得季度最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDateOfSeason(Date date) {
        return getLastDateOfMonth(getSeasonDate(date)[2]);
    }

    /**
     * 取得季度天数
     *
     * @param date
     * @return
     */
    public static int getDayOfSeason(Date date) {
        int day = 0;
        Date[] seasonDates = getSeasonDate(date);
        for (Date date2 : seasonDates) {
            day += getDayOfMonth(date2);
        }
        return day;
    }

    /**
     * 取得季度剩余天数
     *
     * @param date
     * @return
     */
    public static int getRemainDayOfSeason(Date date) {
        return getDayOfSeason(date) - getPassDayOfSeason(date);
    }

    /**
     * 取得季度已过天数
     *
     * @param date
     * @return
     */
    public static int getPassDayOfSeason(Date date) {
        int day = 0;

        Date[] seasonDates = getSeasonDate(date);

        Calendar c = ();
        (date);
        int month = ();

        if (month ==  || month == 
                || month ==  || month == ) {// 季度第一个月
            day = getPassDayOfMonth(seasonDates[0]);
        } else if (month ==  || month == 
                || month ==  || month == ) {// 季度第二个月
            day = getDayOfMonth(seasonDates[0])
                    + getPassDayOfMonth(seasonDates[1]);
        } else if (month ==  || month == 
                || month ==  || month == ) {// 季度第三个月
            day = getDayOfMonth(seasonDates[0]) + getDayOfMonth(seasonDates[1])
                    + getPassDayOfMonth(seasonDates[2]);
        }
        return day;
    }

    /**
     * 取得月的剩余天数
     *
     * @param date
     * @return
     */
    public static int getRemainDayOfMonth(Date date) {
        int dayOfMonth = getDayOfMonth(date);
        int day = getPassDayOfMonth(date);
        return dayOfMonth - day;
    }

    /**
     * 取得月已经过的天数
     *
     * @param date
     * @return
     */
    public static int getPassDayOfMonth(Date date) {
        Calendar c = ();
        (date);
        return (Calendar.DAY_OF_MONTH);
    }

    /**
     * 取得月天数
     *
     * @param date
     * @return
     */
    public static int getDayOfMonth(Date date) {
        Calendar c = ();
        (date);
        return (Calendar.DAY_OF_MONTH);
    }

    /**
     * 取得季度月
     *
     * @param date
     * @return
     */
    public  static Date[] getSeasonDate(Date date) {
        Date[] season = new Date[3];
        Calendar c = ();
        int nSeason = getSeason(date);
        if (nSeason == 1) {// 第一季度
            (, );
            season[0] = ();
            (, );
            season[1] = ();
            (, );
            season[2] = ();
        } else if (nSeason == 2) {// 第二季度
            (, );
            season[0] = ();
            (, );
            season[1] = ();
            (, );
            season[2] = ();
        } else if (nSeason == 3) {// 第三季度
            (, );
            season[0] = ();
            (, );
            season[1] = ();
            (, );
            season[2] = ();
        } else if (nSeason == 4) {// 第四季度
            (, );
            season[0] = ();
            (, );
            season[1] = ();
            (, );
            season[2] = ();
        }
        return season;
    }

    /**
     * 获取当前季度
     * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
     *
     * @param date
     * @return
     */
    public static int getSeason(Date date) {

        int season = 0;

        Calendar c = ();
        (date);
        int month = ();
        switch (month) {
            case :
            case :
            case :
                season = 1;
                break;
            case :
            case :
            case :
                season = 2;
                break;
            case :
            case :
            case :
                season = 3;
                break;
            case :
            case :
            case :
                season = 4;
                break;
            default:
                break;
        }
        return season;
    }

    /**
     * 取得月第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDateOfMonth(Date date) {
        Calendar c = ();
        (date);
        (Calendar.DAY_OF_MONTH, (Calendar.DAY_OF_MONTH));
        return ();
    }

    /**
     * 取得月最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDateOfMonth(Date date) {
        Calendar c = ();
        (date);
        (Calendar.DAY_OF_MONTH, (Calendar.DAY_OF_MONTH));
        return ();
    }

    /**
     * 获取指定日期所属的月份
     * @param date
     * @return
     */
    public static int getMonthInt(Date date) {
        Calendar c = ();
        (date);
        return () + 1;
    }

    /**
     * 获取指定日期所属的年
     * @param date
     * @return
     */
    public static int getYearInt(Date date) {
        Calendar c = ();
        (date);
        return ();
    }

    /**
     * 获取当前天的前n天
     * @param n
     * @return
     */
    public static Date getTodayPreNDay(int n) {
        Calendar calendar = ();
        (, -n);
        return ();
    }


    /**
     * 通过Calendar获取今天的日期
     * @return Date
     */
    public static Date getTodayFromCalendar(){
        Calendar calendar = ();
        return ();
    }

    /**
     * 通过pattern设定日期的格式
     * @return
     */
    public static String getTodayStr() { return getTodayStr(YYYYMMDD); }
    public static String getTodayStr(String pattern) {
        Date NowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return (NowDate);
    }

    /**
     * 字符串转date
     * 根据字符串格式的日期和期望要的格式生成date日期
     * @param strDate
     * @return
     */
    public static Date parseStrToDate(String strDate) {
        return parseStrToDate(strDate, null);
    }
    public static Date parseStrToDate(String strDate, String pattern) {
        Date date = null;
        try {
            if (pattern == null) {
                pattern = YYYYMMDD;
            }
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            date = (strDate);
        } catch (Exception e) {
            ();
        }
        return date;
    }

    /**
     *  date转字符串
     * @param date
     * @return
     */
    public static String formatDateToStr(Date date) {
        return formatDateToStr(date, null);
    }
    public static String formatDateToStr(Date date, String pattern) {
        String strDate = null;
        try {
            if (pattern == null) {
                pattern = YYYYMMDD;
            }
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            strDate = (date);
        } catch (Exception e) {
            ();
        }
        return strDate;
    }
}