Java实现干支纪年法

时间:2021-05-06 09:42:52
public class 干支纪年法 {

// 甲、乙、丙、丁、戊、己、庚、辛、壬、癸
//public static final int[] skyBranch = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

// 子、丑、寅、卯、辰、巳、午、未、申、酉、戌、亥
//public static final char[] earthBranch = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l' };

// 甲、乙、丙、丁、戊、己、庚、辛、壬、癸
public static final char[] skyBranch = new char[] { '甲','乙','丙','丁','戊','己','庚','辛','壬','癸'};

// 子、丑、寅、卯、辰、巳、午、未、申、酉、戌、亥
public static final char[] earthBranch = new char[] { '子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥' };

public static void main(String[] args) {

for (int i = 0; i < 2030; i++) {
try {
caculate(i);
if ((i % 20) == 0) {
System.out.println("");
} else {
System.out.println(",");
}

} catch (RuntimeException e) {
System.out.println("Year" + i + " met exception.");

}
}

}

private static void caculate(int i) {
if (i < 4) {
throw new IllegalArgumentException("The starting year must be greater than 4");
}
int realYear = i - 4;
System.out.print("year" + i + " =[" + skyBranch[realYear % 10] + "][" + earthBranch[realYear % 12] + "]");

}

}