当前日期为:2019-12-20 15:43:04 星期五(时间戳1576827784092)
1)打印当前时间的Date为:
System.out.println("date="+new Date(1576827784092L));
//date=Thu Dec 20 15:43:04 CST 2019
2)打印当前时间的Calendar为:
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(1576827784092L);
System.out.println("calendar="+calendar.toString());
//calendar=java.util.GregorianCalendar[time=1576827784092,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=11,WEEK_OF_YEAR=51,WEEK_OF_MONTH=3,DAY_OF_MONTH=20,DAY_OF_YEAR=354,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=43,SECOND=4,MILLISECOND=92,ZONE_OFFSET=28800000,DST_OFFSET=0]
打印内容较长,格式化后为:
{
"time": "1576827784092",
"areFieldsSet": "true",
"areAllFieldsSet": "true",
"lenient": "true",
"zone": {
"sun.util.calendar.ZoneInfo": {
"id": "Asia/Shanghai",
"offset": "28800000",
"dstSavings": "0",
"useDaylight": "false",
"transitions": "19",
"lastRule": "null"
}
},
"firstDayOfWeek": "1",
"minimalDaysInFirstWeek": "1",
"ERA": "1",
"YEAR": "2019",
"MONTH": "11",
"WEEK_OF_YEAR": "51",
"WEEK_OF_MONTH": "3",
"DAY_OF_MONTH": "20",
"DAY_OF_YEAR": "354",
"DAY_OF_WEEK": "6",
"DAY_OF_WEEK_IN_MONTH": "3",
"AM_PM": "1",
"HOUR": "3",
"HOUR_OF_DAY": "15",
"MINUTE": "43",
"SECOND": "4",
"MILLISECOND": "92",
"ZONE_OFFSET": "28800000",
"DST_OFFSET": "0"
}
3)接着往下看:
当前时间为:2019-12-20 15:43:04 星期五
Calendar打印当前时间中的部分内容为:
"YEAR": "2019",
"MONTH": "11",
"WEEK_OF_YEAR": "51",
"WEEK_OF_MONTH": "3",
"DAY_OF_MONTH": "20",
"DAY_OF_YEAR": "354",
"DAY_OF_WEEK": "6",
"DAY_OF_WEEK_IN_MONTH": "3"
这里的月份和星期对不上,这是为什么呢?
Calendar中的解释,月份是从JANUARY开始的,但是JANUARY的值为0;星期是从周日开始的,所以周日是每周的第1天,所以今天星期五,这里calendar.get(Calendar.DAY_OF_WEEK)=6;同样的道理,calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH)=3;
源码中的内容如下
/**
* 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 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;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Sunday.
*/
public final static int SUNDAY = 1;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Monday.
*/
public final static int MONDAY = 2;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Tuesday.
*/
public final static int TUESDAY = 3;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Wednesday.
*/
public final static int WEDNESDAY = 4;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Thursday.
*/
public final static int THURSDAY = 5;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Friday.
*/
public final static int FRIDAY = 6;
/**
* Value of the {@link #DAY_OF_WEEK} field indicating
* Saturday.
*/
public final static int SATURDAY = 7;
/**
* Value of the {@link #MONTH} field indicating the
* first month of the year in the Gregorian and Julian calendars.
*/
public final static int JANUARY = 0;
/**
* Value of the {@link #MONTH} field indicating the
* second month of the year in the Gregorian and Julian calendars.
*/
public final static int FEBRUARY = 1;
/**
* Value of the {@link #MONTH} field indicating the
* third month of the year in the Gregorian and Julian calendars.
*/
public final static int MARCH = 2;
/**
* Value of the {@link #MONTH} field indicating the
* fourth month of the year in the Gregorian and Julian calendars.
*/
public final static int APRIL = 3;
/**
* Value of the {@link #MONTH} field indicating the
* fifth month of the year in the Gregorian and Julian calendars.
*/
public final static int MAY = 4;
/**
* Value of the {@link #MONTH} field indicating the
* sixth month of the year in the Gregorian and Julian calendars.
*/
public final static int JUNE = 5;
/**
* Value of the {@link #MONTH} field indicating the
* seventh month of the year in the Gregorian and Julian calendars.
*/
public final static int JULY = 6;
/**
* Value of the {@link #MONTH} field indicating the
* eighth month of the year in the Gregorian and Julian calendars.
*/
public final static int AUGUST = 7;
/**
* Value of the {@link #MONTH} field indicating the
* ninth month of the year in the Gregorian and Julian calendars.
*/
public final static int SEPTEMBER = 8;
/**
* Value of the {@link #MONTH} field indicating the
* tenth month of the year in the Gregorian and Julian calendars.
*/
public final static int OCTOBER = 9;
/**
* Value of the {@link #MONTH} field indicating the
* eleventh month of the year in the Gregorian and Julian calendars.
*/
public final static int NOVEMBER = 10;
/**
* Value of the {@link #MONTH} field indicating the
* twelfth month of the year in the Gregorian and Julian calendars.
*/
public final static int DECEMBER = 11;
/**
* Value of the {@link #MONTH} field indicating the
* thirteenth month of the year. Although <code>GregorianCalendar</code>
* does not use this value, lunar calendars do.
*/
public final static int UNDECIMBER = 12;