- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我遇到了来自 java.util.Calendar 的奇怪行为:
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.Calendar;
public class Tester1 {
@Test
public void test_monthOfDate() {
assertEquals(1, monthOfDate(2013, 1, 30)); // OK
assertEquals(1, monthOfDate(2013, 1, 31)); // OK
// Start of February
assertEquals(2, monthOfDate(2013, 2, 1)); // FAIL
assertEquals(2, monthOfDate(2013, 2, 28)); // FAIL
// to the end of it
// and after that it is okay also
assertEquals(3, monthOfDate(2013, 3, 1)); // OK
}
public int monthOfDate(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
// just a simple get! but seems it is very important
cal.get(Calendar.MONTH);
//
cal.set(Calendar.DAY_OF_MONTH, day);
return cal.get(Calendar.MONTH) + 1;
}
}
我想知道为什么会这样?
最佳答案
问题是您从 2013 年 1 月 30 日开始使用日历。
然后您将年份设置为 2013 - 这不是问题。
然后您将月份设置为 1(即二月)。你希望这里发生什么?实际发生的是它会记住需要将月份设置为 1,但不会重新计算实际时间值。不过,根据 documentation,时间值将在您调用 get
时重新计算。 (强调我的):
set(f, value) changes calendar field f to value. In addition, it sets an internal member variable to indicate that calendar field f has been changed. Although calendar field f is changed immediately, the calendar's time value in milliseconds is not recomputed until the next call to get(), getTime(), getTimeInMillis(), add(), or roll() is made. Thus, multiple calls to set() do not trigger multiple, unnecessary computations. As a result of changing a calendar field using set(), other calendar fields may also change, depending on the calendar field, the calendar field value, and the calendar system. In addition, get(f) will not necessarily return value set by the call to the set method after the calendar fields have been recomputed. The specifics are determined by the concrete calendar class.
当您尝试将“1 月 30 日”更改为“2 月 30 日”并强制进行计算时,实际上发生的是您在我的框中结束于 3 月 2 日 - 但它可能与您的不同实现。
最好的修复是:
Calendar.set(year, month, date)
而不是为了避免这个订购问题关于java - 二月 java.util.Calendar 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14605360/
也许是一个愚蠢的问题,但我有一个在 Java 文档中没有找到的问题。 Calendar.get(Calendar.DAY_OF_WEEK) 的值是否会根据 Calendar.getFirstDayOf
假设以下代码在 2009 年 8 月 22 日(星期六)执行 Calendar c = Calendar.getInstance(); c.set(Calendar.DAY_OF_WEEK
我正在使用以下代码检查所选月份是否为一月: if (calendar.get(Calendar.MONTH) == Calendar.JANUARY) { ... } 这给了我一个 lint
我有两个 Calendar 变量,分别命名为 calendar1 和 calendar2 其中存储了一些日历值。 我想比较这些变量的 MINUTE 值。 我找到了两种方法,但我想知道有什么区别以及哪一
根据文档 ( here ),Google 提供了一些相同的范围: https://www.googleapis.com/auth/calendar读/写访问到日历 https://www.google
我想问一下,是否可以在不调用 Google 日历 API(需要互联网连接)或启动 native 日历 Activity (不需要)的情况下添加日历事件?只需将日历事件添加到设备 native 日历中(
我们可以从谷歌日历设置中获取工作时间数据吗?我已经了解了日历的 API : https://developers.google.com/calendar/v3/reference/settings/g
我正在尝试使用 Exchange Web 服务访问日历数据,但我似乎无法弄清楚如何访问其他用户共享的日历(当它不是他们的默认日历时)。假设我公司的另一个用户创建了一个共享日历并与我共享,我什至找不到日
我对 Java/Android 中的日期比较有点困惑 DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm");
我正在使用Google的Calendar API,但遇到了一个问题。当我设置要插入的事件的dateTime时,需要设置小时偏移量,但是由于o DST的原因,它需要一个小时。我可以为日历设置一个属性,以
在Google日历的常规网络用户界面中,当我添加事件时,可以选择将其设置为“提醒”,而不是“事件”。 我正在尝试使用Python API进行复制,但是似乎找不到有关如何执行该操作的信息。我找到的所有文
我们可以使用这个link通过参数向Google日历添加新事件 https://www.google.com/calendar/render? action=TEMPLATE& text=EventNa
除了在纪元显式设置它们之外,是否有任何现有方法可以使用日历 API 来获取填充纪元时间的日历?我所能做的就是获取当前时间。 最佳答案 没有预定义的构造函数或工厂方法来执行此操作,但它相当简单: Cal
我试图在我的应用程序上次更新时显示在 TextView 中(例如,“上次更新时间为 12:13”)。我正在尝试使用 Calendar 实例,我认为我理解正确,但我似乎遇到了麻烦. 我知道要获取一个实例
这个问题在这里已经有了答案: Calendar.before(Object when), why Object? (3 个答案) 关闭 8 年前。 我遇到了问题,因为我试图将日期传递给 Calend
在IOS5上使用获取所有日历时 EKEventStore *eventStore = [[EKEventStore alloc] init]; NSArray * calendars = [event
当我运行以下代码时: int year = 2017; int month = 7; int dayOfMonth = 10; Calendar dateOfBirth = new Gregorian
要么我不理解方法 getActualMaximum(int) 或字段 WEEK_OF_YEAR,要么涉及 Sun 错误(或所有三个)...有人可以向我解释为什么(至少在德语语言环境中...) 以下代码
我正在检查我几年前写的代码。然后我意识到 Android Studio 在 Calendar.AM 处给出了注释,它说它必须是 Calendar.SUNDAY、Calendar.MONDAY 之一等等
我需要将给定日期复制 100 次(我无法通过引用传递)。我想知道以下两个中哪个是更好的选择 newTime=Calendar.getInstance().setTime(originalDate);
我是一名优秀的程序员,十分优秀!