📄 lunar.java
字号:
int cyclicalMonth = 0;
int cyclicalDay = 0;
// 干支年 1900年立春後为庚子年(60进制36)
int term2 = Lunar.getSolarTermDay(solarYear, 2); // 立春日期
// 依节气调整二月分的年柱, 以立春为界
if (solarMonth < 1 || (solarMonth == 1 && solarDay < term2)) {
cyclicalYear = (solarYear - 1900 + 36 - 1) % 60;
} else {
cyclicalYear = (solarYear - 1900 + 36) % 60;
}
// 干支月 1900年1月小寒以前为 丙子月(60进制12)
int firstNode = Lunar.getSolarTermDay(solarYear, solarMonth * 2); // 传回当月「节」为几日开始
// 依节气月柱, 以「节」为界
if (solarDay < firstNode) {
cyclicalMonth = ((solarYear - 1900) * 12 + solarMonth + 12) % 60;
} else {
cyclicalMonth = ((solarYear - 1900) * 12 + solarMonth + 13) % 60;
}
// 当月一日与 1900/1/1 相差天数
// 1900/1/1与 1970/1/1 相差25567日, 1900/1/1 日柱为甲戌日(60进制10)
cyclicalDay = (int) (Lunar.UTC(solarYear, solarMonth, solarDay, 0, 0, 0) / 86400000 + 25567 + 10) % 60;
this.cyclicalYear = cyclicalYear;
this.cyclicalMonth = cyclicalMonth;
this.cyclicalDay = cyclicalDay;
}
/**
* 取农历年生肖
* @return 农历年生肖(例:龙)
*/
public String getAnimalString() {
return Lunar.Animals[(this.lunarYear - 4) % 12];
}
/**
* 返回公历日期的节气字符串
* @return 二十四节气字符串,若不是节气日,返回空串(例:冬至)
*/
public String getTermString() {
// 二十四节气
String termString = "";
if (Lunar.getSolarTermDay(solarYear, solarMonth * 2) == solarDay) {
termString = Lunar.solarTerm[solarMonth * 2];
} else if (Lunar.getSolarTermDay(solarYear, solarMonth * 2 + 1) == solarDay) {
termString = Lunar.solarTerm[solarMonth * 2 + 1];
}
return termString;
}
/**
* 取得干支历字符串
*
* @return 干支历字符串(例:甲子年甲子月甲子日)
*/
public String getCyclicalDateString() {
return this.getCyclicaYear() + "年" + this.getCyclicaMonth() + "月"
+ this.getCyclicaDay() + "日";
}
/**
* 年份天干
* @return 年份天干
*/
public int getTiananY() {
return Lunar.getTianan(this.cyclicalYear);
}
/**
* 月份天干
* @return 月份天干
*/
public int getTiananM() {
return Lunar.getTianan(this.cyclicalMonth);
}
/**
* 日期天干
* @return 日期天干
*/
public int getTiananD() {
return Lunar.getTianan(this.cyclicalDay);
}
/**
* 年份地支
* @return 年分地支
*/
public int getDeqiY() {
return Lunar.getDeqi(this.cyclicalYear);
}
/**
* 月份地支
* @return 月份地支
*/
public int getDeqiM() {
return Lunar.getDeqi(this.cyclicalMonth);
}
/**
* 日期地支
* @return 日期地支
*/
public int getDeqiD() {
return Lunar.getDeqi(this.cyclicalDay);
}
/**
* 取得干支年字符串
* @return 干支年字符串
*/
public String getCyclicaYear() {
return Lunar.getCyclicalString(this.cyclicalYear);
}
/**
* 取得干支月字符串
* @return 干支月字符串
*/
public String getCyclicaMonth() {
return Lunar.getCyclicalString(this.cyclicalMonth);
}
/**
* 取得干支日字符串
* @return 干支日字符串
*/
public String getCyclicaDay() {
return Lunar.getCyclicalString(this.cyclicalDay);
}
/**
* 返回农历日期字符串
* @return 农历日期字符串
*/
public String getLunarDayString() {
return Lunar.getLunarDayString(this.lunarDay);
}
/**
* 返回农历日期字符串
* @return 农历日期字符串
*/
public String getLunarMonthString() {
return (this.isLeap() ? "闰" : "") + Lunar.getLunarMonthString(this.lunarMonth);
}
/**
* 返回农历日期字符串
* @return 农历日期字符串
*/
public String getLunarYearString() {
return Lunar.getLunarYearString(this.lunarYear);
}
/**
* 返回农历表示字符串
* @return 农历字符串(例:甲子年正月初三)
*/
public String getLunarDateString() {
return this.getLunarYearString() + "年"
+ this.getLunarMonthString() + "月"
+ this.getLunarDayString() + "日";
}
/**
* 农历年是否是闰月
* @return 农历年是否是闰月
*/
public boolean isLeap() {
return isLeap;
}
/**
* 农历年是否是闰年
* @return 农历年是否是闰年
*/
public boolean isLeapYear() {
return isLeapYear;
}
/**
* 当前农历月是否是大月
* @return 当前农历月是大月
*/
public boolean isBigMonth() {
return this.getMaxDayInMonth()>29;
}
/**
* 当前农历月有多少天
* @return 当前农历月有多少天
*/
public int getMaxDayInMonth() {
return this.maxDayInMonth;
}
/**
* 农历日期
* @return 农历日期
*/
public int getLunarDay() {
return lunarDay;
}
/**
* 农历月份
* @return 农历月份
*/
public int getLunarMonth() {
return lunarMonth;
}
/**
* 农历年份
* @return 农历年份
*/
public int getLunarYear() {
return lunarYear;
}
/**
* 公历日期
* @return 公历日期
*/
public int getSolarDay() {
return solarDay;
}
/**
* 公历月份
* @return 公历月份 (不是从0算起)
*/
public int getSolarMonth() {
return solarMonth+1;
}
/**
* 公历年份
* @return 公历年份
*/
public int getSolarYear() {
return solarYear;
}
/**
* 星期几
* @return 星期几(星期日为:1, 星期六为:7)
*/
public int getDayOfWeek() {
return this.solar.get(Calendar.DAY_OF_WEEK);
}
/**
* 黑色星期五
* @return 是否黑色星期五
*/
public boolean isBlackFriday() {
return (this.getSolarDay() == 13 && this.solar.get(Calendar.DAY_OF_WEEK)==6);
}
/**
* 是否是今日
* @return 是否是今日
*/
public boolean isToday() {
Calendar clr = Calendar.getInstance();
return clr.get(Calendar.YEAR)==this.solarYear &&
clr.get(Calendar.MONTH)==this.solarMonth &&
clr.get(Calendar.DAY_OF_MONTH)==this.solarDay;
}
/**
* 取得公历节日名称
* @return 公历节日名称,如果不是节日返回空串
*/
public String getSFestivalName() {
return this.sFestivalName;
}
/**
* 取得农历节日名称
* @return 农历节日名称,如果不是节日返回空串
*/
public String getLFestivalName() {
return this.lFestivalName;
}
/**
* 是否是农历节日
* @return 是否是农历节日
*/
public boolean isLFestival() {
if (!this.isFinded) this.findFestival();
return this.isLFestival;
}
/**
* 是否是公历节日
* @return 是否是公历节日
*/
public boolean isSFestival() {
if (!this.isFinded) this.findFestival();
return this.isSFestival;
}
/**
* 是否是节日
* @return 是否是节日
*/
public boolean isFestival() {
return this.isSFestival() || this.isLFestival();
}
/**
* 是否是放假日
* @return 是否是放假日
*/
public boolean isHoliday() {
if (!this.isFinded) this.findFestival();
return this.isHoliday;
}
/**
* 其它日期说明
* @return 日期说明(如:民国2年)
*/
public String getDescription() {
if (!this.isFinded) this.findFestival();
return this.description;
}
/**
* 干支字符串
* @param cyclicalNumber 指定干支位置(数字,0为甲子)
* @return 干支字符串
*/
private static String getCyclicalString(int cyclicalNumber) {
return Lunar.Tianan[Lunar.getTianan(cyclicalNumber)] + Lunar.Deqi[Lunar.getDeqi(cyclicalNumber)];
}
/**
* 获得地支
* @param cyclicalNumber
* @return 地支 (数字)
*/
private static int getDeqi(int cyclicalNumber) {
return cyclicalNumber % 12;
}
/**
* 获得天干
* @param cyclicalNumber
* @return 天干 (数字)
*/
private static int getTianan(int cyclicalNumber) {
return cyclicalNumber % 10;
}
/**
* 返回指定数字的农历年份表示字符串
* @param lunarYear 农历年份(数字,0为甲子)
* @return 农历年份字符串
*/
private static String getLunarYearString(int lunarYear) {
return Lunar.getCyclicalString(lunarYear - 1900 + 36);
}
/**
* 返回指定数字的农历月份表示字符串
* @param lunarMonth 农历月份(数字)
* @return 农历月份字符串 (例:正)
*/
private static String getLunarMonthString(int lunarMonth) {
String lunarMonthString = "";
if (lunarMonth == 1) {
lunarMonthString = Lunar.lunarString2[4];
} else {
if (lunarMonth > 9)
lunarMonthString += Lunar.lunarString2[1];
if (lunarMonth % 10 > 0)
lunarMonthString += Lunar.lunarString1[lunarMonth % 10];
}
return lunarMonthString;
}
/**
* 返回指定数字的农历日表示字符串
* @param lunarDay 农历日(数字)
* @return 农历日字符串 (例: 廿一)
*/
private static String getLunarDayString(int lunarDay) {
if (lunarDay<1 || lunarDay>30) return "";
int i1 = lunarDay / 10;
int i2 = lunarDay % 10;
String c1 = Lunar.lunarString2[i1];
String c2 = Lunar.lunarString1[i2];
if (lunarDay < 11) c1 = Lunar.lunarString2[0];
if (i2 == 0) c2 = Lunar.lunarString2[1];
return c1 + c2;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -