如何确定Java中给定日期前一天的日期?

时间:2022-10-21 10:43:59

I am assuming Java has some built-in way to do this.

我假设Java有一些内置的方法来做到这一点。

Given a date, how can I determine the date one day prior to that date?

在给定日期的情况下,如何确定该日期前一天的日期?

For example, suppose I am given 3/1/2009. The previous date is 2/28/2009. If I had been given 3/1/2008, the previous date would have been 2/29/2008.

例如,假设我被授予3/1/2009。上一个日期是2009年2月28日。如果我获得了3/1/2008,那么上一个日期将是2/29/2008。

7 个解决方案

#1


Use the Calendar interface.

使用日历界面。

Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.DAY_OF_YEAR,-1);
Date oneDayBefore= cal.getTime();

Doing "addition" in this way guarantees you get a valid date. This is valid for 1st of the year as well, e.g. if myDate is January 1st, 2012, oneDayBefore will be December 31st, 2011.

以这种方式进行“添加”可确保您获得有效日期。这也适用于一年中的第一年,例如如果myDate是2012年1月1日,oneDayBefore将于2011年12月31日。

#2


You can also use Joda-Time, a very good Java library to manipulate dates:

您还可以使用Joda-Time,一个非常好的Java库来操作日期:

DateTime result = dt.minusDays(1);

#3


The java.util.Calendar class allows us to add or subtract any number of day/weeks/months/whatever from a date. Just use the add() method:

java.util.Calendar类允许我们在日期中添加或减去任何数量的日/周/月/日。只需使用add()方法:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

Example:

Calendar date = new GregorianCalendar(2009, 3, 1);
date.add(Calendar.DATE, -1);

#4


With java.time.Instant

Date.from(Instant.now().minus(Duration.ofDays(1)))

#5


import java.util.Calendar;
import java.util.GregorianCalendar;
import static java.util.Calendar.*;


public class TestDayBefore {

    public static void main(String... args) {
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.set(YEAR, 2009);
        calendar.set(MONTH, MARCH);
        calendar.set(DAY_OF_MONTH, 1);
        System.out.println(calendar.getTime()); //prints Sun Mar 01 23:20:20 EET 2009
        calendar.add(DAY_OF_MONTH, -1);
        System.out.println(calendar.getTime()); //prints Sat Feb 28 23:21:01 EET 2009

    }
}

#6


With the date4j library :

使用date4j库:

DateTime yesterday = today.minusDays(1);

#7


This would help.

这会有所帮助。

getPreviousDateForGivenDate("2015-01-19", 10);
getPreviousDateForGivenDate("2015-01-19", -10);

public static String getPreviousDateForGivenDate(String givenDate, int datesPriorOrAfter) {
    String saleDate = getPreviousDate(datesPriorOrAfter);

    try {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String[] arr = givenDate.split("-", 3);
        Calendar cal = new GregorianCalendar(Integer.parseInt(arr[0]), Integer.parseInt(arr[1])-1, Integer.parseInt(arr[2]));
        cal.add(Calendar.DAY_OF_YEAR, datesPriorOrAfter);    
        saleDate = dateFormat.format(cal.getTime());

    } catch (Exception e) {
        System.out.println("Error at getPreviousDateForGivenDate():"+e.getMessage());
    }

    return saleDate;
}

#1


Use the Calendar interface.

使用日历界面。

Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.DAY_OF_YEAR,-1);
Date oneDayBefore= cal.getTime();

Doing "addition" in this way guarantees you get a valid date. This is valid for 1st of the year as well, e.g. if myDate is January 1st, 2012, oneDayBefore will be December 31st, 2011.

以这种方式进行“添加”可确保您获得有效日期。这也适用于一年中的第一年,例如如果myDate是2012年1月1日,oneDayBefore将于2011年12月31日。

#2


You can also use Joda-Time, a very good Java library to manipulate dates:

您还可以使用Joda-Time,一个非常好的Java库来操作日期:

DateTime result = dt.minusDays(1);

#3


The java.util.Calendar class allows us to add or subtract any number of day/weeks/months/whatever from a date. Just use the add() method:

java.util.Calendar类允许我们在日期中添加或减去任何数量的日/周/月/日。只需使用add()方法:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

Example:

Calendar date = new GregorianCalendar(2009, 3, 1);
date.add(Calendar.DATE, -1);

#4


With java.time.Instant

Date.from(Instant.now().minus(Duration.ofDays(1)))

#5


import java.util.Calendar;
import java.util.GregorianCalendar;
import static java.util.Calendar.*;


public class TestDayBefore {

    public static void main(String... args) {
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.set(YEAR, 2009);
        calendar.set(MONTH, MARCH);
        calendar.set(DAY_OF_MONTH, 1);
        System.out.println(calendar.getTime()); //prints Sun Mar 01 23:20:20 EET 2009
        calendar.add(DAY_OF_MONTH, -1);
        System.out.println(calendar.getTime()); //prints Sat Feb 28 23:21:01 EET 2009

    }
}

#6


With the date4j library :

使用date4j库:

DateTime yesterday = today.minusDays(1);

#7


This would help.

这会有所帮助。

getPreviousDateForGivenDate("2015-01-19", 10);
getPreviousDateForGivenDate("2015-01-19", -10);

public static String getPreviousDateForGivenDate(String givenDate, int datesPriorOrAfter) {
    String saleDate = getPreviousDate(datesPriorOrAfter);

    try {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String[] arr = givenDate.split("-", 3);
        Calendar cal = new GregorianCalendar(Integer.parseInt(arr[0]), Integer.parseInt(arr[1])-1, Integer.parseInt(arr[2]));
        cal.add(Calendar.DAY_OF_YEAR, datesPriorOrAfter);    
        saleDate = dateFormat.format(cal.getTime());

    } catch (Exception e) {
        System.out.println("Error at getPreviousDateForGivenDate():"+e.getMessage());
    }

    return saleDate;
}