1 3 package jodd.datetime; 4 5 import jodd.util.ObjectUtil; 6 import jodd.util.HashCodeUtil; 7 8 import java.math.BigDecimal ; 9 10 41 public class JulianDateStamp implements java.io.Serializable , Cloneable { 42 43 46 public int integer; 47 48 49 53 public double fraction; 54 55 59 public int getJulianDayNumber() { 60 if (fraction >= 0.5) { 61 return integer + 1; 62 } 63 return integer; 64 } 65 66 67 69 72 public JulianDateStamp() { 73 } 74 75 78 public JulianDateStamp(double jd) { 79 integer = (int)jd; 80 fraction = jd - (double)integer; 81 } 82 83 92 public JulianDateStamp(int i, double f) { 93 set(i, f); 94 } 95 96 99 public JulianDateStamp(BigDecimal bd) { 100 double d = bd.doubleValue(); 101 integer = (int) d; 102 bd = bd.subtract(new BigDecimal ((double) integer)); 103 fraction = bd.doubleValue(); 104 } 105 106 107 109 110 115 public double doubleValue() { 116 return (double)integer + fraction; 117 } 118 119 122 public BigDecimal toBigDecimal() { 123 BigDecimal bd = new BigDecimal ((double) integer); 124 return bd.add(new BigDecimal (fraction)); 125 } 126 127 132 public String toString() { 133 String s = Double.toString(fraction); 134 int i = s.indexOf('.'); 135 s = s.substring(i); 136 return integer + s; 137 } 138 139 140 142 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 155 public JulianDateStamp add(double delta) { 156 set(this.integer, this.fraction + delta); 157 return this; 158 } 159 160 161 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 174 public JulianDateStamp sub(double delta) { 175 set(this.integer, this.fraction - delta); 176 return this; 177 } 178 179 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 199 public boolean equals(Object 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 221 protected Object clone() { 222 JulianDateStamp jds = new JulianDateStamp(); 223 jds.integer = this.integer; 224 jds.fraction = this.fraction; 225 return jds; 226 } 227 } 228 | Popular Tags |