001: public class Day
002: {
003:    /**
004:       Constructs a day with a given year, month, and day
005:       of the Julian/Gregorian calendar. The Julian calendar
006:       is used for all days before October 15, 1582
007:       @param aYear a year != 0
008:       @param aMonth a month between 1 and 12
009:       @param aDate a date between 1 and 31
010:    */
011:    public Day(int aYear, int aMonth, int aDate)
012:    {
013:       year = aYear;
014:       month = aMonth;
015:       date = aDate;
016:       ymdValid = true;
017:       julianValid = false;
018:    }
019: 
020:    /**
021:       Returns the year of this day
022:       @return the year
023:    */
024:    public int getYear()
025:    {
026:       ensureYmd();
027:       return year;
028:    }
029: 
030:    /**
031:       Returns the month of this day
032:       @return the month
033:    */
034:    public int getMonth()
035:    {
036:       ensureYmd();
037:       return month;
038:    }
039: 
040:    /**
041:       Returns the day of the month of this day
042:       @return the day of the month
043:    */
044:    public int getDate()
045:    {
046:       ensureYmd();
047:       return date;
048:    }
049: 
050:    /**
051:       Returns a day that is a certain number of days away from
052:       this day
053:       @param n the number of days, can be negative
054:       @return a day that is n days away from this one
055:    */
056:    public Day addDays(int n)
057:    {
058:       ensureJulian();
059:       return new Day(julian + n);
060:    }
061: 
062:    /**
063:       Returns the number of days between this day and another
064:       day
065:       @param other the other day
066:       @return the number of days that this day is away from 
067:       the other (>0 if this day comes later)
068:    */
069:    public int daysFrom(Day other)
070:    {
071:       ensureJulian();
072:       other.ensureJulian();
073:       return julian - other.julian;
074:    }
075: 
076:    private Day(int aJulian)
077:    {
078:       julian = aJulian;
079:       ymdValid = false;
080:       julianValid = true;
081:    }
082: 
083:    /**
084:       Computes the Julian day number of this day if 
085:       necessary
086:    */
087:    private void ensureJulian()
088:    {  
089:       if (julianValid) return;
090:       julian = toJulian(year, month, date);
091:       julianValid = true;
092:    }
093: 
094:    /**
095:       Converts this Julian day mumber to a calendar date if necessary.
096:    */
097:    private void ensureYmd()
098:    {  
099:       if (ymdValid) return;
100:       int[] ymd = fromJulian(julian);
101:       year = ymd[0];
102:       month = ymd[1];
103:       date = ymd[2];
104:       ymdValid = true;
105:    }
106: 
107:    /**
108:       Computes the Julian day number of the given day day.
109: 
110:       @param year a year
111:       @param month a month
112:       @param date a day of the month
113:       @return The Julian day number that begins at noon of 
114:       the given day
115:       Positive year signifies CE, negative year BCE. 
116:       Remember that the year after 1 BCE is 1 CE.
117: 
118:       A convenient reference point is that May 23, 1968 noon
119:       is Julian day number 2440000.
120: 
121:       Julian day number 0 is a Monday.
122: 
123:       This algorithm is from Press et al., Numerical Recipes
124:       in C, 2nd ed., Cambridge University Press 1992
125:    */
126:    private static int toJulian(int year, int month, int date)
127:    {  
128:       int jy = year;
129:       if (year < 0) jy++;
130:       int jm = month;
131:       if (month > 2) jm++;
132:       else
133:       {  
134:          jy--;
135:          jm += 13;
136:       }
137:       int jul = (int) (java.lang.Math.floor(365.25 * jy) 
138:             + java.lang.Math.floor(30.6001 * jm) + date + 1720995.0);
139: 
140:       int IGREG = 15 + 31 * (10 + 12 * 1582);
141:          // Gregorian Calendar adopted Oct. 15, 1582
142: 
143:       if (date + 31 * (month + 12 * year) >= IGREG)
144:          // Change over to Gregorian calendar
145:       {  
146:          int ja = (int) (0.01 * jy);
147:          jul += 2 - ja + (int) (0.25 * ja);
148:       }
149:       return jul;
150:    }
151: 
152:    /**
153:       Converts a Julian day number to a calendar date.
154:       
155:       This algorithm is from Press et al., Numerical Recipes
156:       in C, 2nd ed., Cambridge University Press 1992
157: 
158:       @param j  the Julian day number
159:       @return an array whose 0 entry is the year, 1 the month,
160:       and 2 the day of the month.
161:    */
162:    private static int[] fromJulian(int j)
163:    {  
164:       int ja = j;
165:    
166:       int JGREG = 2299161;
167:          // The Julian day number of the adoption of the Gregorian calendar    
168: 
169:       if (j >= JGREG)
170:          // Cross-over to Gregorian Calendar produces this correction
171:       {  
172:          int jalpha = (int) (((float) (j - 1867216) - 0.25) 
173:              / 36524.25);
174:          ja += 1 + jalpha - (int) (0.25 * jalpha);
175:       }
176:       int jb = ja + 1524;
177:       int jc = (int) (6680.0 + ((float) (jb - 2439870) - 122.1)
178:           / 365.25);
179:       int jd = (int) (365 * jc + (0.25 * jc));
180:       int je = (int) ((jb - jd) / 30.6001);
181:       int date = jb - jd - (int) (30.6001 * je);
182:       int month = je - 1;
183:       if (month > 12) month -= 12;
184:       int year = jc - 4715;
185:       if (month > 2) --year;
186:       if (year <= 0) --year;
187:       return new int[] { year, month, date };
188:    }
189: 
190:    private int year;
191:    private int month;
192:    private int date;
193:    private int julian;
194:    private boolean ymdValid;
195:    private boolean julianValid;
196: }
197: 
198: 
199: 
200: 
201: