DateUtils工具类

package com.xqjr.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author 时
 * @Date 2018-03-11
 */
public class DateUtils {

    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 将日期转化为指定格式的字符串
     * @param date    需要转化的日期
     * @param pattern 转化规则
     * @return 指定格式的日期字符串
     */
    public static String getString(Date date, String pattern) {
        DateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }

    public static String getStringShort(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if(date!=null)
            return df.format(date);
        else
            return "";
    }

    /**
     * 将日期转化为指定格式的字符串
     * @param date    需要转化的日期
     * @return 指定格式的日期字符串
     */
    public static String getToString(Date date) {
        if(date==null){
            return "";
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    public static String getToStringShort(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if(date!=null)
            return df.format(date);
        else
            return "";
    }
    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 判断两个日期是否为同一天
     * @param date1
     * @param Date2
     * @return
     */
    public static boolean inSameDay(Date date1, Date Date2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        int year1 = calendar.get(Calendar.YEAR);
        int day1 = calendar.get(Calendar.DAY_OF_YEAR);

        calendar.setTime(Date2);
        int year2 = calendar.get(Calendar.YEAR);
        int day2 = calendar.get(Calendar.DAY_OF_YEAR);

        if ((year1 == year2) && (day1 == day2)) {
            return true;
        }
        return false;
    }

    /*
     * 将时间戳转换为时间
     */
    public static Date stampToDate(String stampDate){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(stampDate);
        Date date = new Date(lt);
        return date;
    }

    /**
     * 将日期字符串转换为指定格式的日期对象
     * @param strDate 日期字符串
     * @param format  转化规则
     * @return 指定格式的日期对象
     */
    public static Date getDate(String strDate, String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            return df.parse(strDate);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Can't parse " + strDate + " using " + format);
        }
    }

    /**
     * 获取两个日期对象之间相差的天数
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return 相差天数
     */
    public static int getDays(Date beginDate, Date endDate) {
        int days = -1;
        long beginMillisecond = beginDate.getTime();
        long endMillisecond = endDate.getTime();
        long millisecondForDay = 24 * 60 * 60 * 1000;
        days = (int) ((endMillisecond - beginMillisecond) / millisecondForDay);
        return days;
    }

    /**
     * 获得两个日期对象之间相差的小时数
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return 相差小时
     */
    public static long getTimes(Date beginDate, Date endDate) {
        long time = -1;
        long beginMillisecond = beginDate.getTime();
        long endMillisecond = endDate.getTime();
        long millisecondForDay = 60 * 60 * 1000;
        time = (long) ((endMillisecond - beginMillisecond) / millisecondForDay);
        return time;
    }

    /**
     * 指定的日期加上或者减去指定的天数
     * @param date 日期
     * @param day  天数
     * @return 修改后的日期
     */
    public static Date addDays(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 指定的日期加上或者减去指定的月数
     * @param date 日期
     * @param count  月数
     * @return 修改后的日期
     */
    public static Date addMonths(Date date, int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, count);
        return calendar.getTime();
    }

    /**
     * 判断当前时间是上午还是下午
     * @param date 日期
     * @return 上午or下午
     */
    public static String getAmOrPm(Date date) {
        String str = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int num = calendar.get(Calendar.AM_PM);
        if (num == 0) {
            str = "上午";
        } else if (num == 1) {
            str = "下午";
        }
        return str;
    }

    /**
     * 获取指定时间的星期数
     * @param date 日期
     * @return 例如:星期一
     */
    public static String getStrWeek(Date date) {
        String strWeek = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekNum = calendar.get(Calendar.DAY_OF_WEEK);
        if (weekNum == 1) {
            strWeek = "星期天";
        } else if (weekNum == 2) {
            strWeek = "星期一";
        } else if (weekNum == 3) {
            strWeek = "星期二";
        } else if (weekNum == 4) {
            strWeek = "星期三";
        } else if (weekNum == 5) {
            strWeek = "星期四";
        } else if (weekNum == 6) {
            strWeek = "星期五";
        } else if (weekNum == 7) {
            strWeek = "星期六";
        }
        return strWeek;
    }

    /**
     * 获取指定时间的星期数
     * @param date
     * @return 例如:1
     */
    public static int getIntWeek(Date date) {
        int intWeek = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekNum = calendar.get(Calendar.DAY_OF_WEEK);
        if (weekNum == 1) {
            intWeek = 7;
        } else if (weekNum == 2) {
            intWeek = 1;
        } else if (weekNum == 3) {
            intWeek = 2;
        } else if (weekNum == 4) {
            intWeek = 3;
        } else if (weekNum == 5) {
            intWeek = 4;
        } else if (weekNum == 6) {
            intWeek = 5;
        } else if (weekNum == 7) {
            intWeek = 6;
        }
        return intWeek;
    }

    /**
     * 获取当前时间的下个星期一
     * @return 下个星期一的Date对象
     */
    public static Date getNextMonday() {
        Date date = new Date();
        int w = getIntWeek(date);
        if (w == 1) {
            return addDays(date, 7);
        }
        if (w == 2) {
            return addDays(date, 6);
        }
        if (w == 3) {
            return addDays(date, 5);
        }
        if (w == 4) {
            return addDays(date, 4);
        }
        if (w == 5) {
            return addDays(date, 3);
        }
        if (w == 6) {
            return addDays(date, 2);
        }
        return null;
    }

    /**
     * 获取当月第一天
     * @return
     */
    public static String getFirstDayOfMonth() {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.DATE, 1);// 设为当前月的1 号
        str = sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * 获取当月第一天
     * @return
     */
    public static Date getFirstDayOfMonthToDate() {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.DATE, 1);// 设为当前月的1 号
        lastDate.set(Calendar.HOUR_OF_DAY,0);//设置小时为 0时
        lastDate.set(Calendar.MINUTE,0);//设置分钟为0分钟
        lastDate.set(Calendar.SECOND,0);//设置秒为0秒
        return lastDate.getTime();
    }

