日期时间处理

目录

1.日期加加减

2.遍历最近15天

3.遍历两个日期


1.日期加加减

   //获取时间加一年或加一月或加一天
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//设置起时间
    //System.out.println("111111111::::"+cal.getTime());
    cal.add(Calendar.YEAR, 1);//增加一年
    cd.add(Calendar.DATE, n);//增加一天  
    cd.add(Calendar.DATE, -10);//减10天  
    cd.add(Calendar.MONTH, n);//增加一个月   
    System.out.println("输出::"+cal.getTime());

2.遍历最近15天

Calendar calendar = Calendar.getInstance();
calendar.setTime(HikDateUtils.getTimesmorning());

for (int i = 0; i < 15; i++) {
    calendar.add(Calendar.DATE, -1);
    dateList.add(HikDateUtils.getDateString(calendar.getTime(), "yyyy-MM-dd"));
}

3.遍历两个日期

public static void main(String[] args) {

    Calendar start = Calendar.getInstance();
    start.set(2016, 3, 10);
    Long startTIme = start.getTimeInMillis();

    Calendar end = Calendar.getInstance();
    end.set(2016, 4, 10);
    Long endTime = end.getTimeInMillis();

    Long oneDay = 1000 * 60 * 60 * 24l;

    Long time = startTIme;
    while (time <= endTime) {
        Date d = new Date(time);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(df.format(d));
        time += oneDay;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_30869501/article/details/85111586