关于处理日期的工具类

  1. 记录笔记
@Slf4j
public class DateUtil {
    /**
     * 计算日期天数的差
     * @param dateStr1 开始日期
     * @param dateStr2 结束日期
     * @return
     * @throws ParseException
     */
    public static int  daysOfTwo(String dateStr1,String dateStr2) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        //跨年不会出现问题
        //如果时间为:2016-03-18 11:59:59 和 2016-03-19 00:00:01的话差值为 0
        Date fDate= null;
        Date oDate= null;
        try {
            fDate = sdf.parse(dateStr1);
            oDate=sdf.parse(dateStr2);
        } catch (ParseException e) {
            log.error("subtraction  date error {}",e);
            return -1;
        }
        long days=(oDate.getTime()-fDate.getTime())/(1000*3600*24);
        return  (int)days;
    }


    /**
     * 获取下一天的日期
     * @param dateStr
     * @return
     */
    public static String getNextDay(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            log.error("get next date error {}",e);
            return null;
        }
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        // 此处修改为+1则是获取后一天
        calendar.set(Calendar.DATE, day + 1);
        String nextDay = sdf.format(calendar.getTime());
        return nextDay;

    }



    /**
     * Sat Nov 25 2017 00:00:00 GMT+0800 (中国标准时间)  转date
     * @return
     */
    public static Date cnToDate(String dateString){
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
        try{
            Date date  = sdf.parse(dateString); //将字符串改为date的格式
            return date;
        }catch(Exception e){
            log.error("parse date error {}",e);
            return null;
        }
    }

    /**
     * Sat Nov 25 2017 00:00:00 GMT+0800 (中国标准时间)  转 date  to string 格式 yyyy-MM-dd
     * @param dateString
     * @return
     */
    public static String  cnToDateString (String dateString){
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);
        try{
            Date date  = sdf.parse(dateString); //将字符串改为date的格式
            String resDate= new SimpleDateFormat("yyyy-MM-dd").format(date);
            return resDate;
        }catch(Exception e){
            log.error("parse date error {}",e);
            return null;
        }
    }
    /**
     *  时间格式
     * @param time
     * @return
     */
    public static Date timeFormat(String time){
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date  date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            log.error("parse time error {}",e);
            return null;
        }
        return date;
    }


    /**
     *  将2018-08-15T16:00:00.000Z 时间格式转换成Date类型格式
     * @param date  字符串
     * @return
     */
    public static Date getDateFormat(String date){
        try {
            date = date.replace("Z", " UTC");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            Date d = null;
            d = format.parse(date);
            return d;
        }catch (Exception e){
            log.error("parse date error { }",e);
            return null;
        }
    }

    /**
     * 将2018-08-15T16:00:00.000Z 时间格式转换成string 类型 yyyy-MM-dd格式
     * @param date
     * @return
     */
    public static String getDateFormatString(String date){
        try {
            date = date.replace("Z", " UTC");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            Date d = null;
            d = format.parse(date);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formatStr = sdf.format(d);
            return formatStr;
        }catch (Exception e){
            log.error("parse date error { }",e);
            return null;
        }
    }

    /**
     * 获取本周的星期一
     * @param date
     * @return
     */
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 获取下周星期1
     * @param date
     * @return
     */
    public static Date getNextWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 7);
        return cal.getTime();
    }

    /**
     * 获取本周的周日
     * @param date
     * @return
     */
    public static Date getThisWeekSunDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 6);
        return cal.getTime();
    }
// Java获取两个日期之间的所有日期集合
// 1.返回Date的list
 private List<Date> getBetweenDates(Date start, Date end) {
    List<Date> result = new ArrayList<Date>();
    Calendar tempStart = Calendar.getInstance();
    tempStart.setTime(start);
    tempStart.add(Calendar.DAY_OF_YEAR, 1);
    
    Calendar tempEnd = Calendar.getInstance();
    tempEnd.setTime(end);
    while (tempStart.before(tempEnd)) {
        result.add(tempStart.getTime());
        tempStart.add(Calendar.DAY_OF_YEAR, 1);
    }
    return result;
}

2.返回yyyy-MM-dd的list
  public static List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

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

        return days;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值