开发者

Mayan calendar in Java

How can I use the Maya calenda开发者_高级运维r in Java?


Has your calendar run out now? :-)


If you are really looking for a solution this Maya Calendar implementation looks quite good.

It implements a maya Tzolk'in calender using Java's GregorianCalendar. Dates can be retrieved both in Gregorian or Tzolk'in format.

Here are the core parts:

[...]
/** parses Date specified in Long Count format, e.g. "12.19.19.17.19" */
public void parseLongCountDate (String longCountDate) {
     String [] components = longCountDate.split("\\.");
     try {
          if (components.length != 5)
               throw new Exception("Expecting 5 numbers separated by dots");
          int baktuns = Integer.valueOf(components[0]);
          int katuns = Integer.valueOf(components[1]);
          int tuns = Integer.valueOf(components[2]);
          int winals = Integer.valueOf(components[3]);
          int kins = Integer.valueOf(components[4]);
          set (baktuns, katuns, tuns, winals, kins);
     } catch (Throwable e) {
          throw new IllegalArgumentException("Invalid long count date format: " 
          + e.getMessage());
     }
}

/** Set date to given long count date */
public void set (int baktuns, int katuns, int tuns, int uinals, int kins) {
     assert MayaTimeUnit.Kin.toDays (1) == 1;
     daysSinceGreatCycle =
          MayaTimeUnit.Baktun.toDays (baktuns) +
          MayaTimeUnit.Katun.toDays(katuns) +
          MayaTimeUnit.Tun.toDays(tuns) +
          MayaTimeUnit.Winal.toDays(uinals) +
          kins;
}

[...]

/** @return day name number in Tzolk'in calendar, e.g. it returns 0 (Ajaw) for the day "4 Ajaw" */
public Tzolkin toTzolkinDayName () {
     // The Tzolk'in date is counted forward from 4 Ajaw.
     return Tzolkin.DAYS[(daysSinceGreatCycle + 19) % 20]; // relative to Ajaw
}

/** @return day number in Tzolk'in calendar, e.g. it returns 4 for the day "4 Ajaw" */
public int toTzolkinDayNumber () {
     // The Tzolk'in date is counted forward from 4 Ajaw.
     return (daysSinceGreatCycle + 4) % 13;
}
[...]  

/** @return day name number in Haab calendar, e.g. it returns Yaxkin (5) for the day "14 Yaxk'in" */
public Haab toHaabDayName () {
     int d = (daysSinceGreatCycle + 349) % 365;
     return Haab.DAYS[d / 20];
}

/** @return day number in Haab calendar, e.g. it returns 14 for the day "14 Yaxk'in" */
public int toHaabDayNumber () {
     int d = (daysSinceGreatCycle + 349) % 365;
     return d % 20 - 1;
}
[...]  

/** @return Gregorian calendar representation of currently set date  */
public String toGregorianString () {
     Calendar c = toGregorianDate ();
     return format.format(c.getTime());
}

/** @return Converts currently defined date into Gregorian calendar */
public Calendar toGregorianDate () {
     Calendar c = (Calendar)greatCycleStartDate.clone();
     c.add(Calendar.DAY_OF_YEAR, daysSinceGreatCycle);
     return c;
}
[...]

In any case: Cool question :-)


The best way of using other calendars/chronologies in Java is the excellent Joda-Time library. It doesn't have a Mayan chronology itself, but you could right your own implementation of the Mayan rules and plug it in. Shouldn't be too onerous.


Use JodaTime. Oops, sorry, just a reflex when reading a question about java.util.Calendar ;-)

There are some Java applets on the web that might be helpful to you.


LOL, try setting the last selectable date to 21 December 2012 ? but is does not really end there, it just starts over so you want to start counting again after 21 December 2012?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