// // StarDate.java: // StarDate Class for astronomical calculations. // Copyright 1996 by Akkana Peck. // // package Akk.Astro; import java.util.Date; // A date class which can calculate decimal years, and knows how // to initialize itself from a string with decimal days, e.g. // "1997 Apr 1.034170" class StarDate extends Date { StarDate() { super(); } StarDate(Date d) { super(); setTime(d.getTime()); } StarDate(String s) { setTime(s); } void setTime(String s) { // see if the day is decimal: int dot = s.indexOf('.'); if (dot > 0) { String maindate = s.substring(0, dot); String decimal = s.substring(dot); float correction = Float.valueOf(decimal).floatValue() * 24*60*60*1000; // This is so stupid -- Date has a ctor to set itself // from a string, but has no way of setting itself // from a string after construction! Sigh. Date stupid = new Date(maindate); // Now add the appropriate decimal days: setTime(stupid.getTime() + (long)correction); } else { Date stupid = new Date(s); setTime(stupid.getTime()); } } public void addDays(int days) { setTime(getTime() + days * (24*60*60*1000)); } public double daysSince(StarDate t) { return ((double)(getTime() - t.getTime())) / (24.*60.*60.*1000.); } public double decimalYears() { // getTime() returns milliseconds Math.since Jan 1, 1970, so: return getTime() / 365.242191 / (24*60*60*1000); } public double getTimeAsDecimalDay() { Double doub = new Double((double)getTime() / (24.*60.*60.*1000.)); System.out.println("doub = " + doub.toString()); return doub.doubleValue() - doub.intValue(); } public double getJulianDate() { return ( daysSince(new StarDate("Jan 1 0:00 PST 1970")) + 2440587.83333333333); } // // toDateString() only prints the date, not day or time: // public String toDateString() { String s = super.toString(); // we only want the second, third, and last words (month day year): int firstindex = s.indexOf(' ')+1; int index = s.indexOf(' ', firstindex); index = s.indexOf(' ', index+1); int yearindex = s.lastIndexOf(' '); return s.substring(firstindex, index) + s.substring(yearindex); } // // toString() prints the date and time, but not day of the week // public String toString() { String s = super.toString(); // we want everything except the first word: int firstindex = s.indexOf(' ')+1; return s.substring(firstindex); } // // toJulianString() prints the string value of the julian date; // if we rely on Double.toString(), we get scientific notation // whether we want it or not. // public String toJulianString() { double JD = getJulianDate(); Long l = new Long((long)JD); String decimal = new Double(JD - l.doubleValue()).toString(); decimal = decimal.substring(decimal.indexOf(".")); return l.toString() + decimal; } }