public
Created

Proposal for Intermediate Challenge #117 with Bonus 2. Unreliable for date before 1st January 1970.

  • Download Gist
MayaLongCount.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
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));
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.