date 工具类

package lizikj.bigwheel.common.vo.merchandise.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

/**
 * 时间帮助类
 *
 */
public class DateUtil {

    final static Logger logger = Logger.getLogger(DateUtil.class);

    /**
     * 判断是否是周末
     * 
     * @return
     */
    public static boolean isWeek() {
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (week == 6 || week == 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断时间是否在时间段内
     * 
     * @param date
     *            当前时间 yyyy-MM-dd HH:mm:ss
     * @param strDateBegin
     *            开始时间 00:00:00
     * @param strDateEnd
     *            结束时间 00:05:00
     * @return
     */
    public static boolean isTimePeriod(Date date, String strDateBegin, String strDateEnd) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        // 截取当前时间时分秒
        int strDateH = Integer.parseInt(strDate.substring(11, 13));
        int strDateM = Integer.parseInt(strDate.substring(14, 16));
        int strDateS = Integer.parseInt(strDate.substring(17, 19));
        // 截取开始时间时分秒
        int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
        int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
        int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));
        // 截取结束时间时分秒
        int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
        int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));
        int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));
        if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
            // 当前时间小时数在开始时间和结束时间小时数之间
            if (strDateH > strDateBeginH && strDateH < strDateEndH) {
                return true;
                // 当前时间小时数等于开始时间小时数,分钟数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数等于开始时间小时数,分钟数等于开始时间分钟数,秒数在开始和结束之间
            } else if (strDateH == strDateBeginH && strDateM == strDateBeginM && strDateS >= strDateBeginS
                    && strDateS <= strDateEndS) {
                return true;
            }
            // 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数小等于结束时间分钟数
            else if (strDateH >= strDateBeginH && strDateH == strDateEndH && strDateM <= strDateEndM) {
                return true;
                // 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数等于结束时间分钟数,秒数小等于结束时间秒数
            } else if (strDateH >= strDateBeginH && strDateH == strDateEndH && strDateM == strDateEndM
                    && strDateS <= strDateEndS) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 判断当前日期是否在一个日期范围内
     * 
     * @param fromDate
     * @param toDate
     * @return
     */
    public static boolean isDate(Date fromDate, Date toDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String d1 = sdf.format(new Date());// 第一个时间

        try {
            Date testDate = sdf.parse(d1);
            // 1、 交换开始和结束日期
            if (fromDate.getTime() > toDate.getTime()) {
                Date tempDate = fromDate;
                fromDate = toDate;
                toDate = tempDate;
            }
            // 2、缩小范围
            long testDateTime = testDate.getTime();
            if ((testDateTime > fromDate.getTime() && testDateTime > toDate.getTime())
                    || testDateTime < fromDate.getTime() && testDateTime < toDate.getTime()) {
                return false;
            }
        } catch (Exception e) {
            logger.error("isDate is error", e);
        }
        return true;
    }

    // 最近一周
    public static String getWeek(int num) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cl = Calendar.getInstance();
        cl.add(Calendar.WEEK_OF_YEAR, -1); // 一周
        Date dateFrom = cl.getTime();
        result = sdf.format(dateFrom);
        // System.out.println(sdf.format(dateFrom)+"到"+sdf.format(dateNow));
        return result;
    }

    // 最近一月
    public static String getMonth(int num) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cl = Calendar.getInstance();
        cl.add(Calendar.MONTH, -num); // 一个月
        Date dateFrom = cl.getTime();
        result = sdf.format(dateFrom);
        return result;
    }

    // 当前时间的前一天
    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }

    // 判断是否在时间前后
    public static int compare_date(String DATE1, String DATE2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception e) {
            logger.error("compare_date is error", e);
        }
        return 0;
    }

    /**
     * 判断指定日期是否在一个日期范围内
     * 
     * @param fromDate
     *            范围开始日期
     * @param toDate
     *            范围结束日期
     * @param testDate
     *            测试日期
     * @return 在范围内true,否则false
     */
    public static boolean betweenDays(Date fromDate, Date toDate, Date testDate) {
        if (fromDate == null || toDate == null || testDate == null) {
            return false;
        }

        // 1、 交换开始和结束日期
        if (fromDate.getTime() > toDate.getTime()) {
            Date tempDate = fromDate;
            fromDate = toDate;
            toDate = tempDate;
        }

        // 2、缩小范围
        long testDateTime = testDate.getTime();
        if ((testDateTime > fromDate.getTime() && testDateTime > toDate.getTime())
                || testDateTime < fromDate.getTime() && testDateTime < toDate.getTime()) {
            return false;
        }

        return true;
    }

    public static Date getDateTime(long now) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(now);
        System.out.println(now + " = " + formatter.format(calendar.getTime()));
        String result = formatter.format(calendar.getTime());
        return calendar.getTime();
    }

    public static Date transferLongToDate(Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date(millSec);
        return date;
    }

    public static void main(String[] args) {
        String now = "1475921070450";
        System.out.println(transferLongToDate(Long.valueOf(now)));
    }
}
/**
     * 判断两个时间区间是否有交集的方法
     * 
     * @param date1_1
     *            区间1的时间始
     * @param date1_2
     *            区间1的时间止
     * @param date2_1
     *            区间2的时间始
     * @param date2_2
     *            区间2的时间止
     * @return 区间1和区间2如果存在交集,则返回true,否则返回falses
     */
    public static boolean test(String startA, String endA, String startB, String endB) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date a1 = sdf.parse(startA);
        Date a2 = sdf.parse(endA);

        Date b1 = sdf.parse(startB);
        Date b2 = sdf.parse(endB);

        /**
         * if(第一组最后时间小于等于第二组最前时间 || 第一组最前时间大于等于第二组最后时间){ 无交集 } else{ 有交集 }
         */
        if ((a2.getTime() <= b1.getTime()) || a1.getTime() >= b2.getTime()) {
            return false;
        } else {
            return true;
        }
    }

    public static void main(String[] args) throws Exception {
        String dateStr1_1 = "2017-10-01 00:00:00";
        String dateStr1_2 = "2017-10-03 23:59:59";
        String dateStr2_1 = "2017-10-04 00:00:00";
        String dateStr2_2 = "2017-10-05 23:59:59";
    
        boolean b = test(dateStr1_1, dateStr1_2, dateStr2_1, dateStr2_2);
        System.out.println(b == true ? "有交集" : "无交集");
    }
