How to subtract X days from a date using Java calendar?

后端 未结 10 1248
無奈伤痛
無奈伤痛 2020-11-22 15:40

Anyone know a simple way using Java calendar to subtract X days from a date?

I have not been able to find any function which allows me to directly subtract X days fr

10条回答
  •  难免孤独
    2020-11-22 15:42

    Eli Courtwright second solution is wrong, it should be:

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(Calendar.DATE, -days);
    date.setTime(c.getTime().getTime());
    

提交回复
热议问题