daily pastebin goal
10%
SHARE
TWEET

APCS_CH8_EasyDate.java

calcpage Mar 13th, 2013 34 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Calendar;
  2. import java.util.GregorianCalendar;
  3.  
  4. /**
  5.  * @author Gary Litvin
  6.  * @version 1.6, 01/01/11
  7.  *
  8.  * Appendix to:
  9.  *
  10.  * <i>Java Methods: Object-Oriented Programming and Data Structures</i>
  11.  * (Skylight Publishing 2011, ISBN 978-0-9824775-7-1)
  12.  *
  13.  * EasyDate provides a simple representation of a date with month, day, year
  14.  * fields.  EasyDate objects are immutable.
  15.  *
  16.    <xmp>
  17.    Example:
  18.    =======
  19.  
  20.        EasyDate today = new EasyDate();
  21.        System.out.println("Today is " + today);
  22.  
  23.        EasyDate tomorrow = today.add(1);
  24.        EasyDate yesterday = today.add(-1);
  25.  
  26.        int yr = today.getYear();
  27.        System.out.println(yr + " is a leap year? This is " +
  28.                                         EasyDate.isLeapYear(yr));
  29.  
  30.        EasyDate myBirthday = new EasyDate(bDayMonth, bDayDay, yr);
  31.  
  32.        if (today.equals(myBirthday))
  33.          System.out.println("Today is my birthday");
  34.        else if (yesterday.equals(myBirthday))
  35.          System.out.println("My birthday was yesterday");
  36.        else
  37.        {
  38.          if (myBirthday.compareTo(today) < 0)
  39.            myBirthday = new EasyDate(bDayMonth, bDayDay, yr + 1);
  40.          System.out.println(today.daysTo(myBirthday) +
  41.                         " days are left until my next birthday");
  42.        }
  43.  
  44.    </xmp>
  45.  */
  46. public class EasyDate implements Comparable<EasyDate>
  47. {
  48.   private int month;
  49.   private int day;
  50.   private int year;
  51.  
  52.   /**
  53.    * Number of milliseconds in 24 hours.
  54.    */
  55.   public static final long MILLIS_DAY = 24L * 3600 * 1000;
  56.  
  57.   /**
  58.    * Constructs a date for the current month, day, and year.
  59.    */
  60.   public EasyDate()
  61.   {
  62.     Calendar calendar = new GregorianCalendar();
  63.     month = calendar.get(Calendar.MONTH) + 1;
  64.     day = calendar.get(Calendar.DATE);
  65.     year = calendar.get(Calendar.YEAR);
  66.   }
  67.  
  68.   /**
  69.    * Constructs a date for given month, day, and year.
  70.    * @param month the month (1 - 12)
  71.    * @param day the day (1 - 31)
  72.    * @param year the year (1600 - 2100)
  73.    */
  74.   public EasyDate(int month, int day, int year)
  75.   {
  76.     this.month = month;
  77.     this.day = day;
  78.     this.year = year;
  79.   }
  80.  
  81.   /**
  82.    * Constructs a date equal to a given date.
  83.    * @param other the date to be copied.
  84.    */
  85.   public EasyDate(EasyDate other)
  86.   {
  87.     this.month = other.getMonth();
  88.     this.day = other.getDay();
  89.     this.year = other.getYear();
  90.   }
  91.  
  92.   /**
  93.    * Checks whether a given year is a leap year.
  94.    * @param year a year to be checked (1600-2100).
  95.    * @return true if <code>year</code> is a leap year, false otherwise.
  96.    */
  97.   public static boolean isLeapYear(int year)
  98.   {
  99.     return (new GregorianCalendar()).isLeapYear(year);
  100.   }
  101.  
  102.   /**
  103.    * Returns the month of this EasyDate.
  104.    */
  105.   public int getMonth()
  106.   {
  107.     return month;
  108.   }
  109.  
  110.   /**
  111.    * Returns the day of this EasyDate.
  112.    */
  113.   public int getDay()
  114.   {
  115.     return day;
  116.   }
  117.  
  118.   /**
  119.    * Returns the year of this EasyDate.
  120.    */
  121.   public int getYear()
  122.   {
  123.     return year;
  124.   }
  125.  
  126.   /**
  127.    * Returns a new EasyDate that is <code>numDays</code> later
  128.    * than this date (or earlier, if numDays is negative).
  129.    */
  130.   public EasyDate add(int numDays)
  131.   {
  132.     Calendar calendar = new GregorianCalendar(year, month - 1, day);
  133.     calendar.add(Calendar.DATE, numDays);
  134.     return new EasyDate(calendar.get(Calendar.MONTH) + 1,
  135.              calendar.get(Calendar.DATE), calendar.get(Calendar.YEAR));
  136.   }
  137.  
  138.   /**
  139.    * Returns the number of days from this date to <code>other</code>.
  140.    */
  141.   public int daysTo(EasyDate other)
  142.   {
  143.     Calendar calendar = new GregorianCalendar(other.getYear(),
  144.                                         other.getMonth() - 1, other.getDay());
  145.     long ms = calendar.getTimeInMillis();
  146.     calendar = new GregorianCalendar(year, month - 1, day);
  147.     ms -= calendar.getTimeInMillis();
  148.     return (int)(Math.round((double)ms / MILLIS_DAY));
  149.   }
  150.  
  151.   /**
  152.    * Compares <code>this</code> date to <code>other</code>.
  153.    * @return a positive integer (the number of days elapsed)
  154.    * if <code>this</code>
  155.    * date is later than other; returns a negative integer if <code>this</code>
  156.    * date is earlier than <code>other</code>; returns 0 if <code>this</code>
  157.    * date is the same as <code>other</code>.
  158.    */
  159.   public int compareTo(EasyDate other)
  160.   {
  161.     return other.daysTo(this);
  162.   }
  163.  
  164.   /**
  165.    * Checks whether <code>this</code> date id equal to <code>other</code>.
  166.    * @return true if <code>obj</code> represents the same date as
  167.    * <code>this</code>, false otherwise.
  168.    */
  169.   public boolean equals(Object obj)
  170.   {
  171.     if (!(obj instanceof EasyDate))
  172.       return false;
  173.     EasyDate other = (EasyDate)obj;
  174.     return month == other.getMonth() && day == other.getDay()
  175.                                             && year == other.getYear();
  176.   }
  177.  
  178.   /**
  179.    * Returns this date as a string in the mm/dd/yyyy format.
  180.    */
  181.   public String toString()
  182.   {
  183.     return String.format("%02d/%02d/%04d", getMonth(), getDay(), getYear());
  184.   }
  185. }
RAW Paste Data
Top