Decrement a date in Java

后端 未结 4 2124
野的像风
野的像风 2021-02-20 03:34

I want to get the previous day (24 hours) from the current time.

e.g if current time is Date currentTime = new Date();

2011-04-25 12:

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-20 04:13

    24 hours and 1 day are not the same thing. But you do both using Calendar:

    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DATE, -1);
    Date d = c.getTime();
    

    If you are going back 24 hours, you would use Calendar.HOUR_OF_DAY

提交回复
热议问题