在日期中添加天数

发布于 2024-08-04 12:27:57 字数 637 浏览 3 评论 0 原文

我有一个计划需要在 2009 年 1 月 1 日开始,当我开始新的一天时,我的计划将在第二天显示。 这就是我到目前为止所得到的:

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.
}

我收到错误 found void but Expected int, in 'newDay = startDate.add(5, 1);' 我应该怎么办?

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2024-08-11 12:27:57

日历对象有一个 add 方法,允许添加或减去指定字段的值。

例如,

Calendar c = new GregorianCalendar(2009, Calendar.JANUARY, 1);
c.add(Calendar.DAY_OF_MONTH, 1);

用于指定字段的常量可以在 日历 类。

仅供将来参考,Java API 规范包含许多有关以下内容的有用信息:如何使用属于 Java API 的类。


更新:

我发现错误无效,但是
预期 int,在 'newDay =
开始日期.add(5, 1);'我该怎么办
做什么?

add 方法不会返回任何内容,因此,尝试分配调用 Calendar.add 的结果是无效的。

编译器错误表明有人试图将 void 分配给类型为 int 的变量。这是无效的,因为不能将“无”分配给 int 变量。

只是猜测,但这可能就是想要实现的目标:

// 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);

输出:

Thu Jan 01 00:00:00 PST 2009
Fri Jan 02 00:00:00 PST 2009

需要考虑的是 Calendar 实际是什么。

Calendar 不是日期的表示。它表示日历及其当前指向的位置。为了获得日历当前指向的位置的表示,应该获取 日历 中获取“html” rel="noreferrer">日期 /docs/api/java/util/Calendar.html#getTime()" rel="noreferrer">getTime 方法。

The Calendar object has an add method which allows one to add or subtract values of a specified field.

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 Calendar class.

Just for future reference, The Java API Specification contains a lot of helpful information about how to use the classes which are part of the Java API.


Update:

I am getting the error found void but
expected int, in 'newDay =
startDate.add(5, 1);' What should I
do?

The add method does not return anything, therefore, trying to assign the result of calling Calendar.add is not valid.

The compiler error indicates that one is trying to assign a void to a variable with the type of int. This is not valid, as one cannot assign "nothing" to an int variable.

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 Calendar actually is.

A Calendar is 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 Date from the Calendar using the getTime method.

一个人练习一个人 2024-08-11 12:27:57

如果您可以明智地调整它的要求,请将所有日期/时间需求移至 JODA,这是一个更好的库,还有一个额外的好处,几乎所有内容都是不可变的,这意味着多线程是免费的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文