Java 获取前一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/912762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:00:53  来源:igfitidea点击:

Get Previous Day

javadatetimedate

提问by Nathaniel Flath

Possible Duplicate:
How to determine the date one day prior to a given date in Java?

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

If I have a Java.Util.Date object, what is the best way to get an object representing the 24 hours in the past of it?

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

采纳答案by Jared Oberhaus

Using Java 1.6 java.util.Calendar.add:

使用 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();
}

Others suggest using Joda Time, which is currently JSR 310, and should later be included in Java itself.

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

回答by user101884

subtract 1000*60*60*24 from the time and create a new date.

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

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

回答by Blake Pettersson

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

Personally if there are a lot of time/date calculations, I'd go with Joda-time.

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

回答by Blake Pettersson

The important thing to remember is that the Date class should represent any points in time whilst the Calendar class is used to manipulate those points in time. Last of all, SimpleDateFormat will represent them as Strings.

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

So, the best way is to use the Calendar class to calculate the new Date for you. This will ensure that any vagaries (Daylight Saving, Leap Years and the like) are accounted for.

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

I'm assuming that you don't really want to find '24 Hours previous' but actually do want a new Date instance representing 'this time yesterday' - either way, you can ask the Calendar instance for a Date 24Hours prior to another or 1 Day prior.

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

The Daylight savings is a great example. The UK 'sprang forward' on the 26th March 2009. So, 1 day prior to 3.00a.m. on the 26.Mar.2009 should yield 3.00a.m. 25.Mar.2009 but 24 Hrs prior will yield 2.00a.m.

夏令时就是一个很好的例子。英国在 2009 年 3 月 26 日“向前冲”。因此,2009 年 3 月 26 日凌晨 3 点前 1 天应该产生 2009 年 3 月 25 日凌晨 3 点,但 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));

  }
}

For testing date functions, wwwdot-timeanddate-dot-com is a great resource.

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