Java 将日期添加到日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1416909/
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
adding days to a date
提问by Karen
I have a program that needs to start on 1/1/09 and when I start a new day, my program will show the next day. This is what I have so far:
我有一个程序需要在 1/1/09 开始,当我开始新的一天时,我的程序将在第二天显示。这是我到目前为止:
GregorianCalendar startDate = new GregorianCalendar(2009, Calendar.JANUARY, 1);
SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy");
public void setStart()
{
startDate.setLenient(false);
System.out.println(sdf.format(startDate.getTime()));
}
public void today()
{
newDay = startDate.add(5, 1);
System.out.println(newDay);
//I want to add a day to the start day and when I start another new day, I want to add another day to that.
}
I am getting the error found void but expected int, in 'newDay = startDate.add(5, 1);' What should I do?
我在'newDay = startDate.add(5, 1);'中发现错误为void但预期为int 我该怎么办?
采纳答案by coobird
The Calendarobject has an addmethod which allows one to add or subtract values of a specified field.
该Calendar对象具有一种add方法,该方法允许添加或减去指定字段的值。
For example,
例如,
Calendar c = new GregorianCalendar(2009, Calendar.JANUARY, 1);
c.add(Calendar.DAY_OF_MONTH, 1);
The constants for specifying the field can be found in the "Field Summary" of the Calendarclass.
用于指定字段的常量可以在类的“字段摘要”中找到Calendar。
Just for future reference, The Java API Specificationcontains a lot of helpful information about how to use the classes which are part of the Java API.
仅供将来参考,Java API 规范包含许多有关如何使用属于 Java API 一部分的类的有用信息。
Update:
更新:
I am getting the error found void but expected int, in 'newDay = startDate.add(5, 1);' What should I do?
我在'newDay = startDate.add(5, 1);'中发现错误为void但预期为int 我该怎么办?
The addmethod does not return anything, therefore, trying to assign the result of calling Calendar.addis not valid.
该add方法不返回任何内容,因此,尝试分配调用结果Calendar.add是无效的。
The compiler error indicates that one is trying to assign a voidto a variable with the type of int. This is not valid, as one cannot assign "nothing" to an intvariable.
编译器错误表明有人试图将 a 分配给void类型为 的变量int。这是无效的,因为不能为int变量分配“无” 。
Just a guess, but perhaps this may be what is trying to be achieved:
只是一个猜测,但也许这可能是试图实现的目标:
// Get a calendar which is set to a specified date.
Calendar calendar = new GregorianCalendar(2009, Calendar.JANUARY, 1);
// Get the current date representation of the calendar.
Date startDate = calendar.getTime();
// Increment the calendar's date by 1 day.
calendar.add(Calendar.DAY_OF_MONTH, 1);
// Get the current date representation of the calendar.
Date endDate = calendar.getTime();
System.out.println(startDate);
System.out.println(endDate);
Output:
输出:
Thu Jan 01 00:00:00 PST 2009
Fri Jan 02 00:00:00 PST 2009
What needs to be considered is what Calendaractually is.
需要考虑的是Calendar实际情况。
A Calendaris not a representation of a date. It is a representation of a calendar, and where it is currently pointing at. In order to get a representation of where the calendar is pointing at at the moment, one should obtain a Datefrom the Calendarusing the getTimemethod.
ACalendar不是日期的表示。它是日历的表示,以及它当前指向的位置。为了获得当前日历指向的位置的表示,应该Date从CalendarusinggetTime方法中获得 。
回答by user44242
If you can swing it requirement wise, move all your date/time needs to JODA, which is a much better library, with the added bonus that almost everything is immutable, meaning multithreading comes in for free.
如果您可以明智地调整它,请将您所有的日期/时间需求移动到 JODA,这是一个更好的库,附加的好处是几乎所有东西都是不可变的,这意味着多线程是免费的。

