home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-23 | 3.1 KB | 143 lines |
- package COM.odi.demo.pport;
-
- /**
- * <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
- *
- * A date, represented as a year, month, and day.
- */
-
- import java.util.Calendar;
- import COM.odi.*;
-
- final
- public class TradeDate
- {
-
- /* Years since 1900. */
-
- int year;
-
- /* Months since January - [0, 11]. */
-
- int month;
-
- /* Day of the month - [1, 31]. */
-
- int day;
-
-
- /*
- * Make a new TradeDate.
- * @param year years since 1900
- * @param month months since January - [0, 11]
- * @param day day of the month - [1, 31]
- */
- TradeDate(int year, int month, int day)
- {
- this.year = year;
- this.month = month;
- this.day = day;
- }
-
- /*
- * Copy a TradeDate.
- * @param date The TradeDate to copy.
- */
- TradeDate(TradeDate date)
- {
- year = date.year;
- month = date.month;
- day = date.day;
- }
-
- /*
- * Make a new TradeDate from a string.
- * @param string A string in format YYMMDD.
- */
- TradeDate(String string)
- {
- year = Character.digit(string.charAt(0), 10) * 10 +
- Character.digit(string.charAt(1), 10);
- month = Character.digit(string.charAt(2), 10) * 10 +
- Character.digit(string.charAt(3), 10) - 1;
- day = Character.digit(string.charAt(4), 10) * 10 +
- Character.digit(string.charAt(5), 10);
- }
-
- /**
- * Advance this TradeDate to the next calendar date.
- */
- void next()
- {
- day++;
- if (day > daysPerMonth(month, year)) {
- day = 1;
- month++;
- if (month >= 12) {
- month = 0;
- year++;
- }
- }
- }
-
- public String toString()
- {
- StringBuffer result = new StringBuffer(6);
- result.setLength(6);
- result.setCharAt(0, Character.forDigit(year / 10, 10));
- result.setCharAt(1, Character.forDigit(year % 10, 10));
- result.setCharAt(2, Character.forDigit((month + 1) / 10, 10));
- result.setCharAt(3, Character.forDigit((month + 1) % 10, 10));
- result.setCharAt(4, Character.forDigit(day / 10, 10));
- result.setCharAt(5, Character.forDigit(day % 10, 10));
- return result.toString();
- }
-
- public boolean equals(Object obj)
- {
- if (obj instanceof TradeDate) {
- TradeDate td = (TradeDate)obj;
- if (year == td.year && month == td.month && day == td.day)
- return true;
- }
- return false;
- }
-
- /* The number of days per month, not counting leap days. */
-
- private static int[] nDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
-
- /** The number of days per month.
- */
- static int daysPerMonth(int month, int year)
- {
- int result = nDays[month];
- if (month == 1 && ((year % 4) != 0))
- result = 29;
- return result;
- }
-
- /**
- * Compute the number of days from this date to the specified date.
- * @param endDate the target date
- * @return the number of days until the target date
- */
- public int daysUntil(TradeDate endDate)
- {
- Calendar c1 = Calendar.getInstance();
- c1.set(year, month, day, 0, 0, 0);
- Calendar c2 = Calendar.getInstance();
- c2.set(endDate.year, endDate.month, endDate.day, 0, 0, 0);
- long days = (c2.getTime().getTime() - c1.getTime().getTime()) / 86400000;
- return (int)days;
- }
-
-
- /* This class is never used as a persistent hash key. */
- public int hashCode() {
- return super.hashCode();
- }
-
- }
-
-