v1.2.29 修复1582年总天数问题;修复八字转阳历遗漏问题。
This commit is contained in:
@@ -52,27 +52,7 @@ public class ExactDate {
|
||||
* @return 天数
|
||||
*/
|
||||
public static int getDaysBetween(int ay, int am, int ad, int by, int bm, int bd) {
|
||||
int n;
|
||||
int days;
|
||||
int i;
|
||||
if (ay == by) {
|
||||
n = SolarUtil.getDaysInYear(by, bm, bd) - SolarUtil.getDaysInYear(ay, am, ad);
|
||||
} else if (ay > by) {
|
||||
days = SolarUtil.getDaysOfYear(by) - SolarUtil.getDaysInYear(by, bm, bd);
|
||||
for (i = by + 1; i < ay; i++) {
|
||||
days += SolarUtil.getDaysOfYear(i);
|
||||
}
|
||||
days += SolarUtil.getDaysInYear(ay, am, ad);
|
||||
n = -days;
|
||||
} else {
|
||||
days = SolarUtil.getDaysOfYear(ay) - SolarUtil.getDaysInYear(ay, am, ad);
|
||||
for (i = ay + 1; i < by; i++) {
|
||||
days += SolarUtil.getDaysOfYear(i);
|
||||
}
|
||||
days += SolarUtil.getDaysInYear(by, bm, bd);
|
||||
n = days;
|
||||
}
|
||||
return n;
|
||||
return SolarUtil.getDaysBetween(ay, am, ad, by, bm, bd);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,6 +63,6 @@ public class ExactDate {
|
||||
* @return 天数
|
||||
*/
|
||||
public static int getDaysBetween(Calendar calendar0, Calendar calendar1) {
|
||||
return getDaysBetween(calendar0.get(Calendar.YEAR), calendar0.get(Calendar.MONTH) + 1, calendar0.get(Calendar.DATE), calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH) + 1, calendar1.get(Calendar.DATE));
|
||||
return SolarUtil.getDaysBetween(calendar0.get(Calendar.YEAR), calendar0.get(Calendar.MONTH) + 1, calendar0.get(Calendar.DATE), calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH) + 1, calendar1.get(Calendar.DATE));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,64 +277,38 @@ public class Solar {
|
||||
public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String dayGanZhi, String timeGanZhi, int sect, int baseYear) {
|
||||
sect = (1 == sect) ? 1 : 2;
|
||||
List<Solar> l = new ArrayList<Solar>();
|
||||
Solar today = new Solar();
|
||||
Lunar lunar = today.getLunar();
|
||||
int offsetYear = LunarUtil.getJiaZiIndex(lunar.getYearInGanZhiExact()) - LunarUtil.getJiaZiIndex(yearGanZhi);
|
||||
if (offsetYear < 0) {
|
||||
offsetYear = offsetYear + 60;
|
||||
List<Integer> years = new ArrayList<Integer>();
|
||||
Solar today = fromDate(new Date());
|
||||
int offsetYear = LunarUtil.getJiaZiIndex(today.getLunar().getYearInGanZhiExact())-LunarUtil.getJiaZiIndex(yearGanZhi);
|
||||
if(offsetYear < 0){
|
||||
offsetYear += 60;
|
||||
}
|
||||
int startYear = today.getYear() - offsetYear - 1;
|
||||
while (startYear >= baseYear) {
|
||||
years.add(startYear);
|
||||
startYear -= 60;
|
||||
}
|
||||
int startYear = lunar.getYear() - offsetYear;
|
||||
int hour = 0;
|
||||
String timeZhi = timeGanZhi.substring(1);
|
||||
for (int i = 0, j = LunarUtil.ZHI.length; i < j; i++) {
|
||||
if (LunarUtil.ZHI[i].equals(timeZhi)) {
|
||||
for(int i = 0, j = LunarUtil.ZHI.length; i < j; i++){
|
||||
if(LunarUtil.ZHI[i].equals(timeZhi)){
|
||||
hour = (i - 1) * 2;
|
||||
}
|
||||
}
|
||||
while (startYear >= baseYear) {
|
||||
int year = startYear - 1;
|
||||
int counter = 0;
|
||||
int month = 12;
|
||||
int day;
|
||||
boolean found = false;
|
||||
while (counter < 15) {
|
||||
if (year >= baseYear) {
|
||||
day = 1;
|
||||
Solar solar = new Solar(year, month, day, hour, 0, 0);
|
||||
lunar = solar.getLunar();
|
||||
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
month++;
|
||||
if (month > 12) {
|
||||
month = 1;
|
||||
year++;
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
if (found) {
|
||||
counter = 0;
|
||||
month--;
|
||||
if (month < 1) {
|
||||
month = 12;
|
||||
year--;
|
||||
}
|
||||
day = 1;
|
||||
Solar solar = new Solar(year, month, day, hour, 0, 0);
|
||||
while (counter < 61) {
|
||||
lunar = solar.getLunar();
|
||||
for (Integer integer : years) {
|
||||
inner: for (int x = 0; x < 3; x++) {
|
||||
int year = integer + x;
|
||||
Solar solar = fromYmdHms(year, 1, 1, hour, 0, 0);
|
||||
while (solar.getYear() == year) {
|
||||
Lunar lunar = solar.getLunar();
|
||||
String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
|
||||
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && dgz.equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) {
|
||||
l.add(solar);
|
||||
break;
|
||||
break inner;
|
||||
}
|
||||
solar = solar.next(1);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
startYear -= 60;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
@@ -157,6 +157,9 @@ public class SolarUtil {
|
||||
* @return 天数
|
||||
*/
|
||||
public static int getDaysOfYear(int year) {
|
||||
if (1582 == year) {
|
||||
return 355;
|
||||
}
|
||||
return isLeapYear(year) ? 366 : 365;
|
||||
}
|
||||
|
||||
@@ -193,10 +196,15 @@ public class SolarUtil {
|
||||
for (int i = 1; i < month; i++) {
|
||||
days += getDaysOfMonth(year, i);
|
||||
}
|
||||
days += day;
|
||||
if (1582 == year && 10 == month && day >= 15) {
|
||||
days -= 10;
|
||||
int d = day;
|
||||
if (1582 == year && 10 == month) {
|
||||
if (day >= 15) {
|
||||
d -= 10;
|
||||
} else if (day > 4) {
|
||||
throw new IllegalArgumentException(String.format("wrong solar year %d month %d day %d", year, month, day));
|
||||
}
|
||||
}
|
||||
days += d;
|
||||
return days;
|
||||
}
|
||||
|
||||
@@ -213,4 +221,64 @@ public class SolarUtil {
|
||||
int week = ExactDate.fromYmd(year, month, 1).get(Calendar.DAY_OF_WEEK) - 1;
|
||||
return (int) Math.ceil((days + week - start) * 1D / WEEK.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取两个日期之间相差的天数(如果日期a比日期b小,天数为正,如果日期a比日期b大,天数为负)
|
||||
*
|
||||
* @param ay 年a
|
||||
* @param am 月a
|
||||
* @param ad 日a
|
||||
* @param by 年b
|
||||
* @param bm 月b
|
||||
* @param bd 日b
|
||||
* @return 天数
|
||||
*/
|
||||
public static int getDaysBetween(int ay, int am, int ad, int by, int bm, int bd) {
|
||||
int n;
|
||||
int days;
|
||||
int i;
|
||||
if (ay == by) {
|
||||
n = getDaysInYear(by, bm, bd) - getDaysInYear(ay, am, ad);
|
||||
} else if (ay > by) {
|
||||
days = getDaysOfYear(by) - getDaysInYear(by, bm, bd);
|
||||
for (i = by + 1; i < ay; i++) {
|
||||
days += getDaysOfYear(i);
|
||||
}
|
||||
days += getDaysInYear(ay, am, ad);
|
||||
n = -days;
|
||||
} else {
|
||||
days = getDaysOfYear(ay) - getDaysInYear(ay, am, ad);
|
||||
for (i = ay + 1; i < by; i++) {
|
||||
days += getDaysOfYear(i);
|
||||
}
|
||||
days += getDaysInYear(by, bm, bd);
|
||||
n = days;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
public static int getWeek(int y, int m, int d) {
|
||||
if (1582 == y && 10 == m) {
|
||||
if (d > 4 && d < 15) {
|
||||
throw new IllegalArgumentException(String.format("wrong solar year %d month %d day %d", y, m, d));
|
||||
}
|
||||
}
|
||||
Calendar start = ExactDate.fromYmd(1582, 10, 15);
|
||||
Calendar current = ExactDate.fromYmd(y, m, d);
|
||||
// 蔡勒公式
|
||||
if (m < 3) {
|
||||
m += 12;
|
||||
y--;
|
||||
}
|
||||
int c = y/100;
|
||||
y = y - c * 100;
|
||||
int w;
|
||||
int x = y + y / 4 + c / 4 - 2 * c;
|
||||
if (current.before(start)) {
|
||||
w = (x + (13*(m+1))/5 + d + 2) % 7;
|
||||
} else {
|
||||
w = (x + (26*(m+1))/10 + d - 1) % 7;
|
||||
}
|
||||
return (w + 7) % 7;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user