Java实用DateUtils工具类

 实用日期工具类,有需要可以直接摘

/**
 * @author chx
 * @description cn.gov.cwl.vlt.utils
 * @date 2019/10/24
 */
public class DateUtils {

    private static final DateTimeFormatter SF_DATE = DateTimeFormat.forPattern("yyyy-MM-dd");

    private static final DateTimeFormatter SF_DATE_TIME = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");


    /**
     * 获取当天日期字符串
     *
     * @return yyyy-MM-dd
     */
    public static String getDateString() {
        return SF_DATE.print(System.currentTimeMillis());
    }

    /**
     * 获取当天日期+时分秒字符串
     *
     * @return yyyy-MM-dd
     */
    public static String getDateTimeString() {
        return SF_DATE_TIME.print(System.currentTimeMillis());
    }

    /**
     * 根据日期字符串和时间字符串返回Date类型
     * @param dateString yyyy-MM-dd
     * @param  timeString
     * @return HH:mm:ss
     */
    public static Date transformDate(String dateString,String timeString){
        String s = dateString + " " + timeString;
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            date = sdf.parse(s);
        } catch (ParseException e) {
            throw new RuntimeException("时间格式不正确,需要组合成yyyy-MM-dd HH:mm 格式");
        }
        return date;
    }

    /**
     * 根据日期字符串返回Date类型
     * @param dateString yyyy-MM-dd
     * @return Date
     */
    public static Date stringTransformDate(String dateString){
        if (StringUtils.isBlank(dateString)){
            return null;
        }
        DateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间
        //必须捕获异常
        Date date= null;
        try {
             date = sDateFormat.parse(dateString);
            System.out.println(date);
        } catch(ParseException px) {
            throw new RuntimeException("时间格式不正确,需要组合成yyyy-MM-dd HH:mm:ss 格式");
        }
        return date;
    }

    /**
     * 日期转字符串
     *
     * @return yyyy-MM-dd
     */
    public static String getDateToString(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 小时转天(单位转换)
     * @param hourFrom
     * @return
     */
    public static String hourParseDay(Integer hourFrom){
        if (hourFrom == null){
            return "0小时";
        }
        Integer day = null;
        Integer hour = null;
        day = hourFrom/24;
        hour = hourFrom%24;
        if (day == null || day == 0){
            return hour + "小时";
        }else {
            if (hour == null){
                return day + "天";
            }
            return day + "天" + hour + "小时";
        }
    }

    /**
     * 分钟转天(单位转换)
     * @param minuteFrom
     * @return
     */
    public static String minuteParseDay(Integer minuteFrom){
        if (minuteFrom == null || minuteFrom == 0){
            return "0分钟";
        }
        int hours = (int) Math.floor(minuteFrom / 60);
        int day = hours / 24;
        if (hours >= 24){
            hours = hours - day*24;
        }
        int minute = minuteFrom % 60;
        if (minuteFrom < 60){
            return minute + "分钟";
        }else if (minuteFrom < 1440){
            return hours + "小时" + minute + "分钟";
        }else {
            return (day + "天" + hours + "小时" + minute + "分钟");
        }
    }

    /**
     * 根据入参(单位:分钟)获取触发方法时刻起,算minuteFrom分钟前的时间
     * @param minuteFrom
     * @return
     */
    public static Date getBeforeTime(Integer minuteFrom){
        if (minuteFrom == null){
            return null;
        }
        Calendar nowBefore = Calendar.getInstance();
        Integer beforeTime = -minuteFrom;
        nowBefore.add(Calendar.MINUTE, beforeTime);
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = nowBefore.getTime();
        return time;
    }

    /**
     * 获取两个时间相差多少分钟
     * @param mixTime
     * @param minTime
     * @return
     */
    public static long getTwoTimeDifferMinute(Date mixTime,Date minTime){
        if (mixTime == null || minTime == null){
            throw new RuntimeException("入参的时间不能为null");
        }
        // 计算相差多少分钟
        long nm = 1000 * 60;
        // 获得两个时间的毫秒时间差异
        long diff = mixTime.getTime() - minTime.getTime();
        long residue = diff/nm;
        return residue;
    }
}

 

 

你可能感兴趣的:(Java实用工具类,#DateUtils工具类)