/**
     * 判断时间是否在时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.baoqilai.ddg.exception.BaseRuntimeException;

public class DateUtil {

    /**
     * 
     * @Description: 给出的时间和当前时间的小时差
     * @date 2017年3月24日 下午5:47:49
     * @param startDate
     * @return
     */
    public static double getHour(Date startDate) {
        long time = startDate.getTime();
        Date date = new Date();
        long nowTime = date.getTime();
        double mil = (nowTime - time) / 1000;
        double hour = mil / 3600;
        return hour;
    }

    /**
     * 
     * @Description: 计算两个时间的 天数差 过 00:00算一条
     * @date 2017年3月24日 下午6:34:21
     * @param startDate
     * @return
     */
    public static int getDay(Date startDate) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String nowDate = formatter.format(date);
        String sd = formatter.format(startDate);
        int day = Integer.parseInt(nowDate) - Integer.parseInt(sd);
        return day;
    }

    public static String getDateString(Date startDate,String fmt) {
        SimpleDateFormat formatter = new SimpleDateFormat(fmt);
        String nowDate = formatter.format(startDate);
        return nowDate;
    }
    
    
    public static Date getNowDate() {
        String str = "2017-03-23 13:30:20";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = formatter.parse(str);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static void main(String[] args) {
        Date nowDate = getNowDate();
        /*
         * double hour = getHour(nowDate);
         *
         * if (hour > 5) { System.out.println("-------------"); }
         *
         * System.out.println(hour);
         */
        Integer day = getDay(nowDate);
        System.out.println(day);
    }

    /**
     * desc: 将字符串转换为date对象
     * date: 2017/5/20 15:32
     * @param date
     * @return
     */
    public static  Date formart(String date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date2 = sdf.parse(date);
            return  date2;
        } catch (ParseException e) {
            throw new BaseRuntimeException("传入的日期格式有误!(正确格式为:yyyy-MM-dd HH:mm:ss)");
        }

    }

    /**
     * desc: 判断两个时间段是否有交集
     * date: 2017/5/20 15:37
     * @param startdate1
     * @param enddate1
     * @param startdate2
     * @param enddate2
     * @return
     */
    public  static  boolean isOverlap (String startdate1,String enddate1,String startdate2,String enddate2){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date leftStartDate = null;
        Date leftEndDate = null;
        Date rightStartDate = null;
        Date rightEndDate = null;
        try {
            leftStartDate = format.parse(startdate1);
            leftEndDate = format.parse(enddate1);
            rightStartDate = format.parse(startdate2);
            rightEndDate = format.parse(enddate2);
        } catch (ParseException e) {
            return false;
        }

        return
                ((leftStartDate.getTime() >= rightStartDate.getTime())
                        && leftStartDate.getTime() < rightEndDate.getTime())
                        ||
                        ((leftStartDate.getTime() > rightStartDate.getTime())
                                && leftStartDate.getTime() <= rightEndDate.getTime())
                        ||
                        ((rightStartDate.getTime() >= leftStartDate.getTime())
                                && rightStartDate.getTime() < leftEndDate.getTime())
                        ||
                        ((rightStartDate.getTime() > leftStartDate.getTime())
                                && rightStartDate.getTime() <= leftEndDate.getTime());

    }

    /**
     * desc: 判断当前时间是否包含在指定的时间区间
     * date: 2017/5/25 10:49
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @return
     */
    public static boolean isInDate(Date startTime,Date endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = sdf.format(new Date());
        String strDateBegin = sdf.format(startTime);
        String strDateEnd = sdf.format(endTime);

        // 截取当前时间年月日 转成整型
        Long  tempDate=Long.parseLong(strDate);
        // 截取开始时间年月日 转成整型
        Long  tempDateBegin=Long.parseLong(strDateBegin);
        // 截取结束时间年月日   转成整型
        Long  tempDateEnd=Long.parseLong(strDateEnd);
        if ((tempDate >= tempDateBegin && tempDate <= tempDateEnd)) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 指定日期加上天数后的日期
     * @param num 为增加的天数
     * @param newDate 创建时间
     * @return
     * @throws ParseException 
     */
    public static Date plusDay(int num,String newDate) throws ParseException{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date  currdate = format.parse(newDate);
        System.out.println("现在的日期是:" + currdate);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, num);// num为增加的天数,可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);
        System.out.println("增加天数以后的日期:" + enddate);
        return currdate;
    }
    
    /**
     * 当前日期加上天数后的日期
     * @param num 为增加的天数
     * @return
     */
    public static Date plusDay2(int num){
        Date d = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currdate = format.format(d);
        System.out.println("现在的日期是:" + currdate);

        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, num);// num为增加的天数,可以改变的
        d = ca.getTime();
        String enddate = format.format(d);
        System.out.println("增加天数以后的日期:" + enddate);
        return d;
    }



}
原文地址:https://www.cnblogs.com/lanliying/p/6440140.html