Calendar iso8601 = new Calendar.Builder().setCalendarType("iso8601").build(); Calendar.getAvailableCalendarTypes() // return [gregory]
gregory 和 iso8601:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** *设置GregorianCalendar更改日期。 这是从朱利安日期到格里高利日期的转换发生的时刻。 默认值是1582年10月15日(格里高利)。在此之前,日期将在儒略历中。 *要获取纯Julian日历,请将更改日期设置为Date(Long.MAX_VALUE)。 要获得纯公历,请将更改日期设置为日期(Long.MIN_VALUE)。 */ public void setGregorianChange(Date date) { long cutoverTime = date.getTime(); if (cutoverTime == gregorianCutover) { return; } // Before changing the cutover date, make sure to have the // time of this calendar. complete(); setGregorianChange(cutoverTime); }
/** * Gets a calendar with the specified time zone and locale. * The <code>Calendar</code> returned is based on the current time * in the given time zone with the given locale. * * @param zone the time zone to use * @param aLocale the locale for the week data * @return a Calendar. */ public static Calendar getInstance(TimeZone zone, Locale aLocale) { return createCalendar(zone, aLocale); }
private static Calendar createCalendar(TimeZone zone, Locale aLocale) { // BEGIN Android-changed: only support GregorianCalendar here return new GregorianCalendar(zone, aLocale); // END Android-changed: only support GregorianCalendar here }
/** * Constructs a <code>GregorianCalendar</code> based on the current time * in the given time zone with the given locale. * * @param zone the given time zone. * @param aLocale the given locale. */ public GregorianCalendar(TimeZone zone, Locale aLocale) { super(zone, aLocale); gdate = (BaseCalendar.Date) gcal.newCalendarDate(zone); setTimeInMillis(System.currentTimeMillis()); }
// 创建一个 Date val date = Date(1554362004L)
这里的 Date 类还有其他构造方法,不过已经被弃用了,现在通常都是用 Date(long: Long) 这个构造方法,直接传入时间戳,方便相互转化
/** * Sets the values for the fields <code>YEAR</code>, <code>MONTH</code>, * <code>DAY_OF_MONTH</code>, <code>HOUR_OF_DAY</code>, <code>MINUTE</code>, and * <code>SECOND</code>. * Previous values of other fields are retained. If this is not desired, * call {@link #clear()} first. * * @param year the value used to set the <code>YEAR</code> calendar field. * @param month the value used to set the <code>MONTH</code> calendar field. * Month value is 0-based. e.g., 0 for January. * @param date the value used to set the <code>DAY_OF_MONTH</code> calendar field. * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field. * @param minute the value used to set the <code>MINUTE</code> calendar field. * @param second the value used to set the <code>SECOND</code> calendar field. * @see #set(int,int) * @see #set(int,int,int) * @see #set(int,int,int,int,int) */ publicfinal 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); }
calendar.set(2019,4,8);
set(f, value) 方法日历的某个字段修改为 value 值,此外它还设置了一个内部成员变量,以指示日历字段 f 已经被更改。尽管日历字段 f 是立即更改的,但该 Calendar 所代表的时间却不会立即修改,直到下次调用 get()、getTime()、getTimeInMillis()、add() 或 roll() 时才会重新计算日历的时间。这被称为 set() 方法的延迟修改,采用延迟修改的优势是多次调用 set() 不会触发多次不必要的运算
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; areFieisSet[field] = true; ldsSet = false; stamp[field] = nextStamp++; if (nextStamp == Integer.MAX_VALUE) { adjustStamp(); } }
publicfinal static int YEAR = 1; publicfinal static int WEEK_OF_YEAR = 3; publicfinal static int DAY_OF_MONTH = 5; publicfinal static int DAY_OF_YEAR = 6; publicfinal static int DAY_OF_WEEK = 7;
/** * Adds or subtracts the specified amount of time to the given calendar field, * based on the calendar's rules. For example, to subtract 5 days from * the current time of the calendar, you can achieve it by calling: * <p><code>add(Calendar.DAY_OF_MONTH, -5)</code>. * * @param field the calendar field. * @param amount the amount of date or time to be added to the field. * @see #roll(int,int) * @see #set(int,int) */ abstractpublic void add(int field, int amount);
/** * Adds or subtracts (up/down) a single unit of time on the given time * field without changing larger fields. For example, to roll the current * date up by one day, you can achieve it by calling: * <p>roll(Calendar.DATE, true). * When rolling on the year or Calendar.YEAR field, it will roll the year * value in the range between 1 and the value returned by calling * <code>getMaximum(Calendar.YEAR)</code>. * When rolling on the month or Calendar.MONTH field, other fields like * date might conflict and, need to be changed. For instance, * rolling the month on the date 01/31/96 will result in 02/29/96. * When rolling on the hour-in-day or Calendar.HOUR_OF_DAY field, it will * roll the hour value in the range between 0 and 23, which is zero-based. * * @param field the time field. * @param up indicates if the value of the specified time field is to be * rolled up or rolled down. Use true if rolling up, false otherwise. * @see Calendar#add(int,int) * @see Calendar#set(int,int) */ abstractpublic void roll(int field, boolean up);
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calendartest/com.example.calendartest.MainActivity}java.lang.IllegalArgumentException: MONTH: 8 -> 9 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.IllegalArgumentException: MONTH: 8 -> 9 at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2824) at java.util.Calendar.updateTime(Calendar.java:3397) at java.util.Calendar.getTimeInMillis(Calendar.java:1761) at java.util.Calendar.getTime(Calendar.java:1734) at com.example.calendartest.MainActivity.onCreate(MainActivity.java:67) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)