前一天

时间:2009-05-26 21:05:27

标签: java datetime date

  

可能重复:
  How to determine the date one day prior to a given date in Java?

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

4 个答案:

答案 0 :(得分:44)

使用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本身中。

答案 1 :(得分:0)

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

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

我假设你真的不想找到'24 Hours previous',但实际上确实想要一个代表'昨天这个时间'的新Date实例 - 无论哪种方式,你都可以向Calendar实例询问24小时之前的日期到另一天或前一天。

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

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是一个很好的资源。

答案 2 :(得分:-4)

从当时减去1000 * 60 * 60 * 24并创建一个新日期。

Date yesterday = new Date(d.getTime() - (1000*60*60*24));

答案 3 :(得分:-4)

    int dayInMs = 1000 * 60 * 60 * 24;
    Date previousDay = new Date(olddate.getTime() - dayInMs);

就个人而言,如果有很多时间/日期计算,我会选择Joda-time