Java 中的玛雅日历

发布于 2024-08-27 02:43:23 字数 28 浏览 6 评论 0原文

如何在 Java 中使用 Maya 日历?

How can I use the Maya calendar in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太阳哥哥 2024-09-03 02:43:23

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

Has your calendar run out now? :-)

一影成城 2024-09-03 02:43:23

如果您确实正在寻找解决方案Maya 日历实现 看起来相当不错。

它使用 Java 的 GregorianCalendar 实现了 Maya Tzolk'in 日历。可以以公历或 Tzolk'in 格式检索日期。

以下是核心部分:

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

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

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 :-)

滿滿的愛 2024-09-03 02:43:23

在 Java 中使用其他日历/年表的最佳方法是优秀的 Joda-Time 库。它本身没有玛雅年表,但您可以纠正自己的玛雅规则实施并将其插入。不应该太繁重。

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.

樱娆 2024-09-03 02:43:23

使用 JodaTime。哎呀,抱歉,只是阅读有关 java.util.Calendar 的问题时的反射;-)

有一些 Java 小程序可能对您有帮助的网络。

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.

动次打次papapa 2024-09-03 02:43:23

哈哈,
尝试将最后可选日期设置为 2012 年 12 月 21 日?
但这并没有真正结束,它只是重新开始,所以您想在 2012 年 12 月 21 日之后再次开始计数吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文