JAVA日期计算,格式化日期的常用方法

public class DateUtils {

    private static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";

    private static final String FORMAT_DAY = "yyyy-MM-dd";

    private static final String FORMAT_MONTH = "yyyy-MM";

    /**
     * 获取去年年份
     *
     * @return String
     */
    public static String lastYear() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        return String.valueOf(year - 1);
    }

    /**
     * 通过传入的时间,获取去年年份
     *
     * @param date 日期
     * @return String
     */
    public static String lastYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        return String.valueOf(year - 1);
    }

    /**
     * 获取上月 月份
     *
     * @return String
     */
    public static String lastMonth() {
        Calendar cal = Calendar.getInstance();
        int lastMonth = cal.get(Calendar.MONTH);
        if (lastMonth == 0) {
            lastMonth = 12;
        }
        return String.valueOf(lastMonth);
    }

    /**
     * 根据传入的日期获取上月月份
     *
     * @param date date
     * @return String
     */
    public static String lastMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int lastMonth = cal.get(Calendar.MONTH);
        if (lastMonth == 0) {
            lastMonth = 12;
        }
        return String.valueOf(lastMonth);
    }

    /**
     * 获取当前年份
     *
     * @return String类型年份
     */
    public static String currentYear() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        return String.valueOf(year);
    }

    /**
     * 获取当前年份
     *
     * @return int
     */
    private static int currentIntYear() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.YEAR);
    }

    /**
     * 获取当前月份
     *
     * @return String类型月份
     */
    public static String currentMonth() {
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        return String.valueOf(month);
    }

    /**
     * 获取当前月份
     *
     * @return int类型月份
     */
    public static int currentIntMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前月份最后一天 23:59:59
     *
     * @return date
     */
    public static Date currentMonthLastDay() {
        Calendar cal = Calendar.getInstance();
        int maxDate = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DATE, maxDate);
        //将小时至0
        cal.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至0
        cal.set(Calendar.MINUTE, 59);
        //将秒至0
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 获取下个月第一天 00:00:00
     *
     * @return date
     */
    public static Date nextMonthFirstDay() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        cal.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        cal.set(Calendar.MINUTE, 0);
        //将秒至0
        cal.set(Calendar.SECOND, 0);
        //将毫秒至0
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取当前年最后一天
     *
     * @return date
     */
    public static Date currentYearLastDay() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.getActualMaximum(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至0
        cal.set(Calendar.MINUTE, 59);
        //将秒至0
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 获取次年第一天
     *
     * @return date
     */
    public static Date nextYearFirstDay() {
        Calendar cal = Calendar.getInstance();
        //设置为1月
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        //设置为1号
        cal.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        cal.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        cal.set(Calendar.MINUTE, 0);
        //将秒至0
        cal.set(Calendar.SECOND, 0);
        //将毫秒至0
        cal.set(Calendar.MILLISECOND, 0);
        //增加一年
        cal.add(Calendar.YEAR, 1);
        return cal.getTime();
    }

    public static String dateFormatDay(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DAY);
        return sdf.format(date);
    }

    public static String dateFormatMonth(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_MONTH);
        return sdf.format(date);
    }

    /**
     * 获取当前日期前15天的日期
     *
     * @return string yyyy-MM-dd
     */
    public static String before15Day() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -14);
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DAY);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取当前日期前30天的日期
     *
     * @return string yyyy-MM-dd
     */
    public static String before30Day() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -29);
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DAY);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取当前日期前12个月的1号日期
     *
     * @return yyyy-MM-dd
     */
    public static String before12MonthDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -11);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DAY);
        return sdf.format(calendar.getTime());
    }

    /**
     * 计算两日期相差天数
     * 这里就算日期包含起止时间[begin,end] 日期格式为yyyy-MM-dd HH:mm:ss 如果为yyyy-MM-dd结果会有误差
     *
     * @param before 早日期
     * @param end    晚日期
     * @return 天数
     */
    public static int diffDays(Date before, Date end) {
        Calendar beginCalendar = Calendar.getInstance();
        beginCalendar.setTime(before);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        long beginTime = beginCalendar.getTime().getTime();
        long endTime = endCalendar.getTime().getTime();
        int betweenDays = (int) ((endTime - beginTime) / (1000 * 60 * 60 * 24));
        endCalendar.add(Calendar.DAY_OF_MONTH, -betweenDays);
        endCalendar.add(Calendar.DAY_OF_MONTH, -1);
        if (beginCalendar.get(Calendar.DAY_OF_MONTH) == endCalendar.get(Calendar.DAY_OF_MONTH)) {
            return betweenDays + 2;
        } else {
            return betweenDays + 1;
        }
    }

    /**
     * 计算两个日期相差月份
     *
     * @param before 早日期
     * @param end    晚日期
     * @return 月份差值
     */
    public static int diffMonths(Date before, Date end) {
        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        startTime.setTime(before);
        endTime.setTime(end);
        int result = endTime.get(Calendar.MONTH) - startTime.get(Calendar.MONTH) + 1;
        int month = (endTime.get(Calendar.YEAR) - startTime.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }

}

 

你可能感兴趣的:(JAVA,java)