简体   繁体   English

Calendar.getInstance()...基本问题

[英]Calendar.getInstance()… basic question

Does Calendar.getInstance( ) work like a regular singleton in the sense that if I've called getInstance somewhere else and set the day, month, etc then if I call Calendar.getInstance() somewhere else those fields will be set to whatever I set them before? Calendar.getInstance( )是否像常规单例一样工作,如果我在其他地方调用getInstance并设置日,月等,那么如果我在其他地方调用Calendar.getInstance()那些字段将设置为任何我以前设置它们? In other words, if I want the Calendar.getInstance() to return a Calendar object with the current time and day, etc, what do I need to do? 换句话说,如果我希望Calendar.getInstance()返回具有当前时间和日期等的Calendar对象,我还需要做什么? Just call clear() ? 只需调用clear() Does that reset the instance to the current time, etc? 是否将实例重置为当前时间等?

My apologies if this is a stupid question. 如果这是一个愚蠢的问题,我道歉。

No, Calendar.getInstance() is not returning a singleton instance. 不, Calendar.getInstance()不返回单例实例。 The point of using getInstance() is that it will take the default locale and time-zone into account when deciding which Calendar implementation to return and how to initialize it. 使用getInstance()是,在决定返回哪个Calendar实现以及如何初始化它时,它将考虑默认的语言环境和时区。

So you don't need to do anything to get a second Calendar with the current time, just call Calendar.getInstance() again. 所以你不需要做任何事情来获得当前时间的第二个日历,只需再次调用Calendar.getInstance()

Generally you can use the API documentation (see source) for questions like this. 通常,您可以使用API​​文档(请参阅源代码)来解决此类问题。 From the API documentation: 从API文档:

Calendar's getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time: Calendar的getInstance方法返回一个Calendar对象,其日历字段已使用当前日期和时间进行初始化:

  Calendar rightNow = Calendar.getInstance(); 

Source: http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html 资料来源: http//download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM