CalendarDate

Aus Byte-Welt Wiki
Version vom 25. März 2011, 16:25 Uhr von AndreUhres (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „Die Klasse CalendarDate basiert auf den Algoritmen, die hier vorgestellt werden: http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html . Die weiter unten stehe…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springenZur Suche springen

Die Klasse CalendarDate basiert auf den Algoritmen, die hier vorgestellt werden: http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html .

Die weiter unten stehende Klasse CalendarDateDemo zeigt ein Anwendungsbeispiel.

<code=java>public class CalendarDate {

   private long year;
   private long month;
   private long day;
   //Earliest date on Gregorian calendar:
   private static long calstart = days(new CalendarDate(1582, 10, 1));
   public CalendarDate() {
       this(0, 0, 0);
   }
   public CalendarDate(final long year, final long month, final long day) {
       this.year = year;
       this.month = month;
       this.day = day;
   }
   /* convert day number to CalendarDate (year,month,day)*/
   public static CalendarDate toCalendarDate(final long dayNumber) {
       long year = (10000 * dayNumber + 14780) / 3652425;
       long day = dayNumber - (year * 365 + year / 4 - year / 100 + year / 400);
       if (day < 0) {
           year--;
           day = dayNumber - (year * 365 + year / 4 - year / 100 + year / 400);
       }
       long month = (52 + 100 * day) / 3060;
       return new CalendarDate(year + (month + 2) / 12,
               (month + 2) % 12 + 1,
               day - (month * 306 + 5) / 10 + 1);
   }
   /* return day number, or throw IllegalArgumentException if bad date */
   public static long days(final CalendarDate date) {
       long dayNumber = toDay(date);
       if (dayNumber < calstart) {
           throw new IllegalArgumentException("Dates before Oct. 1582 are inaccurate.");
       }
       if (!date.equals(toCalendarDate(dayNumber))) {
           throw new IllegalArgumentException("Illegal date " + date);
       }
       return dayNumber;
   }
   /* convert date to day number */
   public static long toDay(final CalendarDate d) {
       long month = (d.getMonth() + 9) % 12; //mar=0, feb=11
       long year = d.getYear() - month / 10;   //if Jan/Feb, year--
       return year * 365 + year / 4 - year / 100 + year / 400
               + (month * 306 + 5) / 10 + (d.getDay() - 1);
   }
   @Override
   public String toString() {
       return getDay() + "/" + getMonth() + "/" + getYear();
   }
   public long getYear() {
       return year;
   }
   public long getMonth() {
       return month;
   }
   public long getDay() {
       return day;
   }
   public static long getCalstart() {
       return calstart;
   }
   @Override
   @SuppressWarnings("AccessingNonPublicFieldOfAnotherObject")
   public boolean equals(final Object obj) {
       if (obj == null) {
           return false;
       }
       if (getClass() != obj.getClass()) {
           return false;
       }
       final CalendarDate other = (CalendarDate) obj;
       if (this.year != other.year) {
           return false;
       }
       if (this.month != other.month) {
           return false;
       }
       if (this.day != other.day) {
           return false;
       }
       return true;
   }
   @Override
   public int hashCode() {
       int hash = 3;
       hash = 59 * hash + (int) (this.year ^ (this.year >>> 32));
       hash = 59 * hash + (int) (this.month ^ (this.month >>> 32));
       hash = 59 * hash + (int) (this.day ^ (this.day >>> 32));
       return hash;
   }

} </code=java> <code=java> import java.util.*;

public class CalendarDateDemo {

   private final Calendar NOW = Calendar.getInstance();
   public CalendarDateDemo(final String[] args) {
       String toString = Arrays.toString(args);
       System.out.println("\n" + toString);
       if (args.length > 6) {// too many arguments
           System.out.println("Usage: %s [y m d] [ n | y m d ]\n");
       }
       long[] i = new long[args.length];
       int j = 0;
       /* Convert args to long integer */
       for (final String string : args) {
           try {
               i[j] = Long.parseLong(args[j]);
           } catch (final NumberFormatException numberFormatException) {
               System.out.println("Argument conversion error.");
           }
           j++;
       }
       long base = 0;
       if (args.length > 3) { // arguments with base date
           base = CalendarDate.days(new CalendarDate(i[0], i[1], i[2]));
       } else if (args.length == 3) {// no base date -> set base to current date
           CalendarDate date = new CalendarDate(NOW.get(Calendar.YEAR),
                   NOW.get(Calendar.MONTH) + 1, NOW.get(Calendar.DAY_OF_MONTH));
           base = CalendarDate.days(date);
       }
       //
       if ((args.length) % 3 == 1) { // Compute new date from offset
           CalendarDate date = CalendarDate.toCalendarDate(base + i[args.length - 1]);
           System.out.println("    --> " + date);
           if (base + i[args.length - 1] < CalendarDate.getCalstart()) {
               System.out.println("WARNING: Dates before Oct. 1582 are inaccurate.");
           }
       } else if ((args.length) % 3 == 0) { // Compute days between dates
           CalendarDate date = new CalendarDate(i[args.length - 3],
                   i[args.length - 2], i[args.length - 1]);
           long days = CalendarDate.days(date);
           System.out.println("    --> " + (days - base) + " days");
       } else { // Wrong number of arguments
           System.out.println("Usage: %s [y m d] [ n | y m d ]\n");
       }
   }
   public static void main(String... args) {
       new CalendarDateDemo(new String[]{"2004", "3", "25", "2005", "3", "25"});
       new CalendarDateDemo(new String[]{"731970"});
       new CalendarDateDemo(new String[]{"732335"});
       new CalendarDateDemo(new String[]{"2006", "3", "25", "20"});
       new CalendarDateDemo(new String[]{"2012", "3", "25"});
   }

} </code=java>