为什么日历中的 DATE_OF_MONTH 是静态最终的(Java API)

发布于 2024-09-12 08:38:14 字数 197 浏览 2 评论 0原文

我正在浏览 Java API 中的 Calendar 类源代码,并注意到很多变量,例如 DAY_OF_MONTHYEAR 等都被声明为 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 技术交流群。

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

发布评论

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

评论(4

指尖微凉心微凉 2024-09-19 08:38:14

这是一个明显的常量。它并不指该月中的任何特定日期,而是指概念DAY_OF_MONTH。

您可以使用它来指定要获取(或设置)日期的哪个元素。

例如,我想添加(增加日历的日期)一个月:

    c.add ( Calendar.MONTH, 1 ) 

我想添加一天:

   c.add ( Calendar.DAY_OF_YEAR, 1 ) ;

我正在调用相同的函数,第一个参数是日历字段的清单常量想要增加/减少。 (当然,按任何特定字段递增/递减可能会更改其他字段:如果我在 2099 年 12 月 31 日添加一天,则 DAY_OF_MONTH、MONTH 和 YEAR 字段都将发生更改。)

另一种方法是为每个字段使用不同的设置器,例如,

 addMonth( int n ) ;
 addDayOfYear( int n);

但这会使一些用例的编码变得更加乏味。


OP问:

任何人都可以告诉我发生了什么,比如说,当您调用 cal.set(2010, 8, 2) (其中 cal 是日历)时?我想知道的是编译器如何将 2 连接到 DATE_OF_MONTH,或者 2 最终在堆中的位置,或者存储 2 的限定变量名称是什么。谢谢所有回答的人!

“严格的”面向对象的答案是,“作为使用日历而不是实现它们的客户端程序员,您不需要了解日历的内部布局或算法”。

真正的答案是 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:

    c.add ( Calendar.MONTH, 1 ) 

I want to add one day:

   c.add ( Calendar.DAY_OF_YEAR, 1 ) ;

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,

 addMonth( int n ) ;
 addDayOfYear( int n);

That would make coding some use cases more tedious however.


The OP asks:

Can anyone walk me through what happens, say, when you invoke cal.set(2010, 8, 2) where cal is a Calendar? What I'd like to know is how the compiler connects 2 to DATE_OF_MONTH, or where that 2 ends up in the heap, or what the qualified variable name is under which that 2 is stored. Thanks all who answered!

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, the 8 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.

痴者 2024-09-19 08:38:14

这些是日历值的关键。这些值是通过调用calendar.get(key)获取的。例如:

int dayOfMonth = concreteCalendar.get(Calendar.DAY_OF_MONTH);

These are keys to the calendar values. The values are obtained by calling calendar.get(key). For example:

int dayOfMonth = concreteCalendar.get(Calendar.DAY_OF_MONTH);
抹茶夏天i‖ 2024-09-19 08:38:14

...如果您调用 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.

泪之魂 2024-09-19 08:38:14

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.

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