    /**
     * 获取上月第一天
     * @return
     */
    public static String getPreviousMonthFirst() {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.DATE, 1);// 设为当前月的1 号
        lastDate.add(Calendar.MONTH, -1);// 减一个月,变为上月的1 号
        // lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天
        str = sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * 获得上月最后一天的日期
     * @return
     */
    public static String getPreviousMonthEnd() {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.add(Calendar.MONTH, -1);// 减一个月
        lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
        lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天
        str = sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * 获取给定的两个日期之间的所有的Date
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 日期结果集
     * @throws Exception 开始时间大于结束时间的异常
     */
    public static List<Date> dateSplit(Date startDate, Date endDate) throws Exception {
        if (!startDate.before(endDate))
            throw new Exception("开始时间应该在结束时间之后");
        Long spi = endDate.getTime() - startDate.getTime();
        Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数

        List<Date> dateList = new ArrayList<Date>();
        dateList.add(endDate);
        for (int i = 1; i <= step; i++) {
            dateList.add(new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000)));// 比上一天减一
        }
        return dateList;
    }

    /**
     * 在给定日期上向后推amount个月份
     * @param startDate 开始时间
     * @param amount   推后月份个数
     * @return 返回日期
     */
    public static Date getDateDelayAmount(Date startDate,int amount){

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(startDate);

        calendar.add(Calendar.MONTH, amount);//增加一个月

        return calendar.getTime();
    }

    /**
     * 获取两个日期相差几个月
     * @param start
     * @param end
     * @return
     */
    public static int getMonths(Date start, Date end) {
        if (start.after(end)) {
            Date t = start;
            start = end;
            end = t;
        }
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        Calendar temp = Calendar.getInstance();
        temp.setTime(end);
        temp.add(Calendar.DATE, 1);

        int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
        int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);

        if ((startCalendar.get(Calendar.DATE) == 1)&& (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month + 1;
        } else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
            return year * 12 + month;
        } else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
            return year * 12 + month;
        } else {
            return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
        }
    }


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


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>