KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > JulianDateStamp


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.datetime;
4
5 import jodd.util.ObjectUtil;
6 import jodd.util.HashCodeUtil;
7
8 import java.math.BigDecimal JavaDoc;
9
10 /**
11  * Julian Date stamp, for high precision calculations. Julian date is a real
12  * number and it basicly consist of two parts: integer and fraction. Integer
13  * part carries date information, fraction carries time information.
14  *
15  * <p>
16  * The Julian day or Julian day number (JDN) is the (integer) number of days that
17  * have elapsed since Monday, January 1, 4713 BC in the proleptic Julian calendar 1.
18  * That day is counted as Julian day zero. Thus the multiples of 7 are Mondays.
19  * Negative values can also be used.
20  *
21  * <p>
22  * The Julian Date (JD) is the number of days (with decimal fraction of the day) that
23  * have elapsed since 12 noon Greenwich Mean Time (UT or TT) of that day.
24  * Rounding to the nearest integer gives the Julian day number.
25  *
26  *
27  * For calculations that will have time precision of 1e-3 seconds, both
28  * fraction and integer part must have enough numerics in it. The problem is
29  * that integer part is big and, on the other hand fractional is small, and
30  * since final julian date is a sum of this two values, some fraction
31  * numerals may be lost. Therefore, for higher precision both
32  * fractional and intger part of julian date real number has to be
33  * preserved.
34  *
35  * @see TimeUtil
36  * @see JDateTime
37  * @see DateTimeStamp
38  *
39  * todo Add switches to MJD, TJD...
40  */

41 public class JulianDateStamp implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
42
43     /**
44      * Integer part of the Julian Date (JD).
45      */

46     public int integer;
47     
48
49     /**
50      * Fraction part of the Julian Date (JD).
51      * Should be always in [0.0, 1.0) range.
52      */

53     public double fraction;
54
55     /**
56      * Returns JDN. Note that JDN is not equals to {@link #integer}. It is calculated by
57      * rounding to the nearest integer.
58      */

59     public int getJulianDayNumber() {
60         if (fraction >= 0.5) {
61             return integer + 1;
62         }
63         return integer;
64     }
65
66
67     // ---------------------------------------------------------------- ctors
68

69     /**
70      * Default empty constructor.
71      */

72     public JulianDateStamp() {
73     }
74
75     /**
76      * Creates JD from a <code>double</code>.
77      */

78     public JulianDateStamp(double jd) {
79         integer = (int)jd;
80         fraction = jd - (double)integer;
81     }
82
83     /**
84      * Creates JD from both integer and fractional part using normalization.
85      * Normalization occurs when fractional part is out of range.
86      *
87      * @see #set(int, double)
88      *
89      * @param i integer part
90      * @param f fractional part should be in range [0.0, 1.0)
91      */

92     public JulianDateStamp(int i, double f) {
93         set(i, f);
94     }
95
96     /**
97      * Creates JD from <code>BigDecimal</code>.
98      */

99     public JulianDateStamp(BigDecimal JavaDoc bd) {
100         double d = bd.doubleValue();
101         integer = (int) d;
102         bd = bd.subtract(new BigDecimal JavaDoc((double) integer));
103         fraction = bd.doubleValue();
104     }
105
106
107     // ---------------------------------------------------------------- conversion
108

109
110     /**
111      * Returns <code>double</code> value of JD.
112      * <b>CAUTION</b>: double values may not be suit for precision math due to
113      * loss of precission.
114      */

115     public double doubleValue() {
116         return (double)integer + fraction;
117     }
118
119     /**
120      * Returns <code>BigDecimal</code> value of JD.
121      */

122     public BigDecimal JavaDoc toBigDecimal() {
123         BigDecimal JavaDoc bd = new BigDecimal JavaDoc((double) integer);
124         return bd.add(new BigDecimal JavaDoc(fraction));
125     }
126
127     /**
128      * Returns string representation of JD.
129      *
130      * @return julian integer as string
131      */

132     public String JavaDoc toString() {
133         String JavaDoc s = Double.toString(fraction);
134         int i = s.indexOf('.');
135         s = s.substring(i);
136         return integer + s;
137     }
138
139
140     // ---------------------------------------------------------------- math
141

142     /**
143      * Adds a JD to current instance.
144      */

145     public JulianDateStamp add(JulianDateStamp jds) {
146         int i = this.integer + jds.integer;
147         double f = this.fraction + jds.fraction;
148         set(i, f);
149         return this;
150     }
151
152     /**
153      * Adds a double to current instance.
154      */

155     public JulianDateStamp add(double delta) {
156         set(this.integer, this.fraction + delta);
157         return this;
158     }
159
160
161     /**
162      * Subtracts a JD from current instance.
163      */

164     public JulianDateStamp sub(JulianDateStamp jds) {
165         int i = this.integer - jds.integer;
166         double f = this.fraction -jds.fraction;
167         set(i, f);
168         return this;
169     }
170
171     /**
172      * Subtracts a double from current instance.
173      */

174     public JulianDateStamp sub(double delta) {
175         set(this.integer, this.fraction - delta);
176         return this;
177     }
178
179     /**
180      * Sets integer and fractional part with normalization.
181      * Normalization means that if double is out of range,
182      * values will be correctly fixed.
183      */

184     public void set(int i, double f) {
185         int fi = (int) f;
186         f -= fi;
187         integer += fi;
188         if (f < 0) {
189             f += 1;
190             integer--;
191         }
192         this.fraction = f;
193     }
194
195
196
197     // ---------------------------------------------------------------- equals & hashCode
198

199     public boolean equals(Object JavaDoc object) {
200         if (this == object) {
201             return true;
202         }
203         if (ObjectUtil.equalsType(object, this) == false) {
204             return false;
205         }
206         final JulianDateStamp stamp = (JulianDateStamp) object;
207
208         return (stamp.integer == this.integer) &&
209                 (Double.doubleToLongBits(stamp.fraction) == Double.doubleToLongBits(this.fraction));
210     }
211
212     public int hashCode() {
213         int result = HashCodeUtil.SEED;
214         result = HashCodeUtil.hash(result, this.integer);
215         result = HashCodeUtil.hash(result, this.fraction);
216         return result;
217     }
218
219     // ---------------------------------------------------------------- clone
220

221     protected Object JavaDoc clone() {
222         JulianDateStamp jds = new JulianDateStamp();
223         jds.integer = this.integer;
224         jds.fraction = this.fraction;
225         return jds;
226     }
227 }
228
Popular Tags