Answers:
这样的事情应该可以解决问题:
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确实确实落后于8球。该实用程序方法显示了使用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);java.util.Date,java.util.Calendar和java.text.SimpleDateFormat现在的遗产,由取代java.time内置到Java 8和更高等级。请参见Oracle 教程。
                    在Java 8和更高版本上,java.time包使此过程几乎是自动的。(教程)
假设String输入和输出:
import java.time.LocalDate;
public class DateIncrementer {
  static public String addOneDay(String date) {
    return LocalDate.parse(date).plusDays(1).toString();
  }
}LocalDate。
                    我更喜欢使用Apache的DateUtils。检查此http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html。它非常方便,尤其是当您必须在项目中的多个位置使用它并且不想为此编写一个线性方法时。
API说:
addDays(Date date,int amount):向返回新对象的日期增加天数。
请注意,它将返回一个新的Date对象,并且不会更改前一个对象本身。
看看Joda-Time(https://www.joda.org/joda-time/)。
DateTimeFormatter parser = ISODateTimeFormat.date();
DateTime date = parser.parseDateTime(dateString);
String nextDay = parser.print(date.plusDays(1));Java 8添加了用于处理日期和时间的新API。
使用Java 8,您可以使用以下代码行:
// parse date from yyyy-mm-dd pattern
LocalDate januaryFirst = LocalDate.parse("2014-01-01");
// add one day
LocalDate januarySecond = januaryFirst.plusDays(1);januaryFirst.plusDays(1)不是故意的date.plusDays(1)。
                    您可以使用Simple java.util lib
Calendar cal = Calendar.getInstance(); 
cal.setTime(yourDate); 
cal.add(Calendar.DATE, 1);
yourDate = cal.getTime();java.util.Date,java.util.Calendar和java.text.SimpleDateFormat现在的遗产,由取代java.time内置到Java 8和更高等级。请参见Oracle 教程。
                    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);这将给出明天的日期。c.add(...)可以将参数从1更改为另一个数字以适当增加。
Date或Calendar试。
                    如果您使用的是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 dateLocalDate不要使用该类LocalDateTime。
                    long timeadj = 24*60*60*1000;
Date newDate = new Date (oldDate.getTime ()+timeadj);这花费了从oldDate到新纪元以来的毫秒数,并增加了1天的毫秒数,然后使用Date()公共构造函数使用新值创建日期。此方法使您可以添加1天或任意数量的小时/分钟,而不仅仅是整天。
从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));LocalDateJava 8及更高版本的java.time类中的类,以及在ThreeTen-Backport项目中找到的Java 6和Java 7的反向移植。
                    在Java 8中,简单的方法是:
Date.from(Instant.now().plusSeconds(SECONDS_PER_DAY))Instant.now().plus( 1 , ChronoUnit.DAYS )?
                    在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 str = addedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));这很简单,尝试用一个简单的词来解释。得到今天的日期如下
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime());// print today's date
calendar.add(Calendar.DATE, 1);现在,通过采用(常数,值)的calendar.add方法将日期提前一天。这里的常数可以是DATE,小时,分钟,秒等,而值是常数的值。就像一天一样,提前常量为Calendar.DATE,其值为1,因为我们希望提前一天的值。
System.out.println(calendar.getTime());// print modified date which is 明天的日期
谢谢
Apache Commons已具有此DateUtils.addDays(日期,整数值) http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DateUtils.html#addDays%28java .util.Date,%20int%29,您可以使用它,也可以使用JodaTime使其更整洁。
只需在字符串中传递日期和第二天的数目
 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-31startCalendar.add(Calendar.DATE, 1); //Add 1 Day to the current CalenderCalendar在2019
                    如果您使用Java SE 8或更高版本,则应使用新的日期/时间API
 int days = 7;       
 LocalDate dateRedeemed = LocalDate.now();
 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/YYYY");
 String newDate = dateRedeemed.plusDays(days).format(formatter);   
 System.out.println(newDate);如果必须从转换java.util.Date为java.time.LocalDate,则可以使用此方法。
  public LocalDate asLocalDate(Date date) {
      Instant instant = date.toInstant();
      ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
      return zdt.toLocalDate();
  }如果您使用的是Java SE 8之前的版本,建议您使用Joda-Time
Joda-Time提供了Java日期和时间类的质量替代,并且是Java SE 8之前的Java的事实上的标准日期和时间库。
   int days = 7;       
   DateTime dateRedeemed = DateTime.now();
   DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/uuuu");
   String newDate = dateRedeemed.plusDays(days).toString(formatter);   
   System.out.println(newDate);java.time零件图案不正确。它应该dd/MM/uuuu等效于JodaTime。
                    使用DateFormatAPI将String转换为Date对象,然后使用CalendarAPI添加一天。如果您需要特定的代码示例,请告诉我,我可以更新答案。
如果您使用的是Java 8,java.time.LocalDate并且java.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); 
}toString()它生成i yyyy-MM-dd格式的字符串(如果您坚持要记住,那mm是分钟,而MM月份是月份)。
                    您可以从“ org.apache.commons.lang3.time”使用此软件包:
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 Date myNewDate = DateUtils.addDays(myDate, 4);
 Date yesterday = DateUtils.addDays(myDate, -1);
 String formatedDate = sdf.format(myNewDate);  Date在2018年上课不是很好的建议。麻烦的旧日期,时间类,如java.util.Date,java.util.Calendar和java.text.SimpleDateFormat现在的遗产,由取代java.time内置到Java 8的Java 9.请参见类教程由Oracle。参见使用LocalDate类的其他答案。
                    SimpleDateFormat不幸的是,该类注入了一个时区,隐式地应用了JVM的当前默认时区。因此,此代码的结果将随当前默认值的不同而有所不同-并且该默认值可以在运行时随时更改。
                    其实很简单。一天包含86400000毫秒。因此,首先您可以通过使用从系统获取当前时间(以毫秒为单位),System.currentTimeMillis()然后添加84000000 milliSeconds,并使用DateClass生成毫秒的日期格式。
例
String Today = new Date(System.currentTimeMillis()).toString();
今日弦乐将于2019-05-9
String Tommorow = new Date(System.currentTimeMillis() + 86400000).toString();
String Tommorow将在2019-05-10
String DayAfterTommorow = new Date(System.currentTimeMillis() + (2 * 86400000)).toString();
String DayAfterTommorow将是2019-05-11
Date类,该类在多年前被现代的java.time类所取代。如此简单易用:LocalDate.parse( "2019-01-23" ).plusDays( 1 )
                    让我们澄清一下用例:您要执行日历算术并以java.util.Date开头/结尾。
一些方法:
考虑使用java.time.Instant:
Date _now = new Date();
Instant _instant = _now.toInstant().minus(5, ChronoUnit.DAYS);
Date _newDate = Date.from(_instant);Date与现代java.time类(例如)混合使用Instant。该java.time类完全取代传统的类。具体来说,Instant替换java.util.Date。
                    Date newDate = new Date();
newDate.setDate(newDate.getDate()+1);
System.out.println(newDate);Sun Apr 30 16:25:33 CEST 2017说到Mon May 01 16:25:33 CEST 2017。但是,这仍然是不鼓励的解决方案。不仅有理由不推荐使用该方法,而且在2017年,我们还提供了Date该类的很好的替代方法。