时间的增加,减少,以及时间差

 时间的加减

public Date setTime(Date time1) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(time1);  //time1为初始时间
            calendar.add(Calendar.DAY_OF_MONTH, 1);//在初始时间加上1天
            calendar.add(Calendar.DAY_OF_MONTH, -1);//在初始时间减去1天
            calendar.add(Calendar.HOUR, 2);//在初始时间加上2小时
            calendar.add(Calendar.HOUR, -2);//在初始时间减去2小时
            calendar.add(Calendar.MINUTE, 3);//在初始时间加上3分钟
            calendar.add(Calendar.SECOND, 4);//在初始时间加上4秒
            calendar.add(Calendar.MILLISECOND, 5);//在初始时间加上5毫秒
            Date time2 = calendar.getTime();
            return time2;   //time2为变更后的最终时间
}

两个时间点间的时间差

mysql实现:
SELECT TIMESTAMPDIFF(MINUTE,start_time,end_time);
 SECOND 秒  
 MINUTE 分钟  
 HOUR 时间  
 DAY 天 
 MONTH 月  
 YEAR 年  



 java代码实现:
public String restTime(Date startTime, Date endTime) {
        //与当前时间的差值
//        long rest = endTime.getTime() - new Date(System.currentTimeMillis()).getTime();
        //与固定时间的差值
        long rest = endTime.getTime() - startTime.getTime();
        long d = rest / (24 * 60 * 60 * 1000);
        long h = (rest / (60 * 60 * 1000) - d * 24);
        long m = ((rest / (60 * 1000)) - d * 24 * 60 - h * 60);
        d = d > 0 ? d : -d;
        h = h > 0 ? h : -h;
        m = m > 0 ? m : -m;
        String a = "相差" + d + "天" + h + "小时" + m + "分";
        return a;
    }

 两个时间点间的日期集合 (注释部分不包含begin和end)

private static List<String> getBetweenDates(String begin, String end) {
        try {
            List<String> result = new ArrayList<String>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Calendar tempStart = Calendar.getInstance();
            Date beginDate = sdf.parse(begin);
            Date endDate = sdf.parse(end);
            tempStart.setTime(beginDate);

//            Calendar tempEnd = Calendar.getInstance();
//            tempStart.add(Calendar.DAY_OF_YEAR, 1);
//            tempEnd.setTime(endDate);
//            while (tempStart.before(tempEnd)) {
//                result.add(sdf.format(tempStart.getTime()));
//                tempStart.add(Calendar.DAY_OF_YEAR, 1);
//            }

            while (beginDate.getTime() <= endDate.getTime()) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
                beginDate = tempStart.getTime();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

 两个时间点间的月份集合

public static List<Integer> getMonthBetween(String minDate, String maxDate) {
        try {
            ArrayList<Integer> result = new ArrayList<Integer>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
//            min.setTime(minDate);
            min.setTime(sdf.parse(minDate));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
//            max.setTime(maxDate);
            max.setTime(sdf.parse(maxDate));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            Calendar curr = min;
            while (curr.before(max)) {
                result.add(Integer.valueOf(sdf.format(curr.getTime())));
                curr.add(Calendar.MONTH, 1);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

 获取当月所有日期的集合

public static List<String> getDateOfTheMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        int month = cal.get(Calendar.MONTH);
        while (cal.get(Calendar.MONTH) == month) {
            list.add(sdf.format(cal.getTime()));
            cal.add(Calendar.DATE, 1);
        }
        return list;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值