如何在Java中增加一天的date?

我得到的格式为yyyy-mm-dd。 我需要增加一天。 我该怎么做?

像这样的事情应该做的伎俩:

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 

与C#相比,Java似乎远远落后于八球。 此实用程序方法显示了使用Calendar.add方法 (大概是唯一简单的方法)在Java SE 6中执行的方法 。

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

要添加一天,每个问题的问题,如下所示:

 String sourceDate = "2012-02-29"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date myDate = format.parse(sourceDate); myDate = DateUtil.addDays(myDate, 1); 

我更喜欢使用Apache的DateUtils 。 查看http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html 。 当你不得不在你的项目中使用它多个地方,并且不希望为此写一个class轮方法时,这是非常方便的。

该API说:

addDays(datedate,int数量):为返回新对象的date添加天数。

请注意,它将返回一个新的Date对象,并且不会对前一个对象进行更改。

 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" ); Calendar cal = Calendar.getInstance(); cal.setTime( dateFormat.parse( inputString ) ); cal.add( Calendar.DATE, 1 ); 

java.time

在Java 8及更高版本中, java.time包使得这几乎是自动的。 ( 教程 )

假设Stringinput和输出:

 import java.time.LocalDate; public class DateIncrementer { static public String addOneDay(String date) { return LocalDate.parse(date).plusDays(1).toString(); } } 

看看Joda-Time( http://joda-time.sourceforge.net/ )。

 DateTimeFormatter parser = ISODateTimeFormat.date(); DateTime date = parser.parseDateTime(dateString); String nextDay = parser.print(date.plusDays(1)); 

构build一个Calendar对象并使用方法add(Calendar.DATE,1);

请注意,这条线增加了24小时:

 d1.getTime() + 1 * 24 * 60 * 60 * 1000 

但是这一行增加了一天

 cal.add( Calendar.DATE, 1 ); 

在夏令时变化的日子(25或23小时),您将得到不同的结果!

Java 8添加了一个新的API来处理date和时间。

使用Java 8,您可以使用以下几行代码:

 // parse date from yyyy-mm-dd pattern LocalDate januaryFirst = LocalDate.parse("2014-01-01"); // add one day LocalDate januarySecond = januaryFirst.plusDays(1); 

你可以使用简单的java.util库

 Calendar cal = Calendar.getInstance(); cal.setTime(yourDate); cal.add(Calendar.DATE, 1); yourDate = cal.getTime(); 
 Date today = new Date(); SimpleDateFormat formattedDate = new SimpleDateFormat("yyyyMMdd"); Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, 1); // number of days to add String tomorrow = (String)(formattedDate.format(c.getTime())); System.out.println("Tomorrows date is " + tomorrow); 

这将给明天的date。 c.add(...)参数可以从1更改为另一个数字以适当增加。

试试这个代码:

 Date d1 = new Date(); Date d2 = new Date(); d2.setTime(d1.getTime() + 1 * 24 * 60 * 60 * 1000); 
 long timeadj = 24*60*60*1000; Date newDate = new Date (oldDate.getTime ()+timeadj); 

这需要从oldDate开始的毫秒数,并增加1天的毫秒数,然后使用Date()公共构造函数创build一个使用新值的date。 这种方法允许您添加1天,或者任意数量的小时/分钟,而不仅仅是整天。

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来使其更清洁。

只需在string中传递date和下一天的date

  private String getNextDate(String givenDate,int noOfDays) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); String nextDaysDate = null; try { cal.setTime(dateFormat.parse(givenDate)); cal.add(Calendar.DATE, noOfDays); nextDaysDate = dateFormat.format(cal.getTime()); } catch (ParseException ex) { Logger.getLogger(GR_TravelRepublic.class.getName()).log(Level.SEVERE, null, ex); }finally{ dateFormat = null; cal = null; } return nextDaysDate; } 

如果你想添加一个单位的时间,你也希望增加其他的字段,你可以安全地使用add方法。 看下面的例子:

 SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.set(1970,Calendar.DECEMBER,31); System.out.println(simpleDateFormat1.format(cal.getTime())); cal.add(Calendar.DATE, 1); System.out.println(simpleDateFormat1.format(cal.getTime())); cal.add(Calendar.DATE, -1); System.out.println(simpleDateFormat1.format(cal.getTime())); 

将打印:

 1970-12-31 1971-01-01 1970-12-31 

在Java 8中,您可以使用java.time.LocalDate

 LocalDate parsedDate = LocalDate.parse("2015-10-30"); //Parse date from String LocalDate addedDate = parsedDate.plusDays(1); //Add one to the day field 

您可以按如下方式转换成java.util.Date对象。

 Date date = Date.from(addedDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); 

您可以按如下方式将LocalDate合并为一个string。

 String str = addedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); 

由于Java 1.5 TimeUnit.DAYS.toMillis(1)看起来更干净。

 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" ); Date day = dateFormat.parse(string); // add the day Date dayAfter = new Date(day.getTime() + TimeUnit.DAYS.toMillis(1)); 

如果你正在使用Java 8 ,那就这样做。

 LocalDate sourceDate = LocalDate.of(2017, Month.MAY, 27); // Source Date LocalDate destDate = sourceDate.plusDays(1); // Adding a day to source date. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Setting date format String destDate = destDate.format(formatter)); // End date 

如果你想使用SimpleDateFormat ,那就这样做。

 String sourceDate = "2017-05-27"; // Start date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); calendar.setTime(sdf.parse(sourceDate)); // parsed date and setting to calendar calendar.add(Calendar.DATE, 1); // number of days to add String destDate = sdf.format(calendar.getTime()); // End date 

使用DateFormat API将string转换为Date对象,然后使用Calendar API添加一天。 让我知道如果你想要特定的代码示例,我可以更新我的答案。

如果使用Java 8, java.time.LocalDatejava.time.format.DateTimeFormatter可以使这个工作非常简单。

 public String nextDate(String date){ LocalDate parsedDate = LocalDate.parse(date); LocalDate addedDate = parsedDate.plusDays(1); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd"); return addedDate.format(formatter); } 
 Date newDate = new Date(); newDate.setDate(newDate.getDate()+1); System.out.println(newDate);