如何在Java中确定给定日期前一天的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/745443/
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
How to determine the date one day prior to a given date in Java?
提问by William Brendel
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。上一个日期是 2/28/2009。如果给我 3/1/2008,那么之前的日期应该是 2/29/2008。
采纳答案by Mike Pone
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 日。
回答by moinudin
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
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);
回答by MahdeTo
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
}
}
回答by Vladimir
回答by Vimalraj
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;
}
回答by Vincnetas
With java.time.Instant
使用 java.time.Instant
Date.from(Instant.now().minus(Duration.ofDays(1)))