Fork me on GitHub

日期工具类

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateTimeUtil {

    public static Date formatDate(String str) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.parse(str);
    }

    public static Date getDayStartTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        return cal.getTime();
    }


    public static boolean isZero(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (cal.get(Calendar.HOUR_OF_DAY) == 0 && cal.get(Calendar.MINUTE) == 0 && cal.get(Calendar.SECOND) == 0) {
            return true;
        }
        return false;
    }

    public static Date getDayEndTime24(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 24, 0, 0);

        return cal.getTime();

    }

    public static Date getDate(Date date, Integer time, Field field) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        switch (field) {
            case YEAR:
                cal.add(Calendar.YEAR, time);
                break;
            case MONTH:
                cal.add(Calendar.MONTH, time);
                break;
            case DAY:
                cal.add(Calendar.DAY_OF_MONTH, time);
                break;
            case HOURS:
                cal.add(Calendar.HOUR_OF_DAY, time);
                break;
            case MINUTE:
                cal.add(Calendar.MINUTE, time);
                break;
            case SECOND:
                cal.add(Calendar.SECOND, time);
                break;
        }
        return cal.getTime();
    }

    //17:29:13  09:02:12
    public static Long getDifferTime(Date start, Date end, Field field) {
        long time = start.getTime() - end.getTime();
        switch (field) {
            case DAY:
                return time / (24 * 60 * 60 * 1000);
            case HOURS:
                return time / (60 * 60 * 1000);
            case MINUTE:
                return time / (60 * 1000);
            case SECOND:
                return time / (1000);
        }
        return null;
    }

    public static String getString(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    public static String format(Date date, String stly) {
        String result = "";
        String str = "yyyy-MM-dd HH:mm:ss";
        if (stly != null && !stly.equals("")) str = stly;
        try {
            if (date != null) {
                java.text.DateFormat df = new SimpleDateFormat(str);
                result = df.format(date);
            }
        } catch (Exception e) {
        }
        return result;
    }

    public static Date parse(String dataStr, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);

        Date time = null;
        try {
            time = formatter.parse(dataStr);
        } catch (ParseException e) {

        }
        return time;
    }

    public static List<Date> getCurrAgo(Date startTime, Date endTime) {
        List<Date> result = new ArrayList<Date>();

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startTime);
        tempStart.add(Calendar.DAY_OF_YEAR, 1);
        tempStart.set(Calendar.HOUR_OF_DAY, 0);
        tempStart.set(Calendar.SECOND, 0);
        tempStart.set(Calendar.MINUTE, 0);
        tempStart.set(Calendar.MILLISECOND, 0);

        startTime = tempStart.getTime();

        while (startTime.getTime() <= endTime.getTime()) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
            startTime = tempStart.getTime();
        }

        return result;
    }

    /**
     * 格式化Timestamp 为 yyyy-MM-dd HH:mm:ss
     */
    public static String getDateTimeFromTimeStamp2(Timestamp _timestamp) {
        if (null == _timestamp) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
        String str = df.format(_timestamp);
        return str;
    }

    /**
     * 格式化Timestamp 为 yyyy-MM-dd
     */
    public static String getDateTimeFromTimeStamp(Timestamp _timestamp) {
        if (null == _timestamp) {
            return "";
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式,不显示毫秒
        String str = df.format(_timestamp);
        return str;
    }

    /**
     * 获取零点时间
     *
     * @param flag 0:获取当天0点,1:获取当天23:59:59
     */
    public static Date getZeroTime(Date date, int flag) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        //时分秒(毫秒数)
        long millisecond = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
        //凌晨00:00:00
        cal.setTimeInMillis(cal.getTimeInMillis() - millisecond);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (flag == 0) {
            return cal.getTime();
        } else if (flag == 1) {
            //凌晨23:59:59
            cal.setTimeInMillis(cal.getTimeInMillis() + 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000);
        }
        return cal.getTime();
    }


    /**
     * 获取当前月份第一天0点
     */
    public static Date getZeroTimeFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }


    /**
     * 获取当前月份最后一天23.59.59
     */
    public static Date getZeroTimeLastDayOfMonth() {
        //获取当前月最后一天
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);
        return cal.getTime();
    }


    /**
     * 获取一天前的时间
     */
    public static Date getOneDayAgo(Date date) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    public static enum Field {
        YEAR,
        MONTH,
        DAY,
        HOURS,
        MINUTE,
        SECOND
    }
   /**
    *late日期-early日期
    **/
    public static int daysBetween(Date early, Date late) {
        Calendar calst = Calendar.getInstance();
        Calendar caled = Calendar.getInstance();
        calst.setTime(early);
        caled.setTime(late);
        calst.set(Calendar.HOUR_OF_DAY, 0);
        calst.set(Calendar.MINUTE, 0);
        calst.set(Calendar.SECOND, 0);
        caled.set(Calendar.HOUR_OF_DAY, 0);
        caled.set(Calendar.MINUTE, 0);
        caled.set(Calendar.SECOND, 0);
        //得到两个日期相差的天数

        return ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
    }

    public static Date addDays(Date date, Integer days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }

    public static String timeStamp2Date(String timestampString) {
        Long timestamp = Long.parseLong(timestampString) * 1000;
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date(timestamp));
    }

    /**
     * 得到两个日期之间查几天。
     * @param date1
     * @param date2
     * @return
     */
    public static int getOffsetIn2Date(Date date1, Date date2) {
        long offsetNum = date1.getTime() - date2.getTime();
        return ((Double)Math.ceil(offsetNum / (60 * 60 * 24 * 1000D))).intValue();
    }

    /**
     * @Description //把时间戳转换为date
     * @Param [s]
     * @return
     **/
    public static Date stampToDate(long s){
        Date date = new Date(s*1000);
        return date;
    }
    /**
     *
     * @Description //时间date+周期days=得到相加后的日期
     * @Param [date, days]
     * @return
     *
     **/
    public static Date dayAfter(Date date, int days) {
        Calendar calendar = getZero(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return calendar.getTime();
    }
    public static Calendar getZero(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    // public static void main(String[] args) {
    //     String startProductionDate = DateTimeUtil.timeStamp2Date("1536049980");

    //     String endProductionDate = DateTimeUtil.timeStamp2Date("1536222780");

    //     System.out.println(endProductionDate);

    //     int days = DateTimeUtil.daysBetween(DateTimeUtil.parse(startProductionDate, "yyyy-MM-dd"), DateTimeUtil.parse(endProductionDate, "yyyy-MM-dd"));

    //     System.out.println(days);

    //     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //     Date testDate=DateTimeUtil.dayAfter(new Date(),30);
    //     System.out.println(sdf.format(testDate));


    // }
}

  

posted @ 2019-04-12 17:14  雪人奥特曼  阅读(206)  评论(0编辑  收藏  举报