Commit d6fa8033 by zhangdongwei-intern

fix: 月份的干支有误

parent 63f40978
......@@ -4,6 +4,7 @@ import com.pcloud.common.utils.string.StringUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
......@@ -244,6 +245,11 @@ public class LunarCalendarUtils {
daysOfMonth = daysInLunarMonth(iYear, iMonth);
offset -= daysOfMonth;
}
// offset小于0时,也要校正
if (offset < 0) {
offset += daysOfMonth;
--iMonth;
}
// 当前月超过闰月,要校正
if (leapMonth != 0 && iMonth > leapMonth) {
--iMonth;
......@@ -251,11 +257,6 @@ public class LunarCalendarUtils {
isLeap = true;
}
}
// offset小于0时,也要校正
if (offset < 0) {
offset += daysOfMonth;
--iMonth;
}
lunarDate[1] = iMonth;
lunarDate[2] = offset + 1;
lunarDate[3] = isLeap ? 1 : 0;
......@@ -373,11 +374,20 @@ public class LunarCalendarUtils {
* 传入年份,返回干支年
* 公元元年是辛酉年,干的周期是10,支的周期是12
* @param year
* @param month
* @param day
* @return
*/
private static String ganZhiYear(int year){
private static String ganZhiYear(int year, int month, int day){
int gan = year % 10 > 3 ? (year % 10 - 3) : (year % 10 - 3 + 10);
int zhi = year % 12 > 3 ? (year % 12 - 3) : (year % 12 - 3 + 12);
//计算该年的正月初一是几月几日
int[] date = lunarToSolar(year, 1, 1, false);
if (month < date[1] || (month == date[1] && day < date[2])){
//如果当日在今年正月初一之前,干支都要减一
gan = gan == 1 ? 10 : (gan - 1);
zhi = zhi == 1 ? 12 : (zhi - 1);
}
return GAN[gan - 1] + ZHI[zhi - 1] + "年";
}
......@@ -385,26 +395,29 @@ public class LunarCalendarUtils {
* 传入年月日和农历年月,返回干支月
* 支的周期是12, 正好对应12个月
* 干的周期是10,每一年的一月的“干”间隔2(即只能是:甲丙戊庚壬),每年一月的周期是5,
* 1990年1月是丁丑月
* @param month
* @return
*/
private static String ganZhiMonth(int year, int month, int day, int lunarYear, int lunarMonth){
private static String ganZhiMonth(int year, int month, int day){
//计算该月与1900年1月相差的月数
int monthNum = (year - 1900 ) * 12 + month;
//该月第一个节气是一年里的第几个节气
int num = month * 2 - 2;
//计算该节气在几日
int d = sTerm(year, num);
int m = lunarMonth;
int y = lunarYear;
if (day < d){
//如果今天在本月第一个节气前 农历月份取上一月
y = lunarMonth == 1 ? lunarYear - 1 : lunarYear;
m = lunarMonth == 1 ? 12 : lunarMonth - 1;
}
int ganYear = y % 10 > 3 ? (y % 10 - 3) : (y % 10 - 3 + 10);
int gan = (ganYear * 2 + m) % 10;
gan = gan == 0 ? 10 : gan;
int zhi = (m + 1)%12;
return GAN[gan-1] + ZHI[zhi] + "月";
int d1 = sTerm(year, num);
if (day < d1){
//如果当天在该月的第一个节气前,月数减一
monthNum -= 1;
}
//计算干支数
monthNum %= 60;
//由于1900年1月是丁丑月,所以干支数需要加13
int ganZhi = monthNum + 13;
ganZhi = ganZhi > 60 ? ganZhi-60 : ganZhi;
int gan = ganZhi % 10 == 0 ? ganZhi % 10 + 10 : ganZhi % 10;
int zhi = ganZhi % 12 == 0 ? ganZhi % 12 + 12 : ganZhi % 12;
return GAN[gan-1] + ZHI[zhi-1] + "月";
}
/**
......@@ -416,6 +429,7 @@ public class LunarCalendarUtils {
* @return
*/
private static String ganZhiDay(int year, int month, int day){
//先计算当天与1900年元旦相差的天数
int ganZhi = 0;
int num = 0;
for (int i = 1900; i < year ; i ++ ){
......@@ -432,7 +446,9 @@ public class LunarCalendarUtils {
num += 1;
}
num += day;
//计算干支数
num %= 60;
//由于1900年元旦是甲戌日,所以干支数需要加10
ganZhi = num + 10;
ganZhi = ganZhi > 60 ? ganZhi-60 : ganZhi;
int gan = ganZhi % 10 == 0 ? ganZhi % 10 + 10 : ganZhi % 10;
......@@ -466,26 +482,30 @@ public class LunarCalendarUtils {
}
lunar += CHINESE_NUM[result[2] % 10 == 0 ? 10 : result[2] % 10] + "日";
strMap.put("lunar", lunar);
String ganZhi = ganZhiYear(year) + " " + ganZhiMonth(year, month, day, result[0], result[1]) + " " + ganZhiDay(year, month, day);
String ganZhi = ganZhiYear(year, month, day) + " " + ganZhiMonth(year, month, day) + " " + ganZhiDay(year, month, day);
strMap.put("ganZhi", ganZhi);
return strMap;
}
/*public static void main(String[] args){
public static void main(String[] args){
SimpleDateFormat sdf =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss" );
String str = "2023/2/1 00:00:00";
String str = "2019/02/01 00:00:00";
try {
Date d = sdf.parse(str);
*//*Calendar calendar = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
System.out.println(calendar.get(Calendar.MONTH) + 1);*//*
System.out.println(calendar.get(Calendar.MONTH) + 1);
System.out.printf("字符串 %s 通过格式 yyyy/MM/dd HH:mm:ss %n转换为日期对象: %s\n",str,d.toString());
//System.out.println(leapMonth(calendar.get(Calendar.YEAR)));
System.out.println(dataToLunar(d));
/*for (int year = 1900; year < 2099; year ++) {
System.out.println(Arrays.toString(lunarToSolar(year, 1, 1, false)));
}*/
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment