Java Calendar add() vs roll() 我们什么时候使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2504141/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:34:38  来源:igfitidea点击:

Calendar add() vs roll() when do we use it?

javacalendar

提问by Pentium10

I know add()adds the specified (signed) amount of time to the given time field, based on the calendar's rules.

我知道add()根据日历的规则将指定的(签名的)时间添加到给定的时间字段中。

And roll()adds the specified (signed) single unit of time on the given time field without changing larger fields.

roll()在给定的时间字段上添加指定的(有符号的)单个时间单位而不更改更大的字段。

I can't think of an everyday usage of roll()I would do everything by add().

我想不出roll()我会做所有事情的日常用法add()

Can you help me out with examples when do we use roll()and when add()?

你能帮我举例说明我们什么时候用roll(),什么时候用add()吗?

EDIT 1

编辑 1

Jodaanswers are not accepted!

不接受乔达回答!

采纳答案by Bozho

  • add()- almost always, as you said
  • roll()- for example you want to "dispense" events in one month. The algorithm may be to proceed a number of days and place the event, then proceed further. When the end of the month is reached, it should start over from the beginning. Hence roll().
  • add()- 几乎总是,正如你所说的
  • roll()- 例如,您想在一个月内“分发”事件。算法可能是进行数天并放置事件,然后进一步进行。到了月底,应该从头开始。因此roll()

回答by Menda

Found in jGuru

jGuru 中找到

  • Calendar.roll()
    Changes a specific unit and leaves 'larger' (in terms of time-month is 'larger' than day) units unchanged. The API example is that given a date of August 31, 1999, rolling by (Calendar.MONTH, 8) yields April 30, 1999. That is, the DAY was changed to meet April's maximum, but the 'larger' unit, YEAR, was unchanged.
  • Calendar.roll()
    更改特定单位并保持“更大”(就时间月而言“大于”天)单位不变。API 示例是给定日期 1999 年 8 月 31 日,按 (Calendar.MONTH, 8) 滚动产生 1999 年 4 月 30 日。也就是说,DAY 已更改以满足四月的最大值,但“更大”的单位 YEAR,没有改变。

roll(): Rolls up 8 months here i.e., adding 8 months to Aug will result in Apr but year remains unchanged(untouched).

roll(): Rolls up 8 months here i.e., adding 8 months to Aug will result in Apr but year remains unchanged(untouched).

  • Calendar.add()
    Will cause the next 'larger' unit to change, if necessary. That is, given a date of August 31, 1999, add(Calendar.MONTH, 8) yields April 30, 2000. add() also forces a recalculation of milliseconds and all fields.
  • Calendar.add()
    如有必要,将导致下一个“更大”的单位发生变化。也就是说,给定日期 1999 年 8 月 31 日, add(Calendar.MONTH, 8) 产生 2000 年 4 月 30 日。add() 还强制重新计算毫秒和所有字段。

add(): Adds months to the current date i.e., adding 8 months to Aug will give Apr of Next Year, hence forces the Year change.

add(): Adds months to the current date i.e., adding 8 months to Aug will give Apr of Next Year, hence forces the Year change.

回答by Tim

I was just asking the same question (which is how I found this page) and someone at my work place (well done, DCK) came up with a suggestion:

我只是问了同样的问题(这就是我找到这个页面的方式),我工作场所的某个人(干得好,DCK)提出了一个建议:

The date selectors on many smart phones (and other similar interfaces) will "roll" the day from the 31st to the 1st without altering the month, similarly for the month field.

许多智能手机(和其他类似界面)上的日期选择器会将日期从 31 日“滚动”到 1 日,而不会改变月份,与月份字段类似。

I can't think of another use ATM and this one could be implemented in other ways, but at least it's an example!

我想不出还有其他用途 ATM 并且可以通过其他方式实现这一点,但至少它是一个例子!

Tim

蒂姆

回答by Jadiel de Armas

Here is an example that will not work. The condition in the loop will never be satisfied, because the roll, once reaching January 31, 2014, will go back to January 1, 2014.

这是一个不起作用的例子。循环中的条件永远不会得到满足,因为滚动一旦到达 2014 年 1 月 31 日,将返回到 2014 年 1 月 1 日。

    Calendar start=new GregorianCalendar();
    start.set(Calendar.YEAR, 2014);
    start.set(Calendar.MONTH, 0);
    start.set(Calendar.DAY_OF_MONTH, 1);
    //January 2, 2014

    Calendar end=new GregorianCalendar();
    end.set(Calendar.YEAR, 2014);
    end.set(Calendar.MONTH, 1);
    end.set(Calendar.DAY_OF_MONTH, 2);
    //February 2, 2014

    while (start.getTime().before(end.getTime())){
        start.roll(Calendar.DATE, 1);
    }