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

来源:互联网 发布:10天学会php 编辑:程序博客网 时间:2024/06/15 12:25
@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);

}
0 0