java获得当前月的第一天最后一天

public class DateDistance {

    // 获取当前月最后一天
    public static Date getMonthLastDay(Date date) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date ddd=cale.getTime();
        try {
            ddd = sdf.parse(sdf.format(cale.getTime()));
        } catch (ParseException e) {
            return ddd;
        }
        return ddd;
    }
    //日期添加i整年
    public static Date getNextYear(Date date,int i) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.set(Calendar.YEAR, i);
        return cale.getTime();
    }

    /**
     *  获得本月第一天
     */
    public static Date getMonthFirstDay(Date date) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMinimum(Calendar.DAY_OF_MONTH));  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date ddd=cale.getTime();
        try {
            ddd = sdf.parse(sdf.format(cale.getTime()));
        } catch (ParseException e) {
            return ddd;
        }
        return ddd;
    }


    // 按标准每月30日计算 月份;
    public static double getMonthCount(Date start_time,Date end_time) {
        double count=0;
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(start_time);
        c2.setTime(end_time);
        int year=c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        double month=  c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        double day=c2.get(Calendar.DAY_OF_MONTH) - c1.get(Calendar.DAY_OF_MONTH)+1;
        if(day>=0){
            count=month+day/c2.getActualMaximum(Calendar.DAY_OF_MONTH);
        }else{
            day=day+30;
            count=month-1+day/30;
        }
        count +=year*12;
        return count;
    }
    //获取年数据
    public static double getYearCount(Date start_time,Date end_time) {
            double count=0;
            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();
            c1.setTime(start_time);
            c2.setTime(end_time);
            double month=  c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
            double day=  c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
            if(day>=0){
                count=month+day/30;
            }else{
                day=day+30;
                count=month-1+day/30;
            }
            return count;
    }
    /*public static void main(String[] args) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String g="2017-10-20";
        Date d1=format.parse(g);
        System.out.println(DateDistance.getMonthCount(d1, new Date()));
    }*/
    /**
     * 两个时间之间相差距离多少天
     * 
     * @param one
     *            时间参数 1:
     * @param two
     *            时间参数 2:
     * @return 相差天数
     */
    public static long getDistanceDays(String str1, String str2){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            days =getDistanceDays(one, two);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 两个时间之间相差距离多少天
     * 
     * @param one
     *            时间参数 1:
     * @param two
     *            时间参数 2:
     * @return 相差天数
     */
    public static long getDistanceDays(Date one, Date two){
        long days = 0;
        long time1 = one.getTime();
        long time2 = two.getTime();
        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        days = diff / (1000 * 60 * 60 * 24);
        return days;
    }

    public static long getDistanceDays(Date one, Date two,boolean iszheng){
        long days = 0;
        long time1 = one.getTime();
        long time2 = two.getTime();
        long diff = time2-time1;
        days = diff / (1000 * 60 * 60 * 24);
        return days;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     * 
     * @param str1
     *            时间参数 1 格式:1990-01-01 12:00:00
     * @param str2
     *            时间参数 2 格式:2009-01-01 12:00:00
     * @return long[] 返回值为:{天, 时, 分, 秒}
     */
    public static long[] getDistanceTimes(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long[] times = { day, hour, min, sec };
        return times;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     * 
     * @param str1
     *            时间参数 1 格式:1990-01-01 12:00:00
     * @param str2
     *            时间参数 2 格式:2009-01-01 12:00:00
     * @return String 返回值为:xx天xx小时xx分xx秒
     */
    public static String getDistanceTime(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day + "天" + hour + "小时" + min + "分" + sec + "秒";
    }

    /**
     * 获取两个时间间隔多少月(足月)
     * 
     * @param start
     * @param end
     * @return
     */
    public static int getBetweenMonth(Date start, Date end) {
        if (start.after(end)) {
            Date t = start;
            start = end;
            end = t;
        }
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        Calendar temp = Calendar.getInstance();
        temp.setTime(end);
        temp.add(Calendar.DATE, 1);

        int year = endCalendar.get(Calendar.YEAR)
                - startCalendar.get(Calendar.YEAR);
        int month = endCalendar.get(Calendar.MONTH)
                - startCalendar.get(Calendar.MONTH);

        if ((startCalendar.get(Calendar.DATE) == 1)
                && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month + 1;
        } else if ((startCalendar.get(Calendar.DATE) != 1)
                && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month;
        } else if ((startCalendar.get(Calendar.DATE) == 1)
                && (temp.get(Calendar.DATE) != 1)) {
            return year * 12 + month;
        } else {
            return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
        }
    }


    /**
     * 获取两个时间间隔多少月(实际月,不足一月的舍去)
     * 
     * @param start
     * @param end
     * @return
     */
    public static int getBetweenMonth(Date start, Date end,boolean ff) {
        if (start.after(end)) {
            Date t = start;
            start = end;
            end = t;
        }
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        Calendar temp = Calendar.getInstance();
        temp.setTime(end);
        temp.add(Calendar.DATE, 1);

        int year = endCalendar.get(Calendar.YEAR)
                - startCalendar.get(Calendar.YEAR);
        int month = endCalendar.get(Calendar.MONTH)
                - startCalendar.get(Calendar.MONTH);

        if(endCalendar.get(Calendar.DAY_OF_MONTH)-startCalendar.get(Calendar.DAY_OF_MONTH)<0){
            month--;
        }
        if ((startCalendar.get(Calendar.DATE) == 1)
                && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month + 1;
        } else if ((startCalendar.get(Calendar.DATE) != 1)
                && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month;
        } else if ((startCalendar.get(Calendar.DATE) == 1)
                && (temp.get(Calendar.DATE) != 1)) {
            return year * 12 + month;
        } else {
            return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
        }
    }

    /**
     * 为当前日期加 day天
     * @param today
     * @param day
     * @return
     */
    public static Date getNextDayDateTime(Date today,int day){
         Calendar c = Calendar.getInstance();  
         c.setTime(today);  
         c.add(Calendar.DAY_OF_MONTH, day);// 今天+day天  
         return c.getTime();  
    }
    /**
     * 为当前日期加month 月 , day天
     * @param today
     * @param day
     * @return
     */
    public static Date getNextDayDateTime(Date today,int month,int day){
         Calendar c = Calendar.getInstance();  
         c.setTime(today);  
         if(month!=0){
             c.add(Calendar.MONTH, month);
         }
         if(day!=0){
             c.add(Calendar.DAY_OF_MONTH, day);// 今天+day天  
         }

         return c.getTime();  
    }


}

猜你喜欢

转载自blog.csdn.net/megamind_hl/article/details/80076637
今日推荐