爪哇玛雅历法

2022-09-01 23:49:31

如何在 Java 中使用 Maya 日历?


答案 1

您的日历现在用完了吗?:-)


答案 2

如果您真的在寻找解决方案,那么此Maya日历实现看起来相当不错。

使用Java的.日期可以以公历或 Tzolk'in 格式检索。GregorianCalendar

以下是核心部分:

[...]
/** 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;
}
[...]

无论如何:很酷的问题:-)