八字分四柱,每柱2字,分别对应年、月、日、时

一、天干,地支

/*** 天干**/
public enum TianGan {甲(1, "甲"), 乙(2,"乙"),丙(3, "丙"),丁(4, "丁"),戊(5, "戊"),己(6, "己"),庚(7, "庚"),辛(8, "辛"),壬(9, "壬"),癸(10, "癸");private int code;private String desc;TianGan(int code, String desc){this.code = code;this.desc = desc;}public int getCode() {return code;}public String getDesc() {return desc;}public static TianGan getByCode(int code){for (TianGan tg : values()){if (tg.code == code)return tg;}return 甲;}}
/*** 地支** **/
public enum  DiZhi {子(1, "子", "23:00-01:00", "00:00-02:00", "鼠"),丑(2, "丑", "01:00-03:00", "02:00-04:00", "牛"),寅(3, "寅", "03:00-05:00", "04:00-06:00", "虎"),卯(4, "卯", "05:00-07:00", "06:00-08:00", "兔"),辰(5, "辰", "07:00-09:00", "08:00-10:00", "龙"),巳(6, "巳", "09:00-11:00", "10:00-12:00", "蛇"),午(7, "午", "11:00-13:00", "12:00-14:00", "马"),未(8, "未", "13:00-15:00", "14:00-16:00", "羊"),申(9, "申", "15:00-17:00", "16:00-18:00", "猴"),酉(10, "酉", "17:00-19:00", "18:00-20:00", "鸡"),戌(11, "戌", "19:00-21:00", "20:00-22:00", "狗"),亥(12, "亥", "21:00-23:00", "22:00-00:00", "猪");private int code;private String desc,shiChen, shiChenBeforeTang,shengXiao;DiZhi(int code, String desc, String shiChen, String shiChenBeforeTang, String shengXiao){this.code = code;this.desc = desc;this.shiChen = shiChen;this.shiChenBeforeTang = shiChenBeforeTang;this.shengXiao = shengXiao;}public int getCode() {return code;}public String getDesc() {return desc;}public static DiZhi getByCode(int code){for (DiZhi dz : values()){if (code == dz.code)return dz;}return DiZhi.子;}/*** 获取时辰时间段** **/public String getShiChen() {return shiChen;}/*** 获取唐前时辰时间段** **/public String getShiChenBeforeTang() {return shiChenBeforeTang;}/*** 生肖** **/public String getShengXiao() {return shengXiao;}}
public enum ShiChen {早子(1, "早子", "23:00-00:00", "00:00-01:00"),丑(2, "丑", "01:00-03:00", "02:00-04:00"),寅(3, "寅", "03:00-05:00", "04:00-06:00"),卯(4, "卯", "05:00-07:00", "06:00-08:00"),辰(5, "辰", "07:00-09:00", "08:00-10:00"),巳(6, "巳", "09:00-11:00", "10:00-12:00"),午(7, "午", "11:00-13:00", "12:00-14:00"),未(8, "未", "13:00-15:00", "14:00-16:00"),申(9, "申", "15:00-17:00", "16:00-18:00"),酉(10, "酉", "17:00-19:00", "18:00-20:00"),戌(11, "戌", "19:00-21:00", "20:00-22:00"),亥(12, "亥", "21:00-23:00", "22:00-00:00"),晚子(13, "晚子", "00:00-01:00", "01:00-02:00");private int code;private String desc,shiChen, shiChenBeforeTang;ShiChen(int code, String desc, String shiChen, String shiChenBeforeTang){this.code = code;this.desc = desc;this.shiChen = shiChen;this.shiChenBeforeTang = shiChenBeforeTang;}public int getCode() {return code;}public String getDesc() {return desc;}public static ShiChen getByCode(int code){for (ShiChen dz : values()){if (code == dz.code)return dz;}return ShiChen.早子;}/*** 获取时辰时间段** **/public String getShiChen() {return shiChen;}/*** 获取唐前时辰时间段** **/public String getShiChenBeforeTang() {return shiChenBeforeTang;}}

二、八字计算

1、基础数据表

// 纪月
private static final String[] JI_YUE_TIAN_GAN = new String[]{"甲、已","乙、庚","丙、辛","丁、壬","戊、癸"
};
private static final String[][] JI_YUE = new String[][]{{"丙寅","丁卯","戊辰","已巳","庚午","辛未","壬申","癸酉","甲戌","乙亥","丙子","丁丑"},{"戊寅","已卯","庚辰","辛巳","壬午","癸未","甲申","乙酉","丙戌","丁亥","戊子","已丑"},{"庚寅","辛卯","壬辰","癸巳","甲午","乙未","丙申","丁酉","戊戌","已亥","庚子","辛丑"},{"壬寅","癸卯","甲辰","乙巳","丙午","丁未","戊申","已酉","庚戌","辛亥","壬子","癸丑"},{"甲寅","乙卯","丙辰","丁巳","戊午","已未","庚申","辛酉","壬戌","癸亥","甲子","乙丑"}
};
// 纪时
private static final String[][] JI_SHI = new String[][]{{"甲子","乙丑","丙寅","丁卯","戊辰","已巳","庚午","辛未","壬申","癸酉","甲戌","乙亥"},{"丙子","丁丑","戊寅","已卯","庚辰","辛巳","壬午","癸未","甲申","乙酉","丙戌","丁亥"},{"戊子","已丑","庚寅","辛卯","壬辰","癸巳","甲午","乙未","丙申","丁酉","戊戌","已亥"},{"庚子","辛丑","壬寅","癸卯","甲辰","乙巳","丙午","丁未","戊申","已酉","庚戌","辛亥"},{"壬子","癸丑","甲寅","乙卯","丙辰","丁巳","戊午","已未","庚申","辛酉","壬戌","癸亥"}
};
/*** 批八字* @param year 公历年* @param month 公历月* @param day 公历日* @param shiChen 时辰** @return String[4] 纪年,纪月,纪日,纪时* **/
public static String[] getBaZi(int year, int month, int day, ShiChen shiChen){String[] baZi = new String[4];Calendar calendar = Calendar.getInstance();calendar.set(Calendar.YEAR, year);calendar.set(Calendar.MONTH, month - 1);calendar.set(Calendar.DAY_OF_MONTH, day);calendar.set(Calendar.HOUR_OF_DAY, 1);if (shiChen == ShiChen.晚子){// 子时需要往后推移一天long time = calendar.getTimeInMillis() + 24*3600*1000;calendar.clear();calendar.setTimeInMillis(time);year = calendar.get(Calendar.YEAR);month = calendar.get(Calendar.MONTH) + 1;day = calendar.get(Calendar.DAY_OF_MONTH);}int[] lunarDate = getLunarDateFromDate(calendar.getTime());baZi[0] = getJiNian(lunarDate[0]);baZi[1] = getJiYue(lunarDate[0], Math.abs(lunarDate[1]));baZi[2] = getJiRi(year, month, day);baZi[3] = getJiShi(year, month, day, shiChen);return baZi;
}
/*** 批八字* @param lunarYear 农历年* @param lunarMonth 农历月* @param lunarDay 农历日* @param shiChen 时辰** @return String[4] 纪年,纪月,纪日,纪时* **/
public static String[] getBaZiByLunar(int lunarYear, int lunarMonth, int lunarDay, ShiChen shiChen){String[] baZi = new String[4];Date date = getDateFromLunarDate(lunarYear, lunarMonth, lunarDay);Calendar calendar = Calendar.getInstance();calendar.setTime(date);int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH) + 1;int day = calendar.get(Calendar.DAY_OF_MONTH);if (shiChen == ShiChen.晚子){// 子时需要往后推移一天long time = calendar.getTimeInMillis() + 24*3600*1000;calendar.clear();calendar.setTimeInMillis(time);year = calendar.get(Calendar.YEAR);month = calendar.get(Calendar.MONTH) + 1;day = calendar.get(Calendar.DAY_OF_MONTH);}int[] lunarDate = getLunarDateFromDate(date);baZi[0] = getJiNian(lunarDate[0]);baZi[1] = getJiYue(lunarDate[0], Math.abs(lunarDate[1]));baZi[2] = getJiRi(year, month, day);baZi[3] = getJiShi(year, month, day, shiChen);return baZi;
}
/*** 纪年** **/
public static String getJiNian(int lunarYear){return getYearTianGan(lunarYear).getDesc() + getYearDiZhi(lunarYear);
}/*** 获取纪月(纪月法)* @param lunarYear 农历年* @param lunarMonth 农历月** **/
public static String getJiYue(int lunarYear,@IntRange(from = 1, to = 12) int lunarMonth){TianGan tianGan = getYearTianGan(lunarYear);for (int i = 0; i< JI_YUE_TIAN_GAN.length; i++){String jytg = JI_YUE_TIAN_GAN[i];if (jytg.contains(tianGan.getDesc())){return JI_YUE[i][lunarMonth-1];}}return "";
}/*** 纪日** @param year 公历年* @param month 公历月* @param day 公历日** **/
public static String getJiRi(int year, int month, int day){return getDayTianGan(year, month, day).getDesc() + getDayDiZhi(year, month, day).getDesc();
}/*** 纪时* @param year 公历年* @param month 公历月* @param day 公历日* @param shiChen 时辰* **/
public static String getJiShi(int year, int month, int day, ShiChen shiChen){TianGan tianGan = getDayTianGan(year, month, day);for (int i =0 ; i < JI_YUE_TIAN_GAN.length ; i++){String jytg = JI_YUE_TIAN_GAN[i];if (jytg.contains(tianGan.getDesc())){if (shiChen == ShiChen.早子){return "早" + JI_SHI[i][0];}else if (shiChen == ShiChen.晚子){return "晚" + JI_SHI[i][0];}return JI_SHI[i][shiChen.getCode()];}}return "";
}
/*** 获取年 天干* @param lunarYear 农历年** **/
public static TianGan getYearTianGan(int lunarYear){String temp = String.format("%s",(lunarYear - 3));int tg = Integer.parseInt(temp.substring(temp.length()-1));if (tg == 0)tg =10;return TianGan.getByCode(tg);
}/*** 获取年 地支* @param lunarYear 农历年** **/
public static DiZhi getYearDiZhi(int lunarYear){int dz = (lunarYear -3)%12;if (dz == 0)dz = 12;return DiZhi.getByCode(dz);
}/*** 获取 日天干* @param year 公历年* @param month 公历月* @param day 公历日** **/
public static TianGan getDayTianGan(int year, int month, int day){int shiJi = year/100 + (year%100 > 0 ? 1 : 0);//世纪int yearYue = year%100;int tianGan = 4*(shiJi - 1) + (shiJi -1)/4 + 5* yearYue + yearYue/4 + (int)(3*(month + 1)/5f)+ day - 3;return TianGan.getByCode(tianGan%10);
}/*** 获取 日地支* @param year 公历年* @param month 公历月* @param day 公历日** **/
public static DiZhi getDayDiZhi(int year, int month, int day){int shiJi = year/100 + (year%100 > 0 ? 1 : 0);//世纪int yearYue = year%100;int diZhi = 8*(shiJi - 1) + (shiJi -1)/4 + 5* yearYue + yearYue/4 + (int)(3*(month + 1)/5f)+ day +7 + (month%2 == 0 ? 6 : 0);return DiZhi.getByCode(diZhi%12);
}

农历转换公历,请参考:

农历与公历日期转换(1900年 至 2049年)https://blog.csdn.net/sinde12/article/details/125392591?spm=1001.2014.3001.5502

年月日时天干地支推算(农历、公历)相关推荐

  1. 杨广悟道第一课:年月日时天干地支的计算方法

    一.背景说明: 张三说过:世界上最遥远的距离是什么?不是马里亚纳海沟和珠穆朗玛峰的距离,而是知道和做到的距离,是的,一直以来都想写一个系列的文章,介绍易学之术,但是怕写的不好,逻辑不够完美,技术不够精 ...

  2. python 获取节假日,二十四节气,中国农历,星次、每日凶煞、每日值神、农历建除十二神、农历每日宜忌、彭祖百忌、每日五行、二十八星宿、天干地支、农历生辰八字、时辰凶吉等

    Python 农历项目cnlunar的使用 文章目录 Python 农历项目cnlunar的使用 前言 一.cnlunar是什么? 二.使用步骤 1.安装pip 2.使用 总结 前言 1.不使用寿星通 ...

  3. SwiftUI 日期大全之日期日历年月日星期天干地支十二时辰时分秒(教程含源码)

    本文价值与收获 看完本文后,您将能够作出下面的界面 看完本文您将掌握的技能 使用Timer制作个clock 显示标准中文 显示农历天干地支十二时 解决方案 1.获取标准日期 //中文公历标准 //20 ...

  4. SwiftUI实战之日期日历年月日星期天干地支十二时辰时分秒

    本文价值与收获 看完本文后,您将能够作出下面的界面 Jietu20200409-210227@2x.jpg Jietu20200409-210626.gif 看完本文您将掌握的技能 使用Timer制作 ...

  5. 年月日时紫白推算表_年月日时飞星算法

    精 二十四山砂水诀细解 品 奇门点窍 课 抗疫精神"二十字"解读 堂 小畜卦-韬光养晦 年月日时飞星算法    只有计算出准确的年.月.日.时飞星,才能准确的测算出风水在时间上的变 ...

  6. java 从已知日期计算干支纪日_天干地支纪日纪时计算公式

    年:每个干支为一年,六十个干后,又从头算起,周而复始,循环不息.由甲子开始,满六十年称做一甲子或一花甲子.称为干支纪年法. 月:正月是由寅开始,每个月的地支固定不变,然后依次与天干组合:由第一年的正月 ...

  7. java 天干地支_Java-获取年月日对应的天干地支

    1 public classMyDate {2 /** 3 * 对于年月日的天干地支4 */ 5 private intyear_ganZhi;6 private intmonth_ganZhi;7 ...

  8. Java-获取年月日对应的天干地支

    一.概述 本次是以java语言开发为例,计算出年月日对应的天干地支. 二.代码 1 public class MyDate { 2 /** 3 * 对于年月日的天干地支 4 */ 5 private ...

  9. java 从已知日期计算干支纪日_天干地支纪日怎么计算?

    年:每个干支为一年,六十个干后,又从头算起,周而复始,循环不息.由甲子开始,满六十年称做一甲子或一花甲子.称为干支纪年法.月:正月是由寅开始,每个月的地支固定不变,然后依次与天干组合:由第一年的正月丙 ...

最新文章

  1. 集合框架(九)----Map
  2. 杨辉三角_二维数组的好例子(转载)
  3. 那些年,我们处理过的SQL问题
  4. 90后过年,吃零下18度的年夜饭
  5. 联想拯救者电竞手机新特性确认:有望搭载双X轴线性马达
  6. 【MySQL】MySQL监控工具 mysql-monitor
  7. 软件工程采取了哪些措施以保证最终能够交付给用户一个高质量、低成本的软件产品?
  8. Python Cookbook by Eric
  9. pthread_key_t和pthread_key_create()详解
  10. 上下文保存 中断_从操作系统(Windows)的角度讨论中断和异常机制
  11. 双系统扩展Linux内存
  12. CSS、Bulma介绍
  13. 《紫川》之远东战火 十二卷
  14. 【华为社招OD笔试题】输入一串数字,给出他们的最小的和的绝对值,输出a,b和a+b的绝对值 其中数字范围为[-63500,63500]
  15. C语言之打印错误信息
  16. Day16 GUI编程:贪吃蛇
  17. python 文件夹中的文件批量处理 高通道tif图片转换成jpg格式
  18. java/php/net/python学生社团管理系统设计
  19. kaggle中关于图像的比赛整理
  20. 【IP代理】吐槽某大爷IP

热门文章

  1. GIS地图学习笔记一之基础概念
  2. 按如下函数原型编程从键盘输入一个m行n列的二维数组,然后计算数组中元素的最大值及其所在的行列下标值。其中,m和n的值由用户键盘输入。已知m和n的值都不超过10。
  3. 泛目录站群,黑帽SEO程序,自由搭配任意泛,百科程序
  4. 密码学基础:Base64编码
  5. 软件开发综合实践实习小结
  6. 解决office,word奔溃的问题
  7. 使用esp 8266物联网开发板 + Mqtt制作远程控制LED小灯
  8. [R语言基础]——数据处理实例
  9. Windows PC上创建大数据职业技能竞赛实验环境之一--基本环境的搭建
  10. 第20节 应用HSRP协议布署双核心交换机网络——提高网络故障容错率