java date获取日_java 获取几天前和几天后的时间

/**

* 得到几天前的时间

* @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

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

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

测试通过。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值