如何在 Java 中将日期增加一天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/428918/
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 can I increment a date by one day in Java?
提问by user48094
I'm working with a date in this format: yyyy-mm-dd
.
我正在使用这种格式的日期:yyyy-mm-dd
.
How can I increment this date by one day?
如何将此日期增加一天?
回答by krosenvold
Construct a Calendar object and use the method add(Calendar.DATE, 1);
构造一个 Calendar 对象并使用方法 add(Calendar.DATE, 1);
回答by Ross
Use the DateFormat
API to convert the String into a Date object, then use the Calendar
API to add one day. Let me know if you want specific code examples, and I can update my answer.
使用DateFormat
API 将String 转换为Date 对象,然后使用Calendar
API 增加一天。如果您需要特定的代码示例,请告诉我,我可以更新我的答案。
回答by Alex B
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
Calendar cal = Calendar.getInstance();
cal.setTime( dateFormat.parse( inputString ) );
cal.add( Calendar.DATE, 1 );
回答by Dave
Something like this should do the trick:
像这样的事情应该可以解决问题:
String dt = "2008-01-01"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime()); // dt is now the new date
回答by Willi aus Rohr
Take a look at Joda-Time (https://www.joda.org/joda-time/).
看看 Joda-Time ( https://www.joda.org/joda-time/)。
DateTimeFormatter parser = ISODateTimeFormat.date();
DateTime date = parser.parseDateTime(dateString);
String nextDay = parser.print(date.plusDays(1));
回答by Florian R.
Please note that this line adds 24 hours:
请注意,此行增加了 24 小时:
d1.getTime() + 1 * 24 * 60 * 60 * 1000
but this line adds one day
但这条线增加了一天
cal.add( Calendar.DATE, 1 );
On days with a daylight savings time change (25 or 23 hours) you will get different results!
在夏令时更改(25 或 23 小时)的日子里,您会得到不同的结果!
回答by Lisa
Java does appear to be well behind the eight-ball compared to C#. This utility method shows the way to do in Java SE 6 using the Calendar.add method(presumably the only easy way).
与 C# 相比,Java 似乎确实远远落后于八球。这个实用方法展示了在 Java SE 6 中使用Calendar.add 方法(大概是唯一简单的方法)的方法。
public class DateUtil
{
public static Date addDays(Date date, int days)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days); //minus number would decrement the days
return cal.getTime();
}
}
To add one day, per the question asked, call it as follows:
要添加一天,根据所问的问题,请按如下方式调用:
String sourceDate = "2012-02-29";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = format.parse(sourceDate);
myDate = DateUtil.addDays(myDate, 1);
回答by Risav Karna
I prefer to use DateUtilsfrom Apache. Check this http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html. It is handy especially when you have to use it multiple places in your project and would not want to write your one liner method for this.
我更喜欢使用Apache 的DateUtils。检查这个http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html。它非常方便,尤其是当您必须在项目中的多个地方使用它并且不想为此编写一个 liner 方法时。
The API says:
API 说:
addDays(Date date, int amount) : Adds a number of days to a date returning a new object.
addDays(Date date, int amount) :将天数添加到返回新对象的日期。
Note that it returns a new Date object and does not make changes to the previous one itself.
请注意,它返回一个新的 Date 对象,并且不会对前一个对象本身进行更改。
回答by Filip Trajcevski
Date newDate = new Date();
newDate.setDate(newDate.getDate()+1);
System.out.println(newDate);
回答by ROCKY
Apache Commons already has this DateUtils.addDays(Date date, int amount) http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DateUtils.html#addDays%28java.util.Date,%20int%29which you use or you could go with the JodaTime to make it more cleaner.
Apache Commons 已经有这个 DateUtils.addDays(Date date, int amount) http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DateUtils.html#addDays%28java您使用的.util.Date,%20int%29或者您可以使用 JodaTime 使其更干净。