Created
January 20, 2013 22:04
-
-
Save DanielChesters/4582110 to your computer and use it in GitHub Desktop.
Proposal for Intermediate Challenge #117 with Bonus 2.
Unreliable for date before 1st January 1970.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.daniel.maya; | |
import java.util.Calendar; | |
public class MayaLongCount { | |
public static String convertTo(int year, int month, int date) { | |
Calendar calendar = Calendar.getInstance(); | |
calendar.set(year, month - 1, date); | |
long day = calendar.getTimeInMillis() / 86_400_000; | |
long baktun = day / 144_000 + 12; | |
day %= 144_000; | |
long katun = day / 7_200 + 17; | |
day %= 7_200; | |
long tun = day / 360 + 16 ; | |
day %= 360; | |
long uinal = day / 20 + 7; | |
long kin = day % 20 + 5; | |
if (kin >= 20) { | |
kin -= 20; | |
uinal++; | |
} | |
if (uinal >= 18) { | |
uinal -= 18; | |
tun++; | |
} | |
if (tun >= 20) { | |
tun -= 20; | |
katun++; | |
} | |
if (katun >= 20) { | |
katun -= 20; | |
baktun++; | |
} | |
return String.format("%d.%d.%d.%d.%d", baktun, katun, tun, uinal, kin); | |
} | |
public static String convertFrom(int baktun, int katun, int tun, int uinal, int kin) { | |
baktun -= 12; | |
katun -= 17; | |
tun -= 16; | |
uinal -= 7; | |
kin -= 5; | |
long day = baktun * 144_000 + katun * 7_200 + tun * 360 + uinal * 20 + kin; | |
Calendar calendar = Calendar.getInstance(); | |
calendar.setTimeInMillis(day * 86_400_000); | |
return String.format("%d/%d/%d", calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR)); | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
System.out.println(convertTo(1970, 1, 1)); | |
System.out.println(convertTo(1988, 7, 20)); | |
System.out.println(convertTo(2012, 12, 12)); | |
System.out.println(convertTo(2012, 12, 21)); | |
System.out.println(convertFrom(12, 17, 16, 7, 5)); | |
System.out.println(convertFrom(13, 0, 0, 0, 0)); | |
System.out.println(convertFrom(14, 0, 0, 0, 0)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment