The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

The class also provides additional fields and methods for implementing a concrete calendar system outside the package. Those fields and methods are defined as protected.

1 Calendar instance

Calendar provides simple void constructor that wraps a Calendar object whose calendar fields have been initialized with the current date and time

Copy
<script>
    var cal = new Ax.util.Calendar();
</script>

2 Calendar fields

The calendar field values can be set by calling the set methods. Any field values set in a Calendar will not be interpreted until it needs to calculate its time value (milliseconds from the Epoch) or values of the calendar fields. Calling the get, getTimeInMillis, getTime, add and roll involves such calculation.

Fields are static and must be accessed statically.

Copy
<script>

    Ax.util.Calendar.YEAR;
    Ax.util.Calendar.MONTH;
    Ax.util.Calendar.DAY_OF_YEAR;
    Ax.util.Calendar.DAY_OF_MONTH;
    Ax.util.Calendar.DAY_OF_WEEK;
    Ax.util.Calendar.DAY_OF_WEEK_IN_MONTH;
    Ax.util.Calendar.HOUR;
    Ax.util.Calendar.MINUTE;
    Ax.util.Calendar.SECOND;
    Ax.util.Calendar.MILLISECOND;
    Ax.util.Calendar.HOUR_OF_DAY;
    
    Ax.util.Calendar.JANUARY;
    Ax.util.Calendar.FEBRUARY;
    Ax.util.Calendar.MARCH;
    Ax.util.Calendar.APRIL;
    Ax.util.Calendar.MAY;
    Ax.util.Calendar.JUNE;
    Ax.util.Calendar.JULY;
    Ax.util.Calendar.AUGUST;
    Ax.util.Calendar.SEPTEMBER;
    Ax.util.Calendar.OCTOBER;
    Ax.util.Calendar.NOVEMBER;
    Ax.util.Calendar.DECEMBER;

</script>

3 Create date objects

You can use calendar to create a Date

Copy
<script>
    function date(day, month, year) {
        var calendar = new Ax.util.Calendar();
        calendar.set(year, month, day);
        return calendar.getTime();
    }
    
    var date1 = date( 1, Ax.util.Calendar.APRIL,     2020);
    var date2 = date( 1, Ax.util.Calendar.APRIL,     2021);

</script>

4 Field manipulation

The calendar fields can be changed using three methods: set(), add(), and roll().

  • set(f, value) changes calendar field f to value. In addition, it sets an internal member variable to indicate that calendar field f has been changed. Although calendar field f is changed immediately, the calendar's time value in milliseconds is not recomputed until the next call to get(), getTime(), getTimeInMillis(), add(), or roll() is made. Thus, multiple calls to set() do not trigger multiple, unnecessary computations. As a result of changing a calendar field using set(), other calendar fields may also change, depending on the calendar field, the calendar field value, and the calendar system. In addition, get(f) will not necessarily return value set by the call to the set method after the calendar fields have been recomputed. The specifics are determined by the concrete calendar class.

    Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling set(Calendar.MONTH, Calendar.SEPTEMBER) sets the date to September 31, 1999. This is a temporary internal representation that resolves to October 1, 1999 if getTime()is then called. However, a call to set(Calendar.DAY_OF_MONTH, 30) before the call to getTime() sets the date to September 30, 1999, since no recomputation occurs after set() itself.

  • add(f, delta) adds delta to field f. This is equivalent to calling set(f, get(f) + delta) with two adjustments:
    • Add rule 1. The value of field f after the call minus the value of field f before the call is delta, modulo any overflow that has occurred in field f. Overflow occurs when a field value exceeds its range and, as a result, the next larger field is incremented or decremented and the field value is adjusted back into its range.
    • Add rule 2. If a smaller field is expected to be invariant, but it is impossible for it to be equal to its prior value because of changes in its minimum or maximum after field f is changed or other constraints, such as time zone offset changes, then its value is adjusted to be as close as possible to its expected value. A smaller field represents a smaller unit of time. HOUR is a smaller field than DAY_OF_MONTH. No adjustment is made to smaller fields that are not expected to be invariant. The calendar system determines what fields are expected to be invariant.
In addition, unlike set(), add() forces an immediate recomputation of the calendar's milliseconds and all fields.