/**
* 得到几天前的时间
* @param d
* @param day
* @return
*/
public static Date getDateBefore(Date d,int day){
Calendar now =Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
return now.getTime();
}
/**
* 得到几天后的时间
* @param d
* @param day
* @return
*/
public static Date getDateAfter(Date d,int day){
Calendar now =Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
return now.getTime();
}
/**
*获取今天的时间的凌晨 到 23点
*
*/
private void initTime(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
this.startTime=sdf.format(cal.getTime())+" 00:00:00";
this.endTime=sdf.format(cal.getTime())+" 23:59:59";
}
//判断是不是昨天.同一天,前天
/**
* @author LuoB.
* @param oldTime 较小的时间
* @param newTime 较大的时间 (如果为空 默认当前时间 ,表示和当前时间相比)
* @return -1 :同一天. 0:昨天 . 1 :至少是前天.
* @throws ParseException 转换异常
*/
private int isYeaterday(Date oldTime,Date newTime) throws ParseException{
if(newTime==null){
newTime=new Date();
}
//将下面的 理解成 yyyy-MM-dd 00:00:00 更好理解点
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String todayStr = format.format(newTime);
Date today = format.parse(todayStr);
//昨天 86400000=24*60*60*1000 一天
if((today.getTime()-oldTime.getTime())>0 && (today.getTime()-oldTime.getTime())<=86400000) {
return 0;
}
else if((today.getTime()-oldTime.getTime())<=0){ //至少是今天
return -1;
}
else{ //至少是前天
return 1;
}
}
//判断是不是今天
private boolean isToday(Date time){
try {
Date nowTime=new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String todayStr = format.format(nowTime);
Date today = format.parse(todayStr);
long starttime=today.getTime();
long endtime=today.getTime()+86400000;
if(starttime<=time.getTime() && time.getTime()<=endtime){
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
0
顶
5
踩
分享到:
2012-09-17 16:36
浏览 13258
评论
3 楼
Luob.
2012-09-20
2 楼
chenzheng8975
2012-09-20
//哈哈 给楼主补充的:
// 得到当前日期
Calendar calendar1 = Calendar.getInstance();
// 加上1天
calendar1.add(Calendar.DAY_OF_YEAR, 1);
// 时间格式化
SimpleDateFormat mFormat1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// 输出日期
System.out.println(mFormat1.format(calendar1.getTime()));
// 一星期的第几天(星期天为第一天)
System.out.println(calendar1.get(Calendar.DAY_OF_WEEK));
// 得到当前日期
Calendar calendar2 = Calendar.getInstance();
System.out.println("****************************");
// 减去1天
calendar2.add(Calendar.DAY_OF_YEAR, -1);
// 时间格式化
SimpleDateFormat mFormat2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// 输出日期
System.out.println(mFormat2.format(calendar2.getTime()));
// 一星期的第几天(星期天为第一天)
System.out.println(calendar2.get(Calendar.DAY_OF_WEEK));
1 楼
chenzheng8975
2012-09-19
测试通过。。。