为什么日历中的 DATE_OF_MONTH 是静态最终的(Java API)
我正在浏览 Java API 中的 Calendar 类源代码,并注意到很多变量,例如 DAY_OF_MONTH
、YEAR
等都被声明为 static final
。这让我感到很奇怪,因为我认为日历实例的日期应该是(1)特定于该实例的,即非静态的和(2)可更改的(因此可以设置)。谁能澄清一下吗?谢谢!
I was going through the Calendar class source code in the Java API, and noticed that a lot of variables, e.g. DAY_OF_MONTH
, YEAR
, etc are declared as static final
. This struck me as odd because I thought the date of a Calendar instance should be (1) specific to that instance, i.e. non-static and (2) changeable (so it can be set). Can anyone clarify? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个明显的常量。它并不指该月中的任何特定日期,而是指概念DAY_OF_MONTH。
您可以使用它来指定要获取(或设置)日期的哪个元素。
例如,我想添加(增加日历的日期)一个月:
我想添加一天:
我正在调用相同的函数,第一个参数是日历字段的清单常量想要增加/减少。 (当然,按任何特定字段递增/递减可能会更改其他字段:如果我在 2099 年 12 月 31 日添加一天,则 DAY_OF_MONTH、MONTH 和 YEAR 字段都将发生更改。)
另一种方法是为每个字段使用不同的设置器,例如,
但这会使一些用例的编码变得更加乏味。
OP问:
“严格的”面向对象的答案是,“作为使用日历而不是实现它们的客户端程序员,您不需要了解日历的内部布局或算法”。
真正的答案是 Calendar 是一个接口,因此任何特定的实现类都可以通过多种方式执行这些操作,只要实现遵守公共接口和 Calendar 接口的语义即可。
例如,GregorianCalendar 的实际实现可能是在内部将日期保存为自某个特殊日期以来的秒数,例如 linux“时代”(1970 年 1 月 1 日)或公历的第一个机构(15 年 10 月 15 日)第1582章)
因此,
cal.set(2010, 8, 2)
可能将年份乘以 365 * 24 * 60 * 60,加上任何闰年,8
用于查找到第七个月结束的秒数(同样,考虑闰日,如果有的话),2 添加两天的秒数,等等。It's a manifest constant. It doesn't refer to any particular day of the month, but to the concept DAY_OF_MONTH.
You use it to specify which element of a date you want to get (or set).
E.g., I want to add (increase the Calendar's date by) one month:
I want to add one day:
I'm calling the same function, with the first parameter the manifest constant of the Calendar field I want to increment/decrement by. (Of course, incrementing/decrementing by any particular field may change other fields: If I add one day to December 31 2099, The DAY_OF_MONTH, MONTH and YEAR fields will all be changed.)
The alternative would be to have different setters for each field, e.g,
That would make coding some use cases more tedious however.
The OP asks:
The "strict" Object Oriented answers is, "as a client programmer using Calendars rather than implementing them, you shouldn't need to know about the Calendar's internal layout or algorithms".
The real answer is that Calendar is an interface, so any particular implementing class could do these things any number of ways, so long as the implementation adheres to the public interface and the semantics of the Calendar interface.
The actual implementation of, say, GregorianCalendar is probably that internally it holds the date as some number of seconds since some special date, e.g, the linux "era" (1 January 1970) or the first institution of the Gregorian Calendar (15 October 15 1582).
So
cal.set(2010, 8, 2)
probably multiples the year by 365 * 24 * 60 * 60, plus any leap years, the8
is used to find the number of seconds to the end of the seventh month (again, accounting for a leap day, if any), the 2 adds the number of seconds in two days, etc.这些是日历值的关键。这些值是通过调用
calendar.get(key)
获取的。例如:These are keys to the calendar values. The values are obtained by calling
calendar.get(key)
. For example:...如果您调用 myCalendarObject.get(Calendar.DAY_OF_MONTH),那么您将获得与 myCalendarObject 中的日期相对应的月份中的某一天,就像如果是 8 月 2 日,您将获得“2”。
如果他们创建了名为“getDayOfMonth”、“getMonth”、“getYear”等的函数,可能会更直观。也许这在某种程度上实现起来更有效,或者也许他们认为日历类应该可以扩展到任何日历,所以如果您想创建“MayanCalendar扩展日历”,那么您可以定义新的常量对于 KATUN 和 BAKTUN 等,它将与现有的 set 和 get 函数一起使用。
... and if you call myCalendarObject.get(Calendar.DAY_OF_MONTH), then you will get the day of the month corresponding to the date in myCalendarObject, like if it's August 2, you'll get "2".
It might have been more intuitive if they had created functions called "getDayOfMonth", "getMonth", "getYear", and so forth. Maybe this was somehow more efficient to implement, or maybe they were thinking that as the Calendar class is supposed to be extensible to any calendar, so if you wanted to create, say, "MayanCalendar extends Calendar", that you could then define new constants for KATUN and BAKTUN and so forth and it would work with the existing set and get functions.
DAY_OF_MONTH 只是一个索引,用于标识日历记录的哪个字段代表一月中的某一天。这是一个常数,这就是为什么它是一个常数。
DAY_OF_MONTH is just an index that identifies which field of the Calendar record represents, well, the day of the month. That's a constant, and that's why it's a constant.