DateUtil

package com.test.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;


/**
 *
 */
public class DateUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);

    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 yyyy_MM_dd_plus_HH_mm_ss = "yyyy/MM/dd+HH:mm:ss";
    public static final String yyyy_MM_dd_blank_HH_mm_ss = "yyyy/MM/dd HH:mm:ss";
    public static final String yyyy_MM_dd_blank_0 = "yyyy-MM-dd 00:00:00";
    public static final String yyyy_MM_dd_blank_24 = "yyyy-MM-dd 23:59:59";
    public static final String YYYYMMDD ="yyyyMMDD";



    /**
     * 1天的毫秒数
     */
    public static final Long MILLISECOND_ONE_DAY = 1000 * 60 * 60 * 24L;

    /**
     * 1小时的毫秒数
     */
    public static final Long MILLISECOND_ONE_HOUR = 1000 * 60 * 60L;

    /**
     * 1分钟的毫秒数
     */
    public static final Long MILLISECOND_ONE_MINUTE = 1000 * 60L;

    /**
     * 用指定的格式解析日期
     */
    public static Date parse(String date, String format) {
        DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(format);
        return dateTimeFormat.parseDateTime(date).toDate();
    }

    /**
     *yyyy-MM-dd HH:mm:ss
     */
    public static Date parseAll(String date) {
        DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(yyyy_MM_dd_HH_mm_ss);
        return dateTimeFormat.parseDateTime(date).toDate();
    }
    /*
    * 当前日期零点
    * */
    public static Date parse0(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(yyyy_MM_dd_blank_0);
        return DateUtil.parseAll(df.format(date));
    }
    /*
    * 当前日期24点
    * */
    public static Date parse24(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(yyyy_MM_dd_blank_24);
        return DateUtil.parseAll(df.format(date));
    }

    /**
     * yyyy-MM-dd
     */
    public static Date parseYYMMDD(String date) {
        DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(yyyy_MM_dd);
        return dateTimeFormat.parseDateTime(date).toDate();
    }

    /**
     * 用指定的格式转成日期字符串
     */
    public static String format(Date date, String format) {
        if (date == null) {
            return "";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(format);
    }

    public static String formatYYYY_MM_DD(Date date) {
        if (date == null) {
            return "";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(yyyy_MM_dd);
    }
    public static String formatAll(Date date) {
        if (date == null) {
            return "";
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(yyyy_MM_dd_HH_mm_ss);
    }


    /**
     * 转仅保留指定格式的日期。如取当前时间的 yyyy_MM_dd 日期:
     */
    public static Date formatDate(Date date, String format) {
        return parse(format(date, format), format);
    }

    /**
     * 按年增加日期
     */
    public static Date addDateByYear(Date date, int year) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusYears(year).toDate();
    }

    /**
     * 按月增加日期
     */
    public static Date addDateByMonth(Date date, int month) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(month).toDate();
    }

    /**
     * 按天数增加日期
     */
    public static Date addDateByDay(Date date, int days) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();
    }


    /**
     * 按月份增加日期
     */
    public static Date addDateByMonths(Date date, int months) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(months).toDate();
    }

    /**
     * 按小时增加日期
     */
    public static Date addDateByHour(Date date, int hours) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();
    }

    /**
     * 取得两个日期间的天数
     */
    public static int getDaysOfTwoDate(Date startDate, Date endDate) {
        // 时间格式统一
        DateTime startDateTime = new DateTime(startDate);
        DateTime endDateTime = new DateTime(endDate);
        startDateTime = startDateTime.withTime(0, 0, 0, 0);
        endDateTime = endDateTime.withTime(0, 0, 0, 0);
        return Math.abs(Days.daysBetween(startDateTime, endDateTime).getDays());
    }

    /**
     * 获取年的第一天
     */
    public static Date getFirstDayOfYear(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfYear().withMinimumValue().toDate();
    }

    /**
     * 获取年的最后一天
     */
    public static Date getLastDayOfYear(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfYear().withMaximumValue().toDate();
    }

    /**
     * 获取月的第一天
     */
    public static Date getFirstDayOfMonth(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfMonth().withMinimumValue().toDate();
    }

    public static Date getFirstDayOfMonth(String date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfMonth().withMinimumValue().toDate();
    }

    /**
     * 获取月的最后一天
     */
    public static Date getLastDayOfMonth(Date date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfMonth().withMaximumValue().toDate();
    }

    public static Date getLastDayOfMonth(String date) {
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfMonth().withMaximumValue().toDate();
    }

    /**
     * 获取当天0点
     */
    public static Date getToday0() {
        return DateTime.now().withTime(0, 0, 0, 0).toDate();
    }

    public static String startPreOneDay() {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00");
        return formatter.format(LocalDateTime.now().minus(1, ChronoUnit.DAYS)).toString();//获取一天之前的开始时间
    }

    public static String endPreOneDay() {
        java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd 23:59:59");
        return formatter.format(LocalDateTime.now().minus(1, ChronoUnit.DAYS)).toString();//获取一天之前的开始时间
    }
    /**
     * 获取当天24点
     */
    public static Date getToday24() {
        return DateTime.now().withTime(23, 59, 59, 999).toDate();
    }

    /*
     * 获取昨天0点
     */
    public static Date getYesterDay0() {
        return addDateByDay(getToday0(), -1);
    }

    /**
     * 设置时分
     */
    public static Date resetTime(Date date, int hour, int minutes) {
        DateTime dateTime = new DateTime(date.getTime());
        dateTime = dateTime.withTime(hour, minutes, 0, 0);
        return dateTime.toDate();
    }


    /**
     * 校验日期格式
     * @param str
     * @param dateFormat
     * @return b
     */
    public static boolean isValidDate(String str,String dateFormat) {
        boolean convertSuccess=true;
        // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
            convertSuccess=false;
        }
        return convertSuccess;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值