|
@@ -0,0 +1,105 @@
|
|
|
+package com.miaxis.test;
|
|
|
+
|
|
|
+import com.miaxis.common.config.ShiShenConfig;
|
|
|
+import com.miaxis.common.utils.DateUtils;
|
|
|
+import com.nlf.calendar.Lunar;
|
|
|
+import com.nlf.calendar.Solar;
|
|
|
+import com.nlf.calendar.util.LunarUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+public class TestBaziDate {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ShiShenConfig shiShenConfig;
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 输入八字的年月日时
|
|
|
+ String yearGan = "戊";
|
|
|
+ String yearZhi = "辰";
|
|
|
+ String monthGan = "癸";
|
|
|
+ String monthZhi = "亥";
|
|
|
+ String dayGan = "壬";
|
|
|
+ String dayZhi = "午";
|
|
|
+ String hourGan = "己";
|
|
|
+ String hourZhi = "酉";
|
|
|
+
|
|
|
+ // 计算年月日时对应的年份
|
|
|
+ int year = calculateYear(yearGan, yearZhi);
|
|
|
+ int month = calculateMonth(monthGan, monthZhi);
|
|
|
+ int day = calculateDay(dayGan, dayZhi);
|
|
|
+ int hour = calculateHour(hourGan, hourZhi);
|
|
|
+
|
|
|
+ // 输出结果
|
|
|
+ System.out.println("转换后的年份:" + year);
|
|
|
+ System.out.println("转换后的月份:" + month);
|
|
|
+ System.out.println("转换后的日期:" + day);
|
|
|
+ System.out.println("转换后的小时:" + hour);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据干支计算年份
|
|
|
+ private static int calculateYear(String gan, String zhi) {
|
|
|
+ // 这里使用一个简单的规则,实际计算可能需要更多的细节
|
|
|
+ // 例如,甲子年为1984年
|
|
|
+ int baseYear = 1984;
|
|
|
+ int ganIndex = getGanIndex(gan);
|
|
|
+ int zhiIndex = getZhiIndex(zhi);
|
|
|
+ return baseYear + ganIndex * 10 + zhiIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据干支计算月份
|
|
|
+ private static int calculateMonth(String gan, String zhi) {
|
|
|
+ // 这里使用一个简单的规则,实际计算可能需要更多的细节
|
|
|
+ // 例如,甲子月为1月
|
|
|
+ int baseMonth = 1;
|
|
|
+ int ganIndex = getGanIndex(gan);
|
|
|
+ int zhiIndex = getZhiIndex(zhi);
|
|
|
+ return baseMonth + ganIndex * 2 + zhiIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据干支计算日期
|
|
|
+ private static int calculateDay(String gan, String zhi) {
|
|
|
+ // 这里使用一个简单的规则,实际计算可能需要更多的细节
|
|
|
+ // 例如,甲子日为1号
|
|
|
+ int baseDay = 1;
|
|
|
+ int ganIndex = getGanIndex(gan);
|
|
|
+ int zhiIndex = getZhiIndex(zhi);
|
|
|
+ return baseDay + ganIndex * 2 + zhiIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据干支计算小时
|
|
|
+ private static int calculateHour(String gan, String zhi) {
|
|
|
+ // 这里使用一个简单的规则,实际计算可能需要更多的细节
|
|
|
+ // 例如,甲子时为0时
|
|
|
+ int baseHour = 0;
|
|
|
+ int ganIndex = getGanIndex(gan);
|
|
|
+ int zhiIndex = getZhiIndex(zhi);
|
|
|
+ return baseHour + ganIndex * 2 + zhiIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取干的索引
|
|
|
+ private static int getGanIndex(String gan) {
|
|
|
+ // 这里简单地假设甲为0,乙为1,以此类推
|
|
|
+ String[] ganArray = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
|
|
|
+ for (int i = 0; i < ganArray.length; i++) {
|
|
|
+ if (gan.equals(ganArray[i])) {
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1; // 如果找不到,返回-1
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取支的索引
|
|
|
+ private static int getZhiIndex(String zhi) {
|
|
|
+ // 这里简单地假设子为0,丑为1,以此类推
|
|
|
+ String[] zhiArray = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
|
|
|
+ for (int i = 0; i < zhiArray.length; i++) {
|
|
|
+ if (zhi.equals(zhiArray[i])) {
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1; // 如果找不到,返回-1
|
|
|
+ }
|
|
|
+}
|