1.取一个时间距离现在是几天?
Calendar now=Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY, 0);
Date firstDate=sdf.parse(String.valueOf(firstDateTime/100));
Calendar firstTime=Calendar.getInstance();
firstTime.setTime(firstDate);
firstTime.set(Calendar.HOUR_OF_DAY, 0);
int dayMillTime=86400000;
long firstDateInt=firstTime.getTimeInMillis()/dayMillTime;
long nowDateInt=now.getTimeInMillis()/dayMillTime;
if(firstDateInt==nowDateInt){
return "今日可约";
}else if(firstDateInt-nowDateInt==1){
return "明日可约";
}else if(firstDateInt-nowDateInt>0&&firstDateInt-nowDateInt<4){
return "三日内可约";
}else if(firstDateInt-nowDateInt>0&&firstDateInt-nowDateInt<8){
return "七日内可约";
}
return null;
注意:java中的时间开始点是1970年的0点,对于中国来讲因为是东八区,所以是"1970-01-01 08:00:00"。
所以 当前时间毫秒数/86400000,可能是100,也有可能是101
2.设置时间为22点,用以下那个方法?
cal.set(Calendar.HOUR_OF_DAY, 22);
cal.set(Calendar.HOUR, 23);
正确的应该是Calendar.HOUR_OF_DAY,因为HOUR的23会被转成11