Java 前一天

Java 前一天,java,datetime,date,Java,Datetime,Date,可能重复: 如果我有一个Java.Util.Date对象,那么获取表示过去24小时的对象的最佳方法是什么?从时间中减去1000*60*60*24,然后创建一个新的日期 Date yesterday = new Date(d.getTime() - (1000*60*60*24)); 使用Java 1.6: 其他人建议使用,这是目前的,以后应该包括在Java本身中 int dayInMs = 1000 * 60 * 60 * 24; Date previousDay = new

可能重复:


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

从时间中减去1000*60*60*24,然后创建一个新的日期

Date yesterday = new Date(d.getTime() - (1000*60*60*24));
使用Java 1.6:

其他人建议使用,这是目前的,以后应该包括在Java本身中

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

就我个人而言,如果有大量的时间/日期计算,我会同意。

需要记住的重要一点是,date类应该表示任何时间点,而Calendar类用于处理这些时间点。最后,SimpleDataFormat将它们表示为字符串

因此,最好的方法是使用Calendar类为您计算新日期。这将确保考虑到任何异常情况(夏令时、闰年等)

我假设您并不真的想要查找“24小时前”,但实际上确实想要一个表示“昨天这个时间”的新日期实例——无论哪种方式,您都可以要求日历实例在另一个日期前24小时或前一天输入日期

夏时制就是一个很好的例子。2009年3月26日,英国“突飞猛进”。所以,凌晨3点前1天。2009年3月26日的收益率应为凌晨3点。2009年3月25日,但24小时前将产生凌晨2点

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

这将在夏令时切换期间产生错误的结果实际上,日历是错误的。问题发生在过去的24小时,而不是昨天的同一时间。几乎可以肯定问题本身是错误的,即OP没有考虑到这个特殊情况,并且在这一点上与标题“获取前一天”不一致我会删除方法上下文-它没有添加任何内容,并让我为它缺乏灵活性而感到畏缩。期待1.6中的这一点…日期在JavaBad代码中一直是我的一个错误,夏令时转换将关闭1小时正如您在上面的评论中所指出的,Nathaniel可能不知道其中的区别,所以他需要复述他的真实意思。我明白了。。。我给了文本(“24小时”)更多的权重,而不是标题(“前一天”)。切换到日历对象,这样做就100%容易了。Dupe:如果问题是24小时而不是一天,那么可能不是Dupe。你应该知道,由于夏令时,有些日子有23或25个小时,所以“前一天”与“前24小时”是不同的。日期是令人讨厌的事情。什么是正确的:“过去的24小时”还是“前一天的同一小时”?((计算机做的是你编程它要做的,而不是你想要它做的…)
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));

  }
}