1、得到当前日期的 字符串格式
//得到当前日期如:20160101 即年月日 的字符串
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateToString = simpleDateFormat.format(new Date());
String prefix = dateToString.substring(0, 4)+ dateToString.substring(5, 7)+dateToString.substring(8, 10);
2、 //取两天前的一天 这天的时间范围
Calendar cBegin = Calendar.getInstance();
Calendar cEnd = Calendar.getInstance();
cBegin.add(Calendar.DAY_OF_MONTH, -2); //两天前
cBegin.set(Calendar.HOUR_OF_DAY, 0);
cBegin.set(Calendar.MINUTE, 0);
cBegin.set(Calendar.SECOND, 0);
cBegin.set(Calendar.MILLISECOND, 0);
Date dateBegin = cBegin.getTime();
cEnd.add(Calendar.DAY_OF_MONTH, -2);
cEnd.set(Calendar.HOUR_OF_DAY, 24);
cEnd.set(Calendar.MINUTE, 0);
cEnd.set(Calendar.SECOND, 0);
cEnd.set(Calendar.MILLISECOND, 0);
Date dateEnd = cEnd.getTime();
3、设置时分秒
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 0);
Date dateEnd = calendar.getTime();
4、日期比较
直接用公共方法:需注意该方法的边界值,查询api再使用,防止出错
if(dateBegin.before(nowDate) && dateEnd.after(nowDate))
当前时间在两个日期之间,边界值自己控制是否加等于号
if(dateBegin.getTime()<nowDate.getTime() && dateEnd.getTime()>=nowDate.getTime())