java的Calendar类源码与日期工具类
Calendar类定义
public abstract class Calendar implements Serializable, Cloneable, Comparable {
public final static int ERA = 0;
/**
* Field number for get
and set
indicating the
* year. This is a calendar-specific value; see subclass documentation.
*/
public final static int YEAR = 1;
/**
* Field number for get
and set
indicating the
* month. This is a calendar-specific value. The first month of
* the year in the Gregorian and Julian calendars is
* JANUARY
which is 0; the last depends on the number
* of months in a year.
*
* @see #JANUARY
* @see #FEBRUARY
* @see #MARCH
* @see #APRIL
* @see #MAY
* @see #JUNE
* @see #JULY
* @see #AUGUST
* @see #SEPTEMBER
* @see #OCTOBER
* @see #NOVEMBER
* @see #DECEMBER
* @see #UNDECIMBER
*/
public final static int MONTH = 2;
/**
* Field number for get
and set
indicating the
* week number within the current year. The first week of the year, as
* defined by getFirstDayOfWeek()
and
* getMinimalDaysInFirstWeek()
, has value 1. Subclasses define
* the value of WEEK_OF_YEAR
for days before the first week of
* the year.
*
* @see #getFirstDayOfWeek
* @see #getMinimalDaysInFirstWeek
*/
public final static int WEEK_OF_YEAR = 3;
/**
* Field number for get
and set
indicating the
* week number within the current month. The first week of the month, as
* defined by getFirstDayOfWeek()
and
* getMinimalDaysInFirstWeek()
, has value 1. Subclasses define
* the value of WEEK_OF_MONTH
for days before the first week of
* the month.
*
* @see #getFirstDayOfWeek
* @see #getMinimalDaysInFirstWeek
*/
public final static int WEEK_OF_MONTH = 4;
/**
* Field number for get
and set
indicating the
* day of the month. This is a synonym for DAY_OF_MONTH
.
* The first day of the month has value 1.
*
* @see #DAY_OF_MONTH
*/
public final static int DATE = 5;
/**
* Field number for get
and set
indicating the
* day of the month. This is a synonym for DATE
.
* The first day of the month has value 1.
*
* @see #DATE
*/
public final static int DAY_OF_MONTH = 5;
/**
* Field number for get
and set
indicating the day
* number within the current year. The first day of the year has value 1.
*/
public final static int DAY_OF_YEAR = 6;
/**
* Field number for get
and set
indicating the day
* of the week. This field takes values SUNDAY
,
* MONDAY
, TUESDAY
, WEDNESDAY
,
* THURSDAY
, FRIDAY
, and SATURDAY
.
*
* @see #SUNDAY
* @see #MONDAY
* @see #TUESDAY
* @see #WEDNESDAY
* @see #THURSDAY
* @see #FRIDAY
* @see #SATURDAY
*/
public final static int DAY_OF_WEEK = 7;
/**
* Field number for get
and set
indicating the
* ordinal number of the day of the week within the current month. Together
* with the DAY_OF_WEEK
field, this uniquely specifies a day
* within a month. Unlike WEEK_OF_MONTH
and
* WEEK_OF_YEAR
, this field's value does not depend on
* getFirstDayOfWeek()
or
* getMinimalDaysInFirstWeek()
. DAY_OF_MONTH 1
* through 7
always correspond to DAY_OF_WEEK_IN_MONTH
* 1
; 8
through 14
correspond to
* DAY_OF_WEEK_IN_MONTH 2
, and so on.
* DAY_OF_WEEK_IN_MONTH 0
indicates the week before
* DAY_OF_WEEK_IN_MONTH 1
. Negative values count back from the
* end of the month, so the last Sunday of a month is specified as
* DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1
. Because
* negative values count backward they will usually be aligned differently
* within the month than positive values. For example, if a month has 31
* days, DAY_OF_WEEK_IN_MONTH -1
will overlap
* DAY_OF_WEEK_IN_MONTH 5
and the end of 4
.
*
* @see #DAY_OF_WEEK
* @see #WEEK_OF_MONTH
*/
public final static int DAY_OF_WEEK_IN_MONTH = 8;
/**
* Field number for get
and set
indicating
* whether the HOUR
is before or after noon.
* E.g., at 10:04:15.250 PM the AM_PM
is PM
.
*
* @see #AM
* @see #PM
* @see #HOUR
*/
public final static int AM_PM = 9;
/**
* Field number for get
and set
indicating the
* hour of the morning or afternoon. HOUR
is used for the
* 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12.
* E.g., at 10:04:15.250 PM the HOUR
is 10.
*
* @see #AM_PM
* @see #HOUR_OF_DAY
*/
public final static int HOUR = 10;
/**
* Field number for get
and set
indicating the
* hour of the day. HOUR_OF_DAY
is used for the 24-hour clock.
* E.g., at 10:04:15.250 PM the HOUR_OF_DAY
is 22.
*
* @see #HOUR
*/
public final static int HOUR_OF_DAY = 11;
/**
* Field number for get
and set
indicating the
* minute within the hour.
* E.g., at 10:04:15.250 PM the MINUTE
is 4.
*/
public final static int MINUTE = 12;
/**
* Field number for get
and set
indicating the
* second within the minute.
* E.g., at 10:04:15.250 PM the SECOND
is 15.
*/
public final static int SECOND = 13;
/**
* Field number for get
and set
indicating the
* millisecond within the second.
* E.g., at 10:04:15.250 PM the MILLISECOND
is 250.
*/
public final static int MILLISECOND = 14;
@SuppressWarnings("ProtectedField")
protected int fields[];
@SuppressWarnings("ProtectedField")
protected boolean isSet[];
transient private int stamp[];
@SuppressWarnings("ProtectedField")
protected long time;
@SuppressWarnings("ProtectedField")
protected boolean isTimeSet;
@SuppressWarnings("ProtectedField")
protected boolean areFieldsSet;
transient boolean areAllFieldsSet;
private boolean lenient = true;
private TimeZone zone;
transient private boolean sharedZone = false;
private int firstDayOfWeek;
private int minimalDaysInFirstWeek;
private static final ConcurrentMap cachedLocaleData
= new ConcurrentHashMap<>(3);
protected Calendar()
{
this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
sharedZone = true;
}
protected Calendar(TimeZone zone, Locale aLocale)
{
fields = new int[FIELD_COUNT];
isSet = new boolean[FIELD_COUNT];
stamp = new int[FIELD_COUNT];
this.zone = zone;
setWeekCountData(aLocale);
}
public static Calendar getInstance()
{
return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
}
public static Calendar getInstance(TimeZone zone)
{
return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT));
}
public static Calendar getInstance(Locale aLocale)
{
return createCalendar(TimeZone.getDefault(), aLocale);
}
public static Calendar getInstance(TimeZone zone,
Locale aLocale)
{
return createCalendar(zone, aLocale);
}
private static Calendar createCalendar(TimeZone zone,
Locale aLocale)
{
CalendarProvider provider =
LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
.getCalendarProvider();
if (provider != null) {
try {
return provider.getInstance(zone, aLocale);
} catch (IllegalArgumentException iae) {
// fall back to the default instantiation
}
}
Calendar cal = null;
if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype != null) {
switch (caltype) {
case "buddhist":
cal = new BuddhistCalendar(zone, aLocale);
break;
case "japanese":
cal = new JapaneseImperialCalendar(zone, aLocale);
break;
case "gregory":
cal = new GregorianCalendar(zone, aLocale);
break;
}
}
}
if (cal == null) {
// If no known calendar type is explicitly specified,
// perform the traditional way to create a Calendar:
// create a BuddhistCalendar for th_TH locale,
// a JapaneseImperialCalendar for ja_JP_JP locale, or
// a GregorianCalendar for any other locales.
// NOTE: The language, country and variant strings are interned.
if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
cal = new BuddhistCalendar(zone, aLocale);
} else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
&& aLocale.getCountry() == "JP") {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
}
return cal;
}
public static synchronized Locale[] getAvailableLocales()
{
return DateFormat.getAvailableLocales();
}
protected abstract void computeTime();
protected abstract void computeFields();
public final Date getTime() {
return new Date(getTimeInMillis());
}
public final void setTime(Date date) {
setTimeInMillis(date.getTime());
}
public long getTimeInMillis() {
if (!isTimeSet) {
updateTime();
}
return time;
}
public void setTimeInMillis(long millis) {
// If we don't need to recalculate the calendar field values,
// do nothing.
if (time == millis && isTimeSet && areFieldsSet && areAllFieldsSet
&& (zone instanceof ZoneInfo) && !((ZoneInfo)zone).isDirty()) {
return;
}
time = millis;
isTimeSet = true;
areFieldsSet = false;
computeFields();
areAllFieldsSet = areFieldsSet = true;
}
public int get(int field)
{
complete();
return internalGet(field);
}
protected final int internalGet(int field)
{
return fields[field];
}
final void internalSet(int field, int value)
{
fields[field] = value;
}
public void set(int field, int value)
{
// If the fields are partially normalized, calculate all the
// fields before changing any fields.
if (areFieldsSet && !areAllFieldsSet) {
computeFields();
}
internalSet(field, value);
isTimeSet = false;
areFieldsSet = false;
isSet[field] = true;
stamp[field] = nextStamp++;
if (nextStamp == Integer.MAX_VALUE) {
adjustStamp();
}
}
public final void set(int year, int month, int date)
{
set(YEAR, year);
set(MONTH, month);
set(DATE, date);
}
public final void set(int year, int month, int date, int hourOfDay, int minute)
{
set(YEAR, year);
set(MONTH, month);
set(DATE, date);
set(HOUR_OF_DAY, hourOfDay);
set(MINUTE, minute);
}
public final void set(int year, int month, int date, int hourOfDay, int minute,
int second)
{
set(YEAR, year);
set(MONTH, month);
set(DATE, date);
set(HOUR_OF_DAY, hourOfDay);
set(MINUTE, minute);
set(SECOND, second);
}
public final void clear()
{
for (int i = 0; i < fields.length; ) {
stamp[i] = fields[i] = 0; // UNSET == 0
isSet[i++] = false;
}
areAllFieldsSet = areFieldsSet = false;
isTimeSet = false;
}
public final void clear(int field)
{
fields[field] = 0;
stamp[field] = UNSET;
isSet[field] = false;
areAllFieldsSet = areFieldsSet = false;
isTimeSet = false;
}
public final boolean isSet(int field)
{
return stamp[field] != UNSET;
}
@Override
public String toString() {
// NOTE: BuddhistCalendar.toString() interprets the string
// produced by this method so that the Gregorian year number
// is substituted by its B.E. year value. It relies on
// "...,YEAR=,..." or "...,YEAR=?,...".
StringBuilder buffer = new StringBuilder(800);
buffer.append(getClass().getName()).append('[');
appendValue(buffer, "time", isTimeSet, time);
buffer.append(",areFieldsSet=").append(areFieldsSet);
buffer.append(",areAllFieldsSet=").append(areAllFieldsSet);
buffer.append(",lenient=").append(lenient);
buffer.append(",zone=").append(zone);
appendValue(buffer, ",firstDayOfWeek", true, (long) firstDayOfWeek);
appendValue(buffer, ",minimalDaysInFirstWeek", true, (long) minimalDaysInFirstWeek);
for (int i = 0; i < FIELD_COUNT; ++i) {
buffer.append(',');
appendValue(buffer, FIELD_NAME[i], isSet(i), (long) fields[i]);
}
buffer.append(']');
return buffer.toString();
}
// =======================privates===============================
private static void appendValue(StringBuilder sb, String item, boolean valid, long value) {
sb.append(item).append('=');
if (valid) {
sb.append(value);
} else {
sb.append('?');
}
}
private void setWeekCountData(Locale desiredLocale)
{
/* try to get the Locale data from the cache */
int[] data = cachedLocaleData.get(desiredLocale);
if (data == null) { /* cache miss */
data = new int[2];
data[0] = CalendarDataUtility.retrieveFirstDayOfWeek(desiredLocale);
data[1] = CalendarDataUtility.retrieveMinimalDaysInFirstWeek(desiredLocale);
cachedLocaleData.putIfAbsent(desiredLocale, data);
}
firstDayOfWeek = data[0];
minimalDaysInFirstWeek = data[1];
}
private void updateTime() {
computeTime();
// The areFieldsSet and areAllFieldsSet values are no longer
// controlled here (as of 1.5).
isTimeSet = true;
}
private int compareTo(long t) {
long thisTime = getMillisOf(this);
return (thisTime > t) ? 1 : (thisTime == t) ? 0 : -1;
}
private static long getMillisOf(Calendar calendar) {
if (calendar.isTimeSet) {
return calendar.time;
}
Calendar cal = (Calendar) calendar.clone();
cal.setLenient(true);
return cal.getTimeInMillis();
}
private void adjustStamp() {
int max = MINIMUM_USER_STAMP;
int newStamp = MINIMUM_USER_STAMP;
for (;;) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < stamp.length; i++) {
int v = stamp[i];
if (v >= newStamp && min > v) {
min = v;
}
if (max < v) {
max = v;
}
}
if (max != min && min == Integer.MAX_VALUE) {
break;
}
for (int i = 0; i < stamp.length; i++) {
if (stamp[i] == min) {
stamp[i] = newStamp;
}
}
newStamp++;
if (min == max) {
break;
}
}
nextStamp = newStamp;
}
private void invalidateWeekFields()
{
if (stamp[WEEK_OF_MONTH] != COMPUTED &&
stamp[WEEK_OF_YEAR] != COMPUTED) {
return;
}
// We have to check the new values of these fields after changing
// firstDayOfWeek and/or minimalDaysInFirstWeek. If the field values
// have been changed, then set the new values. (4822110)
Calendar cal = (Calendar) clone();
cal.setLenient(true);
cal.clear(WEEK_OF_MONTH);
cal.clear(WEEK_OF_YEAR);
if (stamp[WEEK_OF_MONTH] == COMPUTED) {
int weekOfMonth = cal.get(WEEK_OF_MONTH);
if (fields[WEEK_OF_MONTH] != weekOfMonth) {
fields[WEEK_OF_MONTH] = weekOfMonth;
}
}
if (stamp[WEEK_OF_YEAR] == COMPUTED) {
int weekOfYear = cal.get(WEEK_OF_YEAR);
if (fields[WEEK_OF_YEAR] != weekOfYear) {
fields[WEEK_OF_YEAR] = weekOfYear;
}
}
}
private synchronized void writeObject(ObjectOutputStream stream)
throws IOException
{
// Try to compute the time correctly, for the future (stream
// version 2) in which we don't write out fields[] or isSet[].
if (!isTimeSet) {
try {
updateTime();
}
catch (IllegalArgumentException e) {}
}
// If this Calendar has a ZoneInfo, save it and set a
// SimpleTimeZone equivalent (as a single DST schedule) for
// backward compatibility.
TimeZone savedZone = null;
if (zone instanceof ZoneInfo) {
SimpleTimeZone stz = ((ZoneInfo)zone).getLastRuleInstance();
if (stz == null) {
stz = new SimpleTimeZone(zone.getRawOffset(), zone.getID());
}
savedZone = zone;
zone = stz;
}
// Write out the 1.1 FCS object.
stream.defaultWriteObject();
// Write out the ZoneInfo object
// 4802409: we write out even if it is null, a temporary workaround
// the real fix for bug 4844924 in corba-iiop
stream.writeObject(savedZone);
if (savedZone != null) {
zone = savedZone;
}
}
private static class CalendarAccessControlContext {
private static final AccessControlContext INSTANCE;
static {
RuntimePermission perm = new RuntimePermission("accessClassInPackage.sun.util.calendar");
PermissionCollection perms = perm.newPermissionCollection();
perms.add(perm);
INSTANCE = new AccessControlContext(new ProtectionDomain[] {
new ProtectionDomain(null, perms)
});
}
private CalendarAccessControlContext() {
}
}
/**
* Reconstitutes this object from a stream (i.e., deserialize it).
*/
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException
{
final ObjectInputStream input = stream;
input.defaultReadObject();
stamp = new int[FIELD_COUNT];
// Starting with version 2 (not implemented yet), we expect that
// fields[], isSet[], isTimeSet, and areFieldsSet may not be
// streamed out anymore. We expect 'time' to be correct.
if (serialVersionOnStream >= 2)
{
isTimeSet = true;
if (fields == null) {
fields = new int[FIELD_COUNT];
}
if (isSet == null) {
isSet = new boolean[FIELD_COUNT];
}
}
else if (serialVersionOnStream >= 0)
{
for (int i=0; i() {
@Override
public ZoneInfo run() throws Exception {
return (ZoneInfo) input.readObject();
}
},
CalendarAccessControlContext.INSTANCE);
} catch (PrivilegedActionException pae) {
Exception e = pae.getException();
if (!(e instanceof OptionalDataException)) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else if (e instanceof IOException) {
throw (IOException) e;
} else if (e instanceof ClassNotFoundException) {
throw (ClassNotFoundException) e;
}
throw new RuntimeException(e);
}
}
if (zi != null) {
zone = zi;
}
// If the deserialized object has a SimpleTimeZone, try to
// replace it with a ZoneInfo equivalent (as of 1.4) in order
// to be compatible with the SimpleTimeZone-based
// implementation as much as possible.
if (zone instanceof SimpleTimeZone) {
String id = zone.getID();
TimeZone tz = TimeZone.getTimeZone(id);
if (tz != null && tz.hasSameRules(zone) && tz.getID().equals(id)) {
zone = tz;
}
}
}
public final Instant toInstant() {
return Instant.ofEpochMilli(getTimeInMillis());
}
}
Java官方推荐使用Calendar来替换Date的使用。
Calendar与Date之间可以自由的进行转换,转换的纽带是time。
使用Calendar的getTime()方法可以得到一个Date类型的对象,这个对象底层是使用Date的第二个带Long型参数的构造器创建的,这个Long型参数是Calendar中的time字段中保存的值,这个time字段的值是在具体的实现类中定义赋值的。
比如GregorianCalendar中的实现computeTime(),这个方法的目的就是将field值转换为time值,这个涉及到Calendar中的两种模式,之后会有介绍。
而通过Calendar的setTime(Date date)方法可以将一个Date对象转换为一个Calendar对象,这个方法以一个Date对象为参数,底层调用的setTimeInMillis(long millis)方法,将date.getTime()的值作为参数,再底层会将这个Long型参数值赋值给time字段,这时会重计算field值。
Calendar与Date的转换:
public class CalendarTest {
public static void main(String[] args) {
//Calendar--->Date
Calendar c = Calendar.getInstance();
Date d = c.getTime();
//Date--->Calendar
Date d1 = new Date();
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
System.out.println(d);
System.out.println(c1.get(Calendar.YEAR)+"年"+(c1.get(Calendar.MONTH)+1)+"月"+c1.get(Calendar.DATE)+"日");
}
}
Calendar中有两种描述时间内容的域,一种就是time,它用来保存Calendar对象所代表的时间点据1970年1月1日 00:00:00的毫秒数,另一种就是field,它是一个数组,它表示的并不是一个内容,而是Calendar内部定义的最多静态常量字段。
而这一般情况下是同步的,即表述的是同一时间点,但也有可能会出现不同步的情况:
更确切的说,当我们通过Calendar.getInstance()方法获取一个全新的Calendar对象时,它所代表的时间点是通过time来设置的,而这个time的值是通过System.currentTimeMillis()得到的,通过time定义Calendar,isTimeSet为true,表示time值是最新的(真的),areFieldsSet为false,表示field字段的值都是旧的(假的),因为当我们重新设置了time值之后,Calendar所代表的时间点就发生了变化(这里是首次,相当于从无到有,也算是变化,之后当我们为Calendar的time重新设置一个新值时,Calendar的时间点就会再次发生变化,它会指向最新的time值所代表的时间点),而这时field中还表示的是原来的时间点内容,然后会调用computeFields()方法进行所有字段值的重计算,确保field中的值与time同步,并同时将areFieldsSet和areAllFieldsSet设置为true,表示所有的field代表的时间值也是最新的了(真)。其实我们每次更改time值都会自动触发重计算,来确保两个域所描述的时间点一致(即同步),这也就是上面b所述的内容。
但是如果我们通过set(int field, int value)单独对field中的某行一字段进行更改时,首先会触发一个验证,areFieldsSet为真而areAllFieldsSet为false时,表示只有一部分field是最新的情况,即存在部分field属于旧的情况,针对这种情况会触发field的重新计算;之后会将isTimeSet设置为false,areFieldsSet设置为false,将isSet[field]设置为true(将当前field设置为真),这种情况下,当我们使用getTime()获取time值所代表的时间点时,由于isTimeSet为false,会触发time的重计算,这个计算依据是根据field的值进行的,之后将isTimeSet设置为true,同样我们在通过get(int field)获取某个field值时也会先验证isTimeSet是否为true,如果为false,同样会触发time的重计算,然后验证areFieldsSet为false,则触发其余field的重计算。
time的重计算是依据field的,确切的说是依据部分field的,而有一部分field也是在field的基础上再计算的,所以可以说有一部分field是固定的,是和time息息相关的。
以上种种所述全部是Calendar内部的实现规则,对外而言,我们只需要简单的调用即可,所有这些都被隐藏在内部,从而保证我们通过对外方法获取到的直接就是正确的值。
public class CalendarTest {
public static void main(String[] args) throws ParseException {
System.out.println("-------初始情况-------");
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
System.out.println(c.get(Calendar.DATE));
System.out.println(c.get(Calendar.HOUR));
System.out.println("-------重设置time-------");
c.setTime(new SimpleDateFormat("yyyyMMdd").parse("20170501"));
System.out.println(c.getTime());
System.out.println(c.get(Calendar.DATE));
System.out.println(c.get(Calendar.HOUR));
System.out.println("-------重设置field-------");
c.set(Calendar.MONTH, 4);
System.out.println(c.getTime());
System.out.println(c.get(Calendar.DATE));
System.out.println(c.get(Calendar.HOUR));
System.out.println("总结:time与field所代表时间点同步,所有的不同步全部在内部处理完成");
}
}
执行结果:
-------初始情况-------
Sat Jul 08 13:08:34 CST 2017
8
1
-------重设置time-------
Mon May 01 00:00:00 CST 2017
1
0
-------重设置field-------
Mon May 01 00:00:00 CST 2017
1
0
总结:time与field所代表时间点同步,所有的不同步全部在内部处理完成
Calendar中的两种解析模式
这也叫Calendar的容错性,lenient的开启与关闭使用setLenient(boolean lenient)方法来设置,true表示开启容错性(默认情况),false表示关闭该功能。
实例:
public class CalendarTest {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, 8);
c.set(Calendar.DAY_OF_MONTH, 33);
System.out.println(c.getTime()+"\n");
c.setLenient(false);
c.set(Calendar.MONTH, 8);
c.set(Calendar.DAY_OF_MONTH, 33);
System.out.println(c.getTime());
}
}
执行结果:
Tue Oct 03 13:18:48 CST 2017
Exception in thread "main" java.lang.IllegalArgumentException: DAY_OF_MONTH
at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2583)
at java.util.Calendar.updateTime(Calendar.java:2606)
at java.util.Calendar.getTimeInMillis(Calendar.java:1118)
at java.util.Calendar.getTime(Calendar.java:1091)
at JdkTest.main(JdkTest.java:87)
从上面的例子中可以看出,默认情况下,我们为Calendar的月份赋值为8即九月份,日期赋值为33即下一月3号,输出为10月3日,容错性将这种不符合规则的输入规则化处理了,而关闭容错性之后,同样的赋值只会报异常java.lang.IllegalArgumentException(非法参数异常)。
Calendar的使用
实例:
public class CalendarTest {
public static void main(String[] args) throws ParseException {
//通过SimpleDateFormat解析日期字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss.SSS");
Date date = sdf.parse("20170502 13:33:23.433");
//将Date格式日期转换成Calendar
Calendar c = Calendar.getInstance();
c.setTime(date);
//获取时间值
System.out.println(c.getTime());
System.out.println("年份为"+c.get(Calendar.YEAR));
System.out.println("月份为"+c.get(Calendar.MONTH));
System.out.println("日期为"+c.get(Calendar.DATE));
System.out.println("日期为"+c.get(Calendar.DAY_OF_MONTH));
System.out.println("日期为"+c.get(Calendar.DAY_OF_WEEK));
System.out.println("日期为"+c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("日期为"+c.get(Calendar.DAY_OF_YEAR));
System.out.println("时为"+c.get(Calendar.HOUR));
System.out.println("时为"+c.get(Calendar.HOUR_OF_DAY));
System.out.println("分为"+c.get(Calendar.MINUTE));
System.out.println("秒为"+c.get(Calendar.SECOND));
System.out.println("毫秒为"+c.get(Calendar.MILLISECOND));
System.out.println("星期为"+c.get(Calendar.WEEK_OF_MONTH));
System.out.println("星期为"+c.get(Calendar.WEEK_OF_YEAR));
System.out.println("历型为"+c.get(Calendar.ERA));
System.out.println("zone为"+c.get(Calendar.ZONE_OFFSET));
//设置
c.set(Calendar.MONTH, Calendar.APRIL);
System.out.println("修改后月份为"+c.get(Calendar.MONTH));
c.set(1999, 0, 23);
System.out.println(c.getTime());
c.set(2000, 1, 12, 13, 33, 14);
System.out.println(c.getTime());
c.set(2001, 2, 13, 14, 13);
System.out.println(c.getTime());
//运算
System.out.println("-----运算-----");
c.add(Calendar.YEAR, 12);
System.out.println(c.getTime());
c.add(Calendar.MONTH, -1);
System.out.println(c.getTime());
c.roll(Calendar.DATE, true);
System.out.println(c.getTime());
c.add(Calendar.DATE, 1);
System.out.println(c.getTime());
//roll与add运算对比
c.set(2000, 1, 29);
System.out.println(c.getTime());
c.roll(Calendar.DATE, 1);
System.out.println(c.getTime());
c.set(2000, 1, 29);
c.add(Calendar.DATE, 1);
System.out.println(c.getTime());
}
}
执行结果为:
Tue May 02 13:33:23 CST 2017
年份为2017
月份为4
日期为2
日期为2
日期为3
日期为1
日期为122
时为1
时为13
分为33
秒为23
毫秒为433
星期为1
星期为18
历型为1
zone为28800000
修改后月份为3
Sat Jan 23 13:33:23 CST 1999
Sat Feb 12 13:33:14 CST 2000
Tue Mar 13 14:13:14 CST 2001
-----运算-----
Wed Mar 13 14:13:14 CST 2013
Wed Feb 13 14:13:14 CST 2013
Thu Feb 14 14:13:14 CST 2013
Fri Feb 15 14:13:14 CST 2013
Tue Feb 29 14:13:14 CST 2000
Tue Feb 01 14:13:14 CST 2000
Wed Mar 01 14:13:14 CST 2000
对比上面最后的两行输出,可以看出add与roll的运算规则其实是不同的,roll的运算不会影响大规则(这里的大规则指的是月份的改变)的改变,而add会影响。