Calendar 的add方法加负数??

来源:百度知道 编辑:UC知道 时间:2024/05/16 15:01:34
这是日期加天数
这是正数的算法
如何能日期减去天数??
private void calculateDate() {
try {
String s = text.getText();
int count = Integer.valueOf(s).intValue();
Calendar c1 = Calendar.getInstance();
c1.setTime(sysDate);
c1.add(Calendar.DATE, count);
label2.setText("The date is [" + sdf.format(c1.getTime()) + "]");

} catch (Throwable e) {
label2.setText("input date error.");
}
}
}
我都服了自己 我只是改动这个可以出现差c1.add(Calendar.DATE, -count); count前加负号

Calendar now = Calendar.getInstance();
Calendar working;
SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

working = (Calendar) now.clone();
working.add(Calendar.DAY_OF_YEAR, -(365 * 2));
prt(" Two years ago it was: " + formatter.format(working.getTime()));

working = (Calendar) now.clone();
working.add(Calendar.DAY_OF_YEAR, +5);
prt(" In five days it will be: " + formatter.format(working.getTime()));

c1.add(Calendar.DAY_OF_MONTH, -1);