Java 获取Date的“昨天”和“明天”
Java 获取Date的“昨天”和“明天”
使用日历类:Calendar
@Test
public void dateTest() {
Date today = new Date();
for(int i=0;i<10;i++) {
today = yesterday(today);
System.out.println(today);
}
System.out.println("------------");
for(int i=0;i<10;i++) {
today = tomorrow(today);
System.out.println(today);
}
}
/**
* 返回昨天
* @param today
* @return
*/
public Date yesterday(Date today) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(today);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
return calendar.getTime();
}
/**
* 返回明天
* @param today
* @return
*/
public Date tomorrow(Date today) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(today);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
return calendar.getTime();
}