Class MyDate

  • All Implemented Interfaces:
    Serializable, Cloneable, Comparable<Calendar>

    public class MyDate
    extends GregorianCalendar
    This class extends GregorianCalendar, mostly to provide methods that make dealing with dates (without time of day) more convenient. Of course you can look at the API documentation for Calendar to see what methods MyDate inherits from Calendar. Note: MyDate's interface on Calendar uses month numbers 1,2,...,12, while Calendar itself uses 0,1,...,11. Beware of this if you try to use methods from the Calendar class!
    See Also:
    Serialized Form
    • Method Detail

      • makeDate

        public static MyDate makeDate​(int y,
                                      int m,
                                      int d)
        Returns a new MyDate object initialized to thegiven year, month and day. E.g.
        MyDate dt = makeDate(2015,1,17); // makes 17 January 2015.
      • getYear

        public int getYear()
        Returns the year of this MyDate object. E.g. 2015
      • getMonth

        public int getMonth()
        Returns the month of this MyDate object. Result is in [1,2,...,12]
      • getDay

        public int getDay()
        Returns the year of this MyDate object. Result is in [1,2,...,31]
      • incYear

        public void incYear​(int k)
        Adds k years to the current date. This MyDate object is modified!
      • incMonth

        public void incMonth​(int k)
        Adds k months to the current date. This MyDate object is modified!
      • incDay

        public void incDay​(int k)
        Adds k days to the current date. This MyDate object is modified!
      • clone

        public MyDate clone()
        Creates a new MyDate object identical to this. This is important because the increment methods modify the MyDate object on which they are called. So for example, after
        MyDate A = makeDate(2015,3,18);
             MyDate B = A;
             B.incDay(3);
        both A and B refer to the same MyDate object 3/21/2015. On the other hand, after
        MyDate C = makeDate(2015,3,18);
             MyDate D = C.clone();
             D.incDay(3);
        D refers to MyDate object 3/21/2015, while C refers to MyDate object 3/18/2015.
        Overrides:
        clone in class GregorianCalendar
      • toString

        public String toString()
        Returns a String representation of this MyDate in the usual mm/dd/yyyy format.
        Overrides:
        toString in class Calendar
      • daysUntil

        public int daysUntil​(MyDate d)
        Returns the number of days (with sign) between MyDate d and this. For example
        MyDate d1 = makeDate(2016,1,31);
             MyDate d2 = makeDate(2016,2,1);
             System.out.println(d1.daysUntil(d2));
        outputs 1.