public abstract class Calendar
extends Object implements Serializable, Cloneable, Comparable<Calendar>
| java.lang.Object | |
| java.util.Calendar | |
| |
所述Calendar类是一个抽象类,可以为在某一特定时刻和一组之间的转换的方法calendar fields如YEAR , MONTH , DAY_OF_MONTH , HOUR ,等等,以及用于操纵该日历字段,如获取的日期下个星期。 即时时间可以用毫秒值来表示,该值与1970年1月1日00:00:00.000 GMT(公历) Epoch的偏移量(格里高利)。
该类还提供了在包外部实现具体日历系统的附加字段和方法。 这些字段和方法定义为protected 。
像其他区域敏感类一样, Calendar提供了一个类方法, getInstance ,用于获取此类型的通用对象。 Calendar的getInstance方法返回一个Calendar对象,其日历字段已用当前日期和时间初始化:
Calendar rightNow = Calendar.getInstance();
一个Calendar对象可以生成实现特定语言和日历样式(例如日文 - 格里历,日文 - 繁体)的日期时间格式所需的所有日历字段值。 Calendar定义了某些日历字段返回的值的范围及其含义。 例如,日历系统的第一个月的所有日历的值为MONTH == JANUARY 。 其他值由具体子类定义,例如ERA 。 有关详细信息,请参阅个别字段文档和子类文档。
日历字段值可以通过调用set方法来设置。 在Calendar设置的任何字段值在需要计算其时间值(从Epoch起的毫秒数)或日历字段的值之前不会被解释。 调用get , getTimeInMillis , getTime , add和roll涉及这样的计算。
Calendar有两种解释日历字段的模式, 宽松和不宽松 。 当Calendar处于宽松模式时,它会接受比它产生的更宽范围的日历字段值。 当Calendar重新计算get()返回的日历字段值时,所有日历字段都将被标准化。 例如,宽松GregorianCalendar解释MONTH == JANUARY , DAY_OF_MONTH == 32如2月1日。
当Calendar处于非宽松模式时,如果其日历字段中存在任何不一致,则会引发异常。 例如,一个GregorianCalendar始终会生成1和月份长度之间的DAY_OF_MONTH值。 如果任何超出范围的字段值已设置,则非宽松GregorianCalendar在计算其时间或日历字段值时会抛出异常。
Calendar defines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in first week (from 1 to 7). These numbers are taken from the locale resource data when a
Calendar is constructed. They may also be specified explicitly through the methods for setting their values.
在设置或获取WEEK_OF_MONTH或WEEK_OF_YEAR字段时, Calendar必须将该月份或年份的第一周确定为参考点。 一个月或一年的第一周被定义为从getFirstDayOfWeek()开始的最早的七天期间,并且该月或该年至少包含getMinimalDaysInFirstWeek()天。 在第一周之前,编号为......,-1,0的周; 编号2,3,...的周数跟随它。 请注意,由get()返回的归一化编号可能会有所不同。 例如,一个特定的Calendar子类可能会指定一年前的第1周之前一周为上一年的第n周。
Calendar will resolve calendar field values to determine the date and time in the following way.
如果日历字段值存在冲突,则Calendar会为最近设置的日历字段赋予优先级。 以下是日历字段的默认组合。 将使用由最近设置的单个字段确定的最近的组合。
For the time of day fields:YEAR + MONTH + DAY_OF_MONTH YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK YEAR + DAY_OF_YEAR YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
HOUR_OF_DAY AM_PM + HOUR
如果有任何日历字段的值未在所选字段组合中设置,则Calendar将使用其默认值。 每个字段的默认值可能因混凝土日历系统而异。 例如, GregorianCalendar ,字段的默认值是相同的时代的开始:即YEAR = 1970 , MONTH = JANUARY , DAY_OF_MONTH = 1 ,等等。
注意:在某些单数时间的解释中可能存在一些含糊不清的情况,这些问题可以通过以下方式解决:
日期或时间格式字符串不是日历定义的一部分,因为这些字符串在运行时必须可由用户修改或覆盖。 使用DateFormat来格式化日期。
set(),
add(), and
roll().
set(f, value)改变日历字段f至value 。 另外,它设置一个内部成员变量来指示日历字段f已被更改。 尽管日历字段f立即更改,以毫秒为单位日历的时间值不重新计算,直到下一次调用get() , getTime() , getTimeInMillis() , add() ,或roll()而成。 因此,多次调用set()不会触发多次不必要的计算。 由于使用set()更改日历字段,其他日历字段也可能会更改,具体取决于日历字段,日历字段值和日历系统。 另外,在重新计算日历字段后, get(f)会通过调用set方法返回value 。 细节由具体的日历类决定。
例如 :考虑一个GregorianCalendar最初被设置为1999调用8月31日, set(Calendar.MONTH, Calendar.SEPTEMBER)套日至9月31日,1999年这是一个临时的内部表示解析为1999年10月1日,如果getTime() ,然后调用。 但是,在致电set(Calendar.DAY_OF_MONTH, 30)之前致电getTime()日期设置为1999年9月30日,因为在set()本身之后不会发生重新计算。
add(f, delta)增加delta现场f 。 这相当于通过两次调整调用set(f, get(f) + delta) :
添加规则1 。 字段的值
f通话减去字段的值后f呼叫之前是delta,模内所出现的字段中的任何溢出f。 当字段值超出其范围时会发生溢出,因此,下一个较大的字段会递增或递减,并将字段值调整回其范围。添加规则2 。 如果一个较小的场预计是不变的,但由于场
f改变或其他约束(如时区偏移改变)后其最小值或最大值的变化,它不可能等于其先前值,则其值调整为尽可能接近其预期值。 较小的字段代表较小的时间单位。HOUR是比DAY_OF_MONTH小的字段。 没有对不期望不变的小字段进行调整。 日历系统确定哪些字段预期是不变的。
另外,与 set()不同, add()强制立即重新计算日历的毫秒数和所有字段。
例如 :考虑一个GregorianCalendar最初被设置为1999调用8月31日, add(Calendar.MONTH, 13)套日历9月30日,2000年添加规则1套MONTH场至九月,因为加入13个月月给明年九月。 由于DAY_OF_MONTH在GregorianCalendar中的9月份不能为31,所以添加规则2将DAY_OF_MONTH设置为30,即最接近的可能值。 虽然这是一个较小的字段, DAY_OF_WEEK并未按规则2进行调整,因为GregorianCalendar的月份发生更改时预计会发生更改。
roll(f, delta)将roll(f, delta)添加到delta字段中, f不更改较大的字段。 这相当于调用add(f, delta)进行以下调整:
滚动规则 。 通话结束后,较大的字段保持不变。 较大的字段代表较大的时间单位。
DAY_OF_MONTH是一个比HOUR更大的字段。
示例 :请参阅 roll(int, int) 。
用法模型 。 为了激发add()和roll()的行为,请考虑一个用户界面组件,其中包含月份,日期和年份的递增和递减按钮,以及底层的GregorianCalendar 。 如果界面显示为1999年1月31日,并且用户按下月份增量按钮,那么它应该读取什么? 如果底层实现使用set() ,它可能会在1999年3月3日阅读。更好的结果是1999年2月28日。此外,如果用户再次按下月份增量按钮,则应该阅读1999年3月31日,而不是1999年3月28日。通过保存原始日期并使用add()或roll() ,取决于是否应该影响较大的字段,用户界面可以像大多数用户直观地预期的那样行为。
常量(Constants) |
|
|---|---|
int |
ALL_STYLES
|
int |
AM
|
int |
AM_PM
|
int |
APRIL
|
int |
AUGUST
|
int |
DATE 指示月份的日期的 |
int |
DAY_OF_MONTH 指示月份的日期的 |
int |
DAY_OF_WEEK
|
int |
DAY_OF_WEEK_IN_MONTH
|
int |
DAY_OF_YEAR
|
int |
DECEMBER
|
int |
DST_OFFSET 指示以毫秒为单位的夏令时偏移的 |
int |
ERA 指示时间的 |
int |
FEBRUARY
|
int |
FIELD_COUNT 由 |
int |
FRIDAY 指示星期五的 |
int |
HOUR 表示上午或下午的小时的 |
int |
HOUR_OF_DAY 表示当天的小时的 |
int |
JANUARY
|
int |
JULY
|
int |
JUNE
|
int |
LONG 样式说明符 |
int |
MARCH
|
int |
MAY
|
int |
MILLISECOND 表示第二秒内的毫秒的 |
int |
MINUTE 指示一小时内分钟的 |
int |
MONDAY 指示星期一的 |
int |
MONTH 表示月份的字段编号为 |
int |
NOVEMBER
|
int |
OCTOBER
|
int |
PM
|
int |
SATURDAY 指示星期六的 |
int |
SECOND 表示在一分钟内的第二秒的 |
int |
SEPTEMBER
|
int |
SHORT 样式说明符为 |
int |
SUNDAY 指示周日的 |
int |
THURSDAY 指示周四的 |
int |
TUESDAY 指示星期二的 |
int |
UNDECIMBER
|
int |
WEDNESDAY 指示周三的 |
int |
WEEK_OF_MONTH
|
int |
WEEK_OF_YEAR
|
int |
YEAR 表示年份的字段编号为 |
int |
ZONE_OFFSET
|
Fields |
|
|---|---|
protected boolean |
areFieldsSet 如果 |
protected int[] |
fields 该日历的当前设置时间的日历字段值。 |
protected boolean[] |
isSet 指示日历的指定日历字段是否已设置的标志。 |
protected boolean |
isTimeSet 如果 |
protected long |
time 此日历的当前设置时间,以1970年1月1日格林威治标准时间0:00:00之后的毫秒数表示。 |
Protected constructors |
|
|---|---|
Calendar() 使用默认时区和区域设置构建日历。 |
|
Calendar(TimeZone zone, Locale aLocale) 使用指定的时区和区域设置构建日历。 |
|
公共方法(Public methods) |
|
|---|---|
abstract void |
add(int field, int amount) 根据日历的规则,将指定的时间量添加或减去给定的日历字段。 |
boolean |
after(Object when) 返回此 |
boolean |
before(Object when) 返回此 |
final void |
clear() 设置此 |
final void |
clear(int field) 设置此 |
Object |
clone() 创建并返回此对象的副本。 |
int |
compareTo(Calendar anotherCalendar) 比较由两个 |
boolean |
equals(Object obj) 将此 |
int |
get(int field) 返回给定日历字段的值。 |
int |
getActualMaximum(int field) 给定此 |
int |
getActualMinimum(int field) 给定此 |
static Locale[] |
getAvailableLocales() 返回 |
String |
getDisplayName(int field, int style, Locale locale) 返回给定的 |
Map<String, Integer> |
getDisplayNames(int field, int style, Locale locale) 返回 |
int |
getFirstDayOfWeek() 获取一周的第一天是什么; 例如, |
abstract int |
getGreatestMinimum(int field) 返回此 |
static Calendar |
getInstance() 使用默认时区和区域设置获取日历。 |
static Calendar |
getInstance(Locale aLocale) 使用默认时区和指定区域设置获取日历。 |
static Calendar |
getInstance(TimeZone zone, Locale aLocale) 获取具有指定时区和区域设置的日历。 |
static Calendar |
getInstance(TimeZone zone) 使用指定的时区和默认语言环境获取日历。 |
abstract int |
getLeastMaximum(int field) 返回此 |
abstract int |
getMaximum(int field) 返回此 |
int |
getMinimalDaysInFirstWeek() 获取一年中第一周所需的最短日期; 例如,如果第一周定义为包含一年中第一个月的第一天的一周,则此方法返回1。 |
abstract int |
getMinimum(int field) 返回此 |
final Date |
getTime() 返回代表这个 |
long |
getTimeInMillis() 以毫秒为单位返回此日历的时间值。 |
TimeZone |
getTimeZone() 获取时区。 |
int |
getWeekYear() 返回此 |
int |
getWeeksInWeekYear() 返回此 |
int |
hashCode() 返回此日历的哈希码。 |
boolean |
isLenient() 告诉日期/时间解释是否宽松。 |
final boolean |
isSet(int field) 确定给定的日历字段是否具有值集,包括通过方法调用 |
boolean |
isWeekDateSupported() 返回此 |
abstract void |
roll(int field, boolean up) 在给定时间字段上添加或减去(上/下)单个时间单位而不改变更大的字段。 |
void |
roll(int field, int amount) 将指定的(已签名)金额添加到指定的日历字段,而不更改较大的字段。 |
final void |
set(int year, int month, int date, int hourOfDay, int minute, int second) 设置字段中的值 |
final void |
set(int year, int month, int date, int hourOfDay, int minute) 设置日历字段的值 |
void |
set(int field, int value) 将给定的日历字段设置为给定值。 |
final void |
set(int year, int month, int date) 设置日历字段的值 |
void |
setFirstDayOfWeek(int value) 设置一周的第一天是什么; 例如, |
void |
setLenient(boolean lenient) 指定日期/时间解释是否宽松。 |
void |
setMinimalDaysInFirstWeek(int value) 设置一年中第一周所需的最短日期; 例如,如果第一周被定义为包含一年的第一个月的第一天的一周,则使用值1调用此方法。 |
final void |
setTime(Date date) 使用给定的 |
void |
setTimeInMillis(long millis) 根据给定的长值设置此日历的当前时间。 |
void |
setTimeZone(TimeZone value) 使用给定的时区值设置时区。 |
void |
setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) 使用给定的日期说明符设置此 |
String |
toString() 返回此日历的字符串表示形式。 |
Protected methods |
|
|---|---|
void |
complete() 填写日历栏中的任何未设置的字段。 |
abstract void |
computeFields() |
abstract void |
computeTime() |
final int |
internalGet(int field) 返回给定日历字段的值。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
java.lang.Comparable
|
|
int ALL_STYLES
getDisplayNames的样式说明 getDisplayNames指示所有样式的名称,例如“January”和“Jan”。
常量值:0(0x00000000)
int AM_PM
get和set字段编号,指示HOUR是在中午之前还是之后。 例如,在10点04分15秒50分, AM_PM是PM 。
常量值:9(0x00000009)
int DATE
指示月份的日期的get和set字段编号。 这是DAY_OF_MONTH的同义词。 本月的第一天有值1。
也可以看看:
常量值:5(0x00000005)
int DAY_OF_MONTH
指示月份的当天的get和set字段编号。 这是DATE的同义词。 本月的第一天有值1。
也可以看看:
常量值:5(0x00000005)
int DAY_OF_WEEK
表示星期几的get和set字段编号。 此字段值SUNDAY , MONDAY , TUESDAY , WEDNESDAY , THURSDAY , FRIDAY ,并SATURDAY 。
常量值:7(0x00000007)
int DAY_OF_WEEK_IN_MONTH
get和set字段编号,指示当前月份中星期几的序号。 与DAY_OF_WEEK字段一起,这个唯一地指定了一个月内的一天。 不像WEEK_OF_MONTH和WEEK_OF_YEAR ,该字段的值并不取决于getFirstDayOfWeek()或者getMinimalDaysInFirstWeek() 。 DAY_OF_MONTH 1到7总是对应于DAY_OF_WEEK_IN_MONTH 1 ; 8到14对应于DAY_OF_WEEK_IN_MONTH 2 ,依此类推。 DAY_OF_WEEK_IN_MONTH 0表示前一周的DAY_OF_WEEK_IN_MONTH 1 。 负值从月末开始计算,因此一个月的最后一个星期日被指定为DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1 。 由于负值倒数,因此通常会在月内以不同的方式排列,而不是正值。 例如,如果在一个月有31天, DAY_OF_WEEK_IN_MONTH -1将重叠DAY_OF_WEEK_IN_MONTH 5和的末端4 。
也可以看看:
常量值:8(0x00000008)
int DAY_OF_YEAR
get和set字段编号,指示当年的日期编号。 今年的第一天有价值1。
常数值:6(0x00000006)
int DST_OFFSET
指示以毫秒为单位的夏令时偏移的 get和 set字段编号。
如果 TimeZone实施子类支持历史夏令时时间表更改,则此字段将反映此 Calendar时区的正确夏令时偏移值。
常量值:16(0x00000010)
int ERA
指示时间的get和set字段编号,例如Julian日历中的AD或BC。 这是一个日历特定的值; 请参阅子类文档。
常量值:0(0x00000000)
int FIELD_COUNT
由get和set识别的不同字段的数量。 字段编号范围从0..FIELD_COUNT-1 。
常量值:17(0x00000011)
int HOUR
表示上午或下午的小时的get和set字段编号。 HOUR用于12小时制时钟(0 - 11)。 中午和午夜用0表示,不用12表示。例如,在10点04分15秒50分, HOUR是10。
也可以看看:
常量值:10(0x0000000a)
int HOUR_OF_DAY
指示当天的小时的get和set字段编号。 HOUR_OF_DAY用于24小时制。 例如,在10:04:15.250 PM, HOUR_OF_DAY是22。
也可以看看:
常量值:11(0x0000000b)
int LONG
样式说明符 getDisplayName和 getDisplayNames表示长名称,例如“January”。
也可以看看:
常量值:2(0x00000002)
int MILLISECOND
get和set字段编号表示秒内的毫秒数。 例如,在10:04:15.250 PM, MILLISECOND是250。
常量值:14(0x0000000e)
int MINUTE
指示一小时内分钟的get和set字段编号。 例如,在10:04:15.250 PM, MINUTE是4。
常量值:12(0x0000000c)
int MONTH
指示月份的字段编号为get和set 。 这是一个日历特定的值。 格里历和儒略历中的第一个月是JANUARY ,这是0; 最后一个取决于一年中的月数。
也可以看看:
常量值:2(0x00000002)
int SECOND
现场编号为get和set表示分钟内的第二个。 例如,在10:04:15.250 PM, SECOND是15。
常量值:13(0x0000000d)
int SHORT
样式说明符 getDisplayName和 getDisplayNames表示短名称,如“Jan”。
也可以看看:
常数值:1(0x00000001)
int UNDECIMBER
MONTH字段的值表示今年的第13个月。 虽然GregorianCalendar不使用此值,但月历可以。
常量值:12(0x0000000c)
int WEEK_OF_MONTH
get和set字段编号,指示当前月份内的星期编号。 如getFirstDayOfWeek()和getMinimalDaysInFirstWeek()所定义的,本月的第一周的值为1,子类在该月的第一周之前定义了WEEK_OF_MONTH的值。
常量值:4(0x00000004)
int WEEK_OF_YEAR
get和set字段编号,指示当前年份内的星期编号。 由getFirstDayOfWeek()和getMinimalDaysInFirstWeek()定义的一年中的第一周的值为1.子类在一年的第一周之前定义了WEEK_OF_YEAR的值。
常量值:3(0x00000003)
int ZONE_OFFSET
get和 set字段编号,指示以毫秒为单位的GMT的原始偏移量。
如果 TimeZone实现子类支持历史GMT偏移更改,则此字段反映此 Calendar时区的正确GMT偏移值。
常量值:15(0x0000000f)
boolean areFieldsSet
如果fields[]与当前设定的时间同步, fields[]真。 如果为false,则下一次获取字段值的尝试将强制重新计算当前值time的所有字段。
int[] fields
该日历的当前设置时间的日历字段值。 这是一个FIELD_COUNT整数的数组,索引值为ERA到DST_OFFSET 。
boolean[] isSet
指示日历的指定日历字段是否已设置的标志。 新对象没有设置字段。 第一次调用生成这些字段的方法后,它们在此之后都保持设置状态。 这是一组FIELD_COUNT布尔值,索引值为ERA到DST_OFFSET 。
Calendar (TimeZone zone, Locale aLocale)
使用指定的时区和区域设置构建日历。
| 参数(Parameters) | |
|---|---|
zone |
TimeZone: the time zone to use |
aLocale |
Locale: the locale for the week data |
void add (int field,
int amount)
根据日历的规则,将指定的时间量添加或减去给定的日历字段。 例如,要从日历的当前时间中减去5天,可以通过调用以下方法来实现它:
add(Calendar.DAY_OF_MONTH, -5) 。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field. |
amount |
int: the amount of date or time to be added to the field. |
也可以看看:
boolean after (Object when)
返回此Calendar是否表示由指定的Object表示的时间之后的时间。 这种方法相当于:
compareTo(when) > 0
if and only if
when is a
Calendar instance. Otherwise, the method returns
false.
| 参数(Parameters) | |
|---|---|
when |
Object: the Object to be compared |
| 返回(Returns) | |
|---|---|
boolean |
true if the time of this Calendar is after the time represented by when; false otherwise. |
也可以看看:
boolean before (Object when)
返回此Calendar是否表示由指定的Object表示的时间之前的时间。 这种方法相当于:
compareTo(when) < 0
if and only if
when is a
Calendar instance. Otherwise, the method returns
false.
| 参数(Parameters) | |
|---|---|
when |
Object: the Object to be compared |
| 返回(Returns) | |
|---|---|
boolean |
true if the time of this Calendar is before the time represented by when; false otherwise. |
也可以看看:
void clear ()
设置此Calendar未定义的所有日历字段值和时间值(来自Epoch的毫秒偏移量)。 这意味着isSet()将为所有日历字段返回false ,并且日期和时间计算会将这些字段视为从未设置过。 一个Calendar实现类可以使用其特定的默认字段值进行日期/时间计算。 例如,如果YEAR字段值未定义,则GregorianCalendar使用1970。
也可以看看:
void clear (int field)
设置此Calendar未定义的给定日历字段值和时间值(来自Epoch的毫秒偏移量)。 这意味着isSet(field)将返回false ,并且日期和时间计算会将该字段视为从未设置过。 Calendar实现类可以使用该字段的特定默认值进行日期和时间计算。
的HOUR_OF_DAY , HOUR和AM_PM字段独立地处理,并且the resolution rule for the time of day被应用。 清除其中一个字段不会重置此Calendar的小时数值。 使用set(Calendar.HOUR_OF_DAY, 0)重置小时值。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field to be cleared. |
也可以看看:
int compareTo (Calendar anotherCalendar)
比较两个 Calendar对象所代表的时间值(来自 Epoch的毫秒偏移量)。
| 参数(Parameters) | |
|---|---|
anotherCalendar |
Calendar: the Calendar to be compared. |
| 返回(Returns) | |
|---|---|
int |
the value 0 if the time represented by the argument is equal to the time represented by this Calendar; a value less than 0 if the time of this Calendar is before the time represented by the argument; and a value greater than 0 if the time of this Calendar is after the time represented by the argument. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the specified Calendar is null. |
IllegalArgumentException |
if the time value of the specified Calendar object can't be obtained due to any invalid calendar values. |
boolean equals (Object obj)
将此Calendar与指定的Object 。 结果是true当且仅当参数是相同日历系统的Calendar对象,该对象表示与此对象相同的Calendar参数下的相同时间值(与Epoch的毫秒偏移量)。
的Calendar参数是由表示的值isLenient , getFirstDayOfWeek , getMinimalDaysInFirstWeek和getTimeZone方法。 如果两个Calendar之间的参数有任何差异,则此方法返回false 。
使用 compareTo方法仅比较时间值。
| 参数(Parameters) | |
|---|---|
obj |
Object: the object to compare with. |
| 返回(Returns) | |
|---|---|
boolean |
true if this object is equal to obj; false otherwise. |
int get (int field)
返回给定日历字段的值。 在宽松模式下,所有日历字段都被标准化。 在非宽松模式下,所有日历字段都将进行验证,并且如果任何日历字段的值超出范围,此方法将引发异常。 规范化和验证由complete()方法处理,该过程依赖于日历系统。
| 参数(Parameters) | |
|---|---|
field |
int: the given calendar field. |
| 返回(Returns) | |
|---|---|
int |
the value for the given calendar field. |
| 抛出异常(Throws) | |
|---|---|
ArrayIndexOutOfBoundsException |
if the specified field is out of range (field < 0 || field >= FIELD_COUNT). |
也可以看看:
int getActualMaximum (int field)
给定此Calendar的时间值,返回指定日历字段可能具有的Calendar 。 例如, MONTH字段的实际最大值在某些年份为12,在其他年份为希伯来语日历系统的13。
此方法的默认实现使用迭代算法来确定日历字段的实际最大值。 如果可能的话,子类应该用更有效的实现覆盖它。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field |
| 返回(Returns) | |
|---|---|
int |
the maximum of the given calendar field for the time value of this Calendar |
int getActualMinimum (int field)
给定此 Calendar的时间值,返回指定的日历字段可能具有的 Calendar 。
此方法的默认实现使用迭代算法来确定日历字段的实际最小值。 如果可能的话,子类应该用更有效的实现覆盖它 - 在很多情况下,它们可以简单地返回getMinimum() 。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field |
| 返回(Returns) | |
|---|---|
int |
the minimum of the given calendar field for the time value of this Calendar |
Locale[] getAvailableLocales ()
返回getInstance方法可返回本地化实例的所有语言环境的数组。 返回的数组必须包含至少一个等于Locale.US的Locale实例。
| 返回(Returns) | |
|---|---|
Locale[] |
An array of locales for which localized Calendar instances are available. |
String getDisplayName (int field, int style, Locale locale)
返回给定style和locale日历field值的字符串表示locale 。 如果没有字符串表示法适用,则返回null 。 如果字符串表示法适用于给定的日历field则此方法调用get(field)以获取日历field值。
例如,如果这个Calendar是GregorianCalendar并且它的日期是2005-01-01,那么MONTH字段的字符串表示在英文语言环境中的长格式中将是“1月”,或者在短格式中是“1月”。 但是, DAY_OF_MONTH字段没有字符串表示形式,并且此方法将返回null 。
默认实现支持 DateFormatSymbols在给定 locale具有名称的日历字段。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field for which the string representation is returned |
style |
int: the style applied to the string representation; one of SHORT or LONG. |
locale |
Locale: the locale for the string representation |
| 返回(Returns) | |
|---|---|
String |
the string representation of the given field in the given style, or null if no string representation is applicable. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if field or style is invalid, or if this Calendar is non-lenient and any of the calendar fields have invalid values |
NullPointerException |
if locale is null |
Map<String, Integer> getDisplayNames (int field, int style, Locale locale)
返回Map其中包含给定的style和locale中的日历field所有名称及其相应的字段值。 例如,如果此Calendar是GregorianCalendar ,则返回的地图将包含英语语言环境中short样式的“Jan”至JANUARY ,“Feb”至FEBRUARY等等。
其他日历字段的值可以被考虑以确定一组显示名称。 例如,如果此Calendar是一个月球日历系统,并且YEAR字段给出的年份值具有闰月,则此方法将返回包含闰月名称的月份名称,并将月份名称映射到特定年份的值。
默认实现支持DateFormatSymbols包含的显示名称。 例如,如果field是MONTH且style是ALL_STYLES ,则此方法返回Map其中包含由getShortMonths()和getMonths()返回的所有字符串。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field for which the display names are returned |
style |
int: the style applied to the display names; one of SHORT, LONG, or ALL_STYLES. |
locale |
Locale: the locale for the display names |
| 返回(Returns) | |
|---|---|
Map<String, Integer> |
a Map containing all display names in style and locale and their field values, or null if no display names are defined for field |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if field or style is invalid, or if this Calendar is non-lenient and any of the calendar fields have invalid values |
NullPointerException |
if locale is null |
int getFirstDayOfWeek ()
获取一周的第一天是什么; 例如, SUNDAY在美国, MONDAY在法国。
| 返回(Returns) | |
|---|---|
int |
the first day of the week. |
int getGreatestMinimum (int field)
返回此Calendar实例的给定日历字段的最大最小值。 最高最小值被定义为getActualMinimum(int)针对任何可能的时间值返回的最大值。 最大最小值取决于实例的日历系统特定参数。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field. |
| 返回(Returns) | |
|---|---|
int |
the highest minimum value for the given calendar field. |
Calendar getInstance ()
使用默认时区和区域设置获取日历。 返回的Calendar基于默认时区中的当前时间和默认语言环境。
| 返回(Returns) | |
|---|---|
Calendar |
a Calendar. |
Calendar getInstance (Locale aLocale)
使用默认时区和指定区域设置获取日历。 返回的Calendar基于给定语言环境的默认时区中的当前时间。
| 参数(Parameters) | |
|---|---|
aLocale |
Locale: the locale for the week data |
| 返回(Returns) | |
|---|---|
Calendar |
a Calendar. |
Calendar getInstance (TimeZone zone, Locale aLocale)
获取具有指定时区和区域设置的日历。 返回的Calendar基于给定语言环境给定时区中的当前时间。
| 参数(Parameters) | |
|---|---|
zone |
TimeZone: the time zone to use |
aLocale |
Locale: the locale for the week data |
| 返回(Returns) | |
|---|---|
Calendar |
a Calendar. |
Calendar getInstance (TimeZone zone)
使用指定的时区和默认语言环境获取日历。 返回的Calendar基于给定时区中的当前时间和默认语言环境。
| 参数(Parameters) | |
|---|---|
zone |
TimeZone: the time zone to use |
| 返回(Returns) | |
|---|---|
Calendar |
a Calendar. |
int getLeastMaximum (int field)
返回此Calendar实例的给定日历字段的最小最大值。 最小的最大值被定义为getActualMaximum(int)针对任何可能的时间值返回的getActualMaximum(int) 。 最小值取决于实例的日历系统特定参数。 例如, Calendar为公历系统返回的28 DAY_OF_MONTH领域,由于28日是这个日历,二月共同的一年中最短的月份的最后一天。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field. |
| 返回(Returns) | |
|---|---|
int |
the lowest maximum value for the given calendar field. |
int getMaximum (int field)
返回此Calendar实例的给定日历字段的Calendar 。 最大值被定义为get方法针对任何可能的时间值返回的最大值。 最大值取决于实例的日历系统特定参数。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field. |
| 返回(Returns) | |
|---|---|
int |
the maximum value for the given calendar field. |
int getMinimalDaysInFirstWeek ()
获取一年中第一周所需的最短日期; 例如,如果第一周定义为包含一年中第一个月的第一天的那一周,则此方法返回1.如果所需的最短天数必须为整周,则此方法返回7。
| 返回(Returns) | |
|---|---|
int |
the minimal days required in the first week of the year. |
int getMinimum (int field)
返回此Calendar实例的给定日历字段的Calendar 。 最小值被定义为get方法为任何可能的时间值返回的最小值。 最小值取决于实例的日历系统特定参数。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field. |
| 返回(Returns) | |
|---|---|
int |
the minimum value for the given calendar field. |
Date getTime ()
返回代表此 Calendar的时间值(从 Epoch “毫秒偏移”)的 Date对象。
| 返回(Returns) | |
|---|---|
Date |
a Date representing the time value. |
也可以看看:
long getTimeInMillis ()
以毫秒为单位返回此日历的时间值。
| 返回(Returns) | |
|---|---|
long |
the current time as UTC milliseconds from the epoch. |
也可以看看:
TimeZone getTimeZone ()
获取时区。
| 返回(Returns) | |
|---|---|
TimeZone |
the time zone object associated with this calendar. |
int getWeekYear ()
返回由此Calendar表示的Calendar 。 周年与周周期同步。 first day of the first week是一周中的第一天。
此方法的默认实现将引发 UnsupportedOperationException 。
| 返回(Returns) | |
|---|---|
int |
the week year of this Calendar |
| 抛出异常(Throws) | |
|---|---|
UnsupportedOperationException |
if any week year numbering isn't supported in this Calendar. |
int getWeeksInWeekYear ()
返回此 Calendar表示的一周中的周 Calendar 。
此方法的默认实现会引发 UnsupportedOperationException 。
| 返回(Returns) | |
|---|---|
int |
the number of weeks in the week year. |
| 抛出异常(Throws) | |
|---|---|
UnsupportedOperationException |
if any week year numbering isn't supported in this Calendar. |
int hashCode ()
返回此日历的哈希码。
| 返回(Returns) | |
|---|---|
int |
a hash code value for this object. |
boolean isLenient ()
告诉日期/时间解释是否宽松。
| 返回(Returns) | |
|---|---|
boolean |
true if the interpretation mode of this calendar is lenient; false otherwise. |
也可以看看:
boolean isSet (int field)
确定给定日历字段是否具有值集,包括通过 get方法调用触发的内部字段计算设置值的情况。
| 参数(Parameters) | |
|---|---|
field |
int
|
| 返回(Returns) | |
|---|---|
boolean |
true if the given calendar field has a value set; false otherwise. |
boolean isWeekDateSupported ()
返回此 Calendar是否支持星期日期。
此方法的默认实现返回 false 。
| 返回(Returns) | |
|---|---|
boolean |
true if this Calendar supports week dates; false otherwise. |
void roll (int field,
boolean up)
在给定时间字段上添加或减去(上/下)单个时间单位而不改变更大的字段。 例如,要将当前日期向上滚动一天,可以通过调用以下命令实现它:
滚动(Calendar.DATE,true)。 在年份或Calendar.YEAR字段上滚动时,它将滚动年份值,范围介于1和通过调用getMaximum(Calendar.YEAR)返回的值之间。 在月份或Calendar.MONTH字段上滚动时,日期等其他字段可能会发生冲突,并且需要更改。 例如,在96年1月31日的日期上滚动的月份将在02/29/96中产生。 在小时分钟或Calendar.HOUR_OF_DAY字段中滚动时,它将滚动小数值,范围介于0和23之间,这是从零开始的。
| 参数(Parameters) | |
|---|---|
field |
int: the time field. |
up |
boolean: indicates if the value of the specified time field is to be rolled up or rolled down. Use true if rolling up, false otherwise. |
也可以看看:
void roll (int field,
int amount)
将指定的(已签名)金额添加到指定的日历字段,而不更改较大的字段。 负数意味着下降。
注意: Calendar上的此默认实现只是重复调用滚动一个单位的版本roll() 。 这可能并不总是正确的。 例如,如果DAY_OF_MONTH字段为31,则滚动到2月将使其设置为28.该函数的GregorianCalendar版本处理此问题。 其他的子类也应该提供覆盖这个功能的功能,做正确的事情。
| 参数(Parameters) | |
|---|---|
field |
int: the calendar field. |
amount |
int: the signed amount to add to the calendar field. |
void set (int year,
int month,
int date,
int hourOfDay,
int minute,
int second)
设置字段中的值YEAR , MONTH , DAY_OF_MONTH , HOUR , MINUTE ,并SECOND 。 其他字段的先前值保留。 如果不需要, clear()致电clear() 。
| 参数(Parameters) | |
|---|---|
year |
int: the value used to set the YEAR calendar field. |
month |
int: the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January. |
date |
int: the value used to set the DAY_OF_MONTH calendar field. |
hourOfDay |
int: the value used to set the HOUR_OF_DAY calendar field. |
minute |
int: the value used to set the MINUTE calendar field. |
second |
int: the value used to set the SECOND calendar field. |
void set (int year,
int month,
int date,
int hourOfDay,
int minute)
设置日历字段的值YEAR , MONTH , DAY_OF_MONTH , HOUR_OF_DAY ,并MINUTE 。 其他字段的先前值保留。 如果不需要, clear()致电clear() 。
| 参数(Parameters) | |
|---|---|
year |
int: the value used to set the YEAR calendar field. |
month |
int: the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January. |
date |
int: the value used to set the DAY_OF_MONTH calendar field. |
hourOfDay |
int: the value used to set the HOUR_OF_DAY calendar field. |
minute |
int: the value used to set the MINUTE calendar field. |
void set (int field,
int value)
将给定的日历字段设置为给定值。 无论宽松模式如何,此方法都不会解释该值。
| 参数(Parameters) | |
|---|---|
field |
int: the given calendar field. |
value |
int: the value to be set for the given calendar field. |
| 抛出异常(Throws) | |
|---|---|
ArrayIndexOutOfBoundsException |
if the specified field is out of range (field < 0 || field >= FIELD_COUNT). in non-lenient mode. |
void set (int year,
int month,
int date)
设置日历字段的值YEAR , MONTH ,并DAY_OF_MONTH 。 其他日历字段的先前值保留。 如果不需要, clear()拨打clear() 。
| 参数(Parameters) | |
|---|---|
year |
int: the value used to set the YEAR calendar field. |
month |
int: the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January. |
date |
int: the value used to set the DAY_OF_MONTH calendar field. |
void setFirstDayOfWeek (int value)
设置一周的第一天是什么; 例如, SUNDAY在美国, MONDAY在法国。
| 参数(Parameters) | |
|---|---|
value |
int: the given first day of the week. |
void setLenient (boolean lenient)
指定日期/时间解释是否宽松。 在宽松的解释下,1996年2月942号的日期等同于1996年2月1日后的第941天。由于严格(非宽松)的解释,这种日期会引发异常。 默认值是宽松的。
| 参数(Parameters) | |
|---|---|
lenient |
boolean: true if the lenient mode is to be turned on; false if it is to be turned off. |
也可以看看:
void setMinimalDaysInFirstWeek (int value)
设置一年中第一周所需的最短日期; 例如,如果第一周被定义为包含一年的第一个月的第一天的一周,则使用值1调用此方法。如果它必须是整周,则使用值7。
| 参数(Parameters) | |
|---|---|
value |
int: the given minimal days required in the first week of the year. |
也可以看看:
void setTime (Date date)
使用给定的 Date设置此日历的时间。
注意:调用 setTime()与 Date(Long.MAX_VALUE)或 Date(Long.MIN_VALUE)可能会从 get()产生不正确的字段值。
| 参数(Parameters) | |
|---|---|
date |
Date: the given Date. |
也可以看看:
void setTimeInMillis (long millis)
根据给定的长值设置此日历的当前时间。
| 参数(Parameters) | |
|---|---|
millis |
long: the new time in UTC milliseconds from the epoch. |
也可以看看:
void setTimeZone (TimeZone value)
使用给定的时区值设置时区。
| 参数(Parameters) | |
|---|---|
value |
TimeZone: the given time zone. |
void setWeekDate (int weekYear,
int weekOfYear,
int dayOfWeek)
使用给定的日期说明符设置此 Calendar的日期 - 星期几,星期几和星期几。
与 set方法不同,所有日历字段和 time值都是在返回时计算的。
如果 weekOfYear超出有效周的年范围在 weekYear ,该 weekYear个 weekOfYear值在宽松模式调整,或 IllegalArgumentException在非宽松的模式抛出。
此方法的默认实现将引发 UnsupportedOperationException 。
| 参数(Parameters) | |
|---|---|
weekYear |
int: the week year |
weekOfYear |
int: the week number based on weekYear |
dayOfWeek |
int: the day of week value: one of the constants for the DAY_OF_WEEK field: SUNDAY, ..., SATURDAY. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if any of the given date specifiers is invalid or any of the calendar fields are inconsistent with the given date specifiers in non-lenient mode |
UnsupportedOperationException |
if any week year numbering isn't supported in this Calendar. |
String toString ()
返回此日历的字符串表示形式。 此方法仅用于调试目的,并且返回的字符串格式可能因实现而异。 返回的字符串可能为空,但可能不是null 。
| 返回(Returns) | |
|---|---|
String |
a string representation of this calendar. |
void complete ()
填写日历栏中的任何未设置的字段。 首先,如果时间值(来自Epoch的毫秒偏移量)未从日历字段值计算,则调用computeTime()方法。 然后,调用computeFields()方法来计算所有日历字段值。
void computeFields ()
当前毫秒时间值转换为time的日历字段值fields[] 。 这允许您使用为日历设置的新时间同步日历字段值。 时间不首先重新计算; 重新计算时间,然后在字段中调用complete()方法。
也可以看看:
int internalGet (int field)
返回给定日历字段的值。 此方法不涉及字段值的标准化或验证。
| 参数(Parameters) | |
|---|---|
field |
int: the given calendar field. |
| 返回(Returns) | |
|---|---|
int |
the value for the given calendar field. |
也可以看看: