获取前一天

2022-09-03 04:09:46

可能的重复:
如何在Java中确定给定日期前一天的日期?

如果我有一个 Java.Util.Date 对象,那么获取一个表示过去 24 小时的对象的最佳方式是什么?


答案 1

使用 Java 1.6 java.util.Calendar.add

public static Date subtractDay(Date date) {

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DAY_OF_MONTH, -1);
    return cal.getTime();
}

其他人建议使用Joda Time,它目前是JSR 310,以后应该包含在Java本身中。


答案 2

要记住的重要一点是,Date 类应表示任何时间点,而 Calendar 类用于操作这些时间点。最后,SimpleDateFormat将它们表示为字符串。

因此,最好的方法是使用日历类为您计算新的日期。这将确保任何变幻莫测(夏令时,闰年等)都被考虑在内。

我假设您并不是真的想找到“24小时前”,但实际上确实想要一个表示“昨天这个时候”的新日期实例 - 无论哪种方式,您都可以向日历实例询问另一个日期之前的24小时或1天前的日期。

夏令时就是一个很好的例子。英国于2009年3月26日“崛起”。所以,在凌晨3点之前的1天。在2009年3月26日应该产生3.00a.m。2009 年 3 月 25 日,但 24 小时前将产生凌晨 2:00。

public class DateTests extends TestCase {
  private static String EXPECTED_SUMMER_TIME = "2009.Mar.29 03:00:00";
  private static String EXPECTED_SUMMER_TIME_LESS_DAY = "2009.Mar.28 03:00:00";
  private static String EXPECTED_SUMMER_TIME_LESS_24_HRS = "2009.Mar.28 02:00:00";
  private static String EXPECTED_SUMMER_TIME_LESS_FURTHER_24_HRS = "2009.Mar.27 02:00:00";

  public void testSubtractDayOr24Hours() {

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MMM.dd HH:mm:SS");

    Calendar calendar = Calendar.getInstance();

    // Create our reference date, 3.00 a.m. on the day the clocks go forward (they 'went' forward at 02.00)
    calendar.clear();
    calendar.set(2009, 2, 29, 3, 0);

    Date summerTime = calendar.getTime(); // Sun Mar 29 03:00:00 BST 2009
    String formattedSummerTime = formatter.format(summerTime);
    calendar.add(Calendar.DAY_OF_MONTH, -1);

    // Our reference date less 'a day'
    Date summerTimeLessADay = calendar.getTime(); // Sat Mar 28 03:00:00 GMT 2009
    String formattedSummerTimeLessADay = formatter.format(summerTimeLessADay);

    // reset the calendar instance to the reference day
    calendar.setTime(summerTime);

    // Our reference date less '24 hours' (is not quite 24 hours)
    calendar.add(Calendar.HOUR, -24);
    Date summerTimeLess24Hrs = calendar.getTime(); // Sat Mar 28 02:00:00 GMT 2009
    String formattedSummerTimeLess24Hrs = formatter.format(summerTimeLess24Hrs);

    // Third date shows that taking a further 24 hours from yields expected result
    calendar.add(Calendar.HOUR, -24);
    Date summerTimeLessFurther24Hrs = calendar.getTime(); // Fri Mar 27 02:00:00 GMT 2009
    String formattedSummerTimeLessFurther24Hrs = formatter.format(summerTimeLessFurther24Hrs);

    // reset the calendar once more to the day before
    calendar.setTime(summerTimeLess24Hrs);

    // Take a 'day' from the Sat will yield the same result as date 03 because Daylight Saving is not a factor
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    Date summerTimeLessFurtherDay = calendar.getTime(); // Fri Mar 27 02:00:00 GMT 2009
    String formattedSummerTimeLessFurtherDay = formatter.format(summerTimeLessFurtherDay);

    assert(formattedSummerTime.equals(EXPECTED_SUMMER_TIME));
    assert(formattedSummerTimeLessADay.equals(EXPECTED_SUMMER_TIME_LESS_DAY));
    assert(formattedSummerTimeLess24Hrs.equals(EXPECTED_SUMMER_TIME_LESS_24_HRS));

    assert(formattedSummerTimeLessFurther24Hrs.equals(EXPECTED_SUMMER_TIME_LESS_FURTHER_24_HRS));

    // This last test proves that taking 24 hors vs. A Day usually yields the same result
    assert(formattedSummerTimeLessFurther24Hrs.equals(formattedSummerTimeLessFurtherDay));

  }
}

对于测试日期函数,wwwdot-timeanddate-dot-com是一个很好的资源。