import java.util.Calendar;
import java.util.Date; public class main { public static void main(String[] args) { Date today = getXdaysLater(0);
Date oneMonLater = getXdaysLater(30);
Date twoMonLater = getXdaysLater(60);
Date halfYearLater = getXdaysLater(180);
Date oneYearLater = getXdaysLater(365); } /**
* 获取X天后的日期
* @param X
* @return
*/
public static Date getXdaysLater(int X){ Calendar dateTimeNow = Calendar.getInstance();
dateTimeNow.set(Calendar.HOUR_OF_DAY, 0);
dateTimeNow.set(Calendar.MINUTE, 0);
dateTimeNow.set(Calendar.SECOND, 0);
dateTimeNow.set(Calendar.MILLISECOND, 0);
dateTimeNow.add(Calendar.DATE,X);
Date dateTimeResult = dateTimeNow.getTime();
System.out.println(dateTimeResult);
return dateTimeResult;
} }
结果
Tue Nov 07 00:00:00 CST 2017
Thu Dec 07 00:00:00 CST 2017
Sat Jan 06 00:00:00 CST 2018
Sun May 06 00:00:00 CST 2018
Wed Nov 07 00:00:00 CST 2018