Java时间处理(加一天和减一天)

@Test
public void addTime() throws ParseException{
DateFormat d = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(d.format(new Date()));
Calendar c = Calendar.getInstance();
c.setTime(d.parse("2014-01-01"));
// c.set(Calendar.DAY_OF_MONTH, 1);
c.add(Calendar.DAY_OF_MONTH, 1);
String t = d.format(c.getTime());
System.out.println(t);

}

@Test
public void beforeTime() throws ParseException{
DateFormat d = new SimpleDateFormat("yyyy-MM-dd");
// System.out.println(d.format(new Date()));
Calendar c = Calendar.getInstance();
c.setTime(d.parse("2014-01-11"));
c.set(Calendar.DATE, c.get(Calendar.DATE)-1);
String t = d.format(c.getTime());
System.out.println(t);

}

你可能感兴趣的:(java)