home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / TradeDate.java < prev    next >
Encoding:
Java Source  |  1997-04-23  |  3.1 KB  |  143 lines

  1. package COM.odi.demo.pport;
  2.  
  3. /**
  4.  *      <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
  5.  *
  6.  * A date, represented as a year, month, and day.
  7.  */
  8.  
  9. import java.util.Calendar;
  10. import COM.odi.*;
  11.  
  12. final
  13. public class TradeDate
  14. {
  15.  
  16.   /* Years since 1900. */
  17.  
  18.   int year;
  19.  
  20.   /* Months since January - [0, 11]. */
  21.  
  22.   int month;
  23.  
  24.   /* Day of the month - [1, 31]. */
  25.  
  26.   int day;
  27.  
  28.  
  29.   /*
  30.    * Make a new TradeDate.
  31.    * @param year years since 1900
  32.    * @param month months since January - [0, 11] 
  33.    * @param day day of the month - [1, 31]
  34.    */
  35.   TradeDate(int year, int month, int day)
  36.   {
  37.     this.year = year;
  38.     this.month = month;
  39.     this.day = day;
  40.   }
  41.  
  42.   /*
  43.    * Copy a TradeDate.
  44.    * @param date The TradeDate to copy.
  45.    */
  46.   TradeDate(TradeDate date)
  47.   {
  48.     year = date.year;
  49.     month = date.month;
  50.     day = date.day;
  51.   }
  52.  
  53.   /*
  54.    * Make a new TradeDate from a string.
  55.    * @param string A string in format YYMMDD.
  56.    */
  57.   TradeDate(String string)
  58.   {
  59.     year = Character.digit(string.charAt(0), 10) * 10 +
  60.       Character.digit(string.charAt(1), 10);
  61.     month = Character.digit(string.charAt(2), 10) * 10 +
  62.       Character.digit(string.charAt(3), 10) - 1;
  63.     day = Character.digit(string.charAt(4), 10) * 10 +
  64.       Character.digit(string.charAt(5), 10);
  65.   }
  66.  
  67.   /**
  68.    * Advance this TradeDate to the next calendar date.
  69.    */
  70.   void next()
  71.   {
  72.     day++;
  73.     if (day > daysPerMonth(month, year)) {
  74.       day = 1;
  75.       month++;
  76.       if (month >= 12) {
  77.     month = 0;
  78.     year++;
  79.       }
  80.     }
  81.   }
  82.  
  83.   public String toString()
  84.   {
  85.     StringBuffer result = new StringBuffer(6);
  86.     result.setLength(6);
  87.     result.setCharAt(0, Character.forDigit(year / 10, 10));
  88.     result.setCharAt(1, Character.forDigit(year % 10, 10));
  89.     result.setCharAt(2, Character.forDigit((month + 1) / 10, 10));
  90.     result.setCharAt(3, Character.forDigit((month + 1) % 10, 10));
  91.     result.setCharAt(4, Character.forDigit(day / 10, 10));
  92.     result.setCharAt(5, Character.forDigit(day % 10, 10));
  93.     return result.toString();
  94.   }
  95.  
  96.   public boolean equals(Object obj)
  97.   {
  98.     if (obj instanceof TradeDate) {
  99.       TradeDate td = (TradeDate)obj;
  100.       if (year == td.year && month == td.month && day == td.day)
  101.     return true;
  102.     }
  103.     return false;
  104.   }
  105.  
  106.   /* The number of days per month, not counting leap days. */
  107.  
  108.   private static int[] nDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  109.  
  110.   /** The number of days per month.
  111.    */
  112.   static int daysPerMonth(int month, int year)
  113.   {
  114.     int result = nDays[month];
  115.     if (month == 1 && ((year % 4) != 0))
  116.       result = 29;
  117.     return result;
  118.   }
  119.  
  120.   /**
  121.    * Compute the number of days from this date to the specified date.
  122.    * @param endDate the target date
  123.    * @return the number of days until the target date
  124.    */
  125.   public int daysUntil(TradeDate endDate)
  126.   {
  127.     Calendar c1 = Calendar.getInstance();
  128.     c1.set(year, month, day, 0, 0, 0);
  129.     Calendar c2 = Calendar.getInstance();
  130.     c2.set(endDate.year, endDate.month, endDate.day, 0, 0, 0);
  131.     long days = (c2.getTime().getTime() - c1.getTime().getTime()) / 86400000;
  132.     return (int)days;
  133.   }
  134.  
  135.  
  136.   /* This class is never used as a persistent hash key. */
  137.   public int hashCode() {
  138.     return super.hashCode();
  139.   }
  140.  
  141. }
  142.  
  143.