1.获取过去一天,过去一月,过去一年的日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calendar.getInstance(); //过去九天 c.setTime(new Date()); c.add(Calendar.DATE, - 9); Date d = c.getTime(); String day = format.format(d); System.out.println("过去九天:"+day); //过去一月 c.setTime(new Date()); c.add(Calendar.MONTH, -1); Date m = c.getTime(); String mon = format.format(m); System.out.println("过去一个月:"+mon); //过去三个月 c.setTime(new Date()); c.add(Calendar.MONTH, -3); Date m3 = c.getTime(); String mon3 = format.format(m3); System.out.println("过去三个月:"+mon3); //过去一年 c.setTime(new Date()); c.add(Calendar.YEAR, -1); Date y = c.getTime(); String year = format.format(y); System.out.println("过去一年:"+year);
2.获取截至今天之前某一段时间的时间集
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //获取当前时间作为endTime截至时间Date todaydate = new Date();String endTime = format.format(todaydate); Calendar c = Calendar.getInstance(); //过去九天的日期为起始日期c.setTime(new Date());c.add(Calendar.DATE, - 9);Date d = c.getTime();String day = format.format(d);
//获得该起止时间段的时间集
Set<String> dayset1 = null;Set<String> dayset2 = null; //两种时间格式集
dayset1 = TimeUtil.getDaysBetween(beginTime,endTime, TimeUtil.MMDD);dayset2= TimeUtil.getDaysBetween(beginTime, endTime, TimeUtil.YYYYMMDD);
原文地址:https://www.cnblogs.com/lynn20/p/11681809.html
时间: 2024-10-12 22:37:13