日期工具類

package com.bz.nwvisitor.common.utils;

import com.bz.common.lang.StringUtil;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;


public class CalendarUtil extends com.taobao.util.CalendarUtil {

    private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
        return new SimpleDateFormat(parttern);
    }


    public static String stringToDate(Date date, String parttern) {
        String myDate = null;
        if (date != null) {
            try {
                myDate = getDateFormat(parttern).format(date);
            } catch (Exception e) {
            }
        }
        return myDate;
    }

    public static Date setDateTime(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date setBeginTime(Date date, int hour, int minute) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getBeginOfMinute(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getEndOfMinute(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static Date getEndOfDay(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static boolean isTimeBetween(Date date, String startTime, String endTime) {
        Date startDate = CalendarUtil.toDate(startTime, "HH:mm");
        Date endDate = CalendarUtil.toDate(endTime, "HH:mm");
        Date cdata = CalendarUtil.toDate(CalendarUtil.toString(date, "HH:mm"), "HH:mm");
        return startDate.getTime() <= cdata.getTime() && cdata.getTime() <= endDate.getTime();
    }

    /**
     * @param sDate
     * @param eDate
     * @return result返回結果(0--相同 1-->前者大 2--<後者大)
     */
    public static int getCompareDate(Date sDate, Date eDate) {
        int result = 0;
        //將開始時間賦給日曆實例
        Calendar sC = Calendar.getInstance();
        sC.setTime(sDate);
        //將結束時間賦給日曆實例
        Calendar eC = Calendar.getInstance();
        eC.setTime(eDate);
        //比較
        result = sC.compareTo(eC);
        //返回結果
        return result;
    }

    public static int getDaysBetween(Date startDate, Date endDate) {
        startDate = zerolizedTime(startDate);
        if (endDate.getTime() > startDate.getTime()) {
            endDate = zerolizedTime(addDate(endDate, 1));
        }
        return getIntervalDays(startDate, endDate);
    }

    public static Date getDateTime(Date bookDate, String startDate) {
        if (!StringUtil.isBlank(startDate)) {
            String[] split = startDate.split(":");
            if (split.length > 0) {
                Date bk = CalendarUtil.setBeginTime(bookDate, Integer.parseInt(split[0]), Integer.parseInt(split[1]));
                return bk;
            }
        }
        return null;
    }

    /**
     * 字符串轉換成日期
     *
     * @param str
     * @return date
     */
    public static Date setDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
        }
        return date;
    }

    public static Date setDateTime(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
        }
        return date;
    }

    public static Calendar setCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    public static boolean areSameDay(Date dateA, Date dateB) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(dateA);

        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(dateB);
        if (calDateB.get(Calendar.HOUR_OF_DAY) == 0 && calDateB.get(Calendar.MINUTE) == 0) {
            calDateB.add(Calendar.DAY_OF_MONTH, -1);
        }
        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
                && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB.get(Calendar.DAY_OF_MONTH);
    }

    public static Date getLimitTime(Integer limit) {
        String xiujia = "09-03|09-04|09-05|10-01|10-02|10-05|10-06|10-07|10-10";
        String gongzuo = "09-06";
        int count = 0;
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        while (limit > count) {

            calendar.add(Calendar.DATE, 1);
            SimpleDateFormat monthDay = new SimpleDateFormat("MM-dd");
            String str = monthDay.format(calendar.getTime());
            if (xiujia.indexOf(str) >= 0) {
                continue;
                //  calendar.add(Calendar.DATE, 1);
            }
            if (gongzuo.indexOf(str) >= 0) {
                count++;
            }
            if (!(Calendar.SUNDAY == calendar.get(Calendar.DAY_OF_WEEK) || Calendar.SATURDAY == calendar.get(Calendar.DAY_OF_WEEK))) {
                count++;
            }

        }
        return calendar.getTime();
    }

    public static String stringInsert(Date date, String b, Integer t) {
        TimeZone time = TimeZone.getTimeZone("GMT+8");
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.setTimeZone(time);
        calendar.add(Calendar.HOUR, -8);
        String a = stringToDate(calendar.getTime(), "yyyyMMddHHmmss");
        return a.substring(0, t) + b + a.substring(t, a.length()) + "Z";
    }

    public static String getHour(Date start, Date end, Integer pr) {
        long l = start.getTime() - end.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        double hour = (l / (60 * 60 * 1000) - day * 24);
        long min = (long) ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        if (min == 30) {
            hour = hour + 0.5;
        }
        double num = hour * pr;
        return String.valueOf(num);
    }
}
相關文章
相關標籤/搜索