Calendar最大最难理解的误区,通过代码输出得到真正的结果,恍然大悟
代码演示:
package com.liangrui.javebase;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* @author liangrui
* @date 2020年1月10日 上午1:20:30
* @version 1.0.0
* @description Calendar最大最难理解的误区,通过代码输出得到真正的结果,恍然大悟
* cal.get(Calendar.DAY_OF_MONTH) 获取真实日期在当月的日期,括号里面的Calendar.DAY_OF_MONTH是为了“告诉”get方法取出“cal真实日期”在该月的日期
* cal.DAY_OF_MONTH 和Calendar.DAY_OF_MONTH是一样的,状态常量5,但是静态变量这样操作是不合法的
* Calendar.DAY_OF_MONTH 表示一种状态常量,为了告诉方法应该使用哪种属性
*/
public class CalendarBiggestMistake {
public static void main(String[] args) {
Date date=new Date();
Calendar cal=new GregorianCalendar();
cal.setTime(date);
System.out.println(cal.get(Calendar.DAY_OF_MONTH));//输出结果:10
System.out.println(cal.DAY_OF_MONTH);//输出结果:5
System.out.println(Calendar.DAY_OF_MONTH);//输出结果:5
}
}
来看看java中Calendar源码各个静态常量就能知晓了
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* year. This is a calendar-specific value; see subclass documentation.
*/
public final static int YEAR = 1;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* month. This is a calendar-specific value. The first month of
* the year in the Gregorian and Julian calendars is
* <code>JANUARY</code> 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 <code>get</code> and <code>set</code> indicating the
* week number within the current year. The first week of the year, as
* defined by <code>getFirstDayOfWeek()</code> and
* <code>getMinimalDaysInFirstWeek()</code>, has value 1. Subclasses define
* the value of <code>WEEK_OF_YEAR</code> for days before the first week of
* the year.
*
* @see #getFirstDayOfWeek
* @see #getMinimalDaysInFirstWeek
*/
public final static int WEEK_OF_YEAR = 3;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* week number within the current month. The first week of the month, as
* defined by <code>getFirstDayOfWeek()</code> and
* <code>getMinimalDaysInFirstWeek()</code>, has value 1. Subclasses define
* the value of <code>WEEK_OF_MONTH</code> for days before the first week of
* the month.
*
* @see #getFirstDayOfWeek
* @see #getMinimalDaysInFirstWeek
*/
public final static int WEEK_OF_MONTH = 4;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* day of the month. This is a synonym for <code>DAY_OF_MONTH</code>.
* The first day of the month has value 1.
*
* @see #DAY_OF_MONTH
*/
public final static int DATE = 5;
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* day of the month. This is a synonym for <code>DATE</code>.
* The first day of the month has value 1.
*
* @see #DATE
*/
public final static int DAY_OF_MONTH = 5;
/**
* Field number for <code>get</code> and <code>set</code> 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 <code>get</code> and <code>set</code> indicating the day
* of the week. This field takes values <code>SUNDAY</code>,
* <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
* <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>.
*
* @see #SUNDAY
* @see #MONDAY
* @see #TUESDAY
* @see #WEDNESDAY
* @see #THURSDAY
* @see #FRIDAY
* @see #SATURDAY
*/
public final static int DAY_OF_WEEK = 7;
还有很多就不一一列举了
总结:
cal.get(Calendar.DAY_OF_MONTH)
- 获取真实日期在当月的日期,括号里面的Calendar.DAY_OF_MONTH是为了 “告诉get方法” 取出 “cal真实日期” 在“该月的日期”。
Calendar.DAY_OF_MONTH
- 表示一种状态常量,为了告诉方法应该使用哪种属性,只是起到功能作用
cal.DAY_OF_MONTH
- 和Calendar.DAY_OF_MONTH是一样的,状态常量5,但是静态变量这样操作是不合法的