Decrement a date in Java

后端 未结 4 2078
野的像风
野的像风 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:21

    I would suggest you use Joda Time to start with, which is a much nicer API. Then you can use:

    DateTime yesterday = new DateTime().minusDays(1);
    

    Note that "this time yesterday" isn't always 24 hours ago though... you need to think about time zones etc. You may want to use LocalDateTime or Instant instead of DateTime.

提交回复
热议问题