Merge branch 'v1.1.0-SNAPSHOT'
This commit is contained in:
@@ -4,6 +4,8 @@ import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.nlf.calendar.util.LunarUtil;
|
||||
import com.nlf.calendar.util.SolarUtil;
|
||||
|
||||
/**
|
||||
@@ -204,6 +206,76 @@ public class Solar{
|
||||
return new Solar(year,month,day,hour,minute,second);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过八字获取阳历列表
|
||||
* @param yearGanZhi 年柱
|
||||
* @param monthGanZhi 月柱
|
||||
* @param dayGanZhi 日柱
|
||||
* @param timeGanZhi 时柱
|
||||
* @return 符合的阳历列表
|
||||
*/
|
||||
public static List<Solar> fromBaZi(String yearGanZhi,String monthGanZhi,String dayGanZhi,String timeGanZhi){
|
||||
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;
|
||||
}
|
||||
int startYear = today.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)){
|
||||
hour = (i-1)*2;
|
||||
}
|
||||
}
|
||||
int year = startYear-1;
|
||||
while(year>=SolarUtil.BASE_YEAR) {
|
||||
int counter = 0;
|
||||
int month = 12;
|
||||
int day;
|
||||
boolean found = false;
|
||||
while (counter < 15) {
|
||||
day = 1;
|
||||
if(year==SolarUtil.BASE_YEAR){
|
||||
if(month<SolarUtil.BASE_MONTH){
|
||||
continue;
|
||||
}else if(month==SolarUtil.BASE_MONTH){
|
||||
day = SolarUtil.BASE_DAY;
|
||||
}
|
||||
}
|
||||
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;
|
||||
Solar solar = new Solar(year, month-1, 1, hour, 0, 0);
|
||||
while (counter < 61) {
|
||||
lunar = solar.getLunar();
|
||||
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && lunar.getDayInGanZhiExact().equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) {
|
||||
l.add(solar);
|
||||
break;
|
||||
}
|
||||
solar = solar.next(1);
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
year -= 61;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否闰年
|
||||
*
|
||||
|
||||
@@ -55,7 +55,7 @@ public class SolarUtil{
|
||||
private static final long serialVersionUID = -1;
|
||||
{
|
||||
put("1-8",Collections.nCopies(1,"周恩来逝世纪念日"));
|
||||
put("1-10",Collections.nCopies(1,"中国公安110宣传日"));
|
||||
put("1-10",Arrays.asList("中国人民警察节","中国公安110宣传日"));
|
||||
put("1-21",Collections.nCopies(1,"列宁逝世纪念日"));
|
||||
put("1-26",Collections.nCopies(1,"国际海关日"));
|
||||
put("2-2",Collections.nCopies(1,"世界湿地日"));
|
||||
|
||||
@@ -2,9 +2,10 @@ package sample;
|
||||
|
||||
import com.nlf.calendar.Lunar;
|
||||
import com.nlf.calendar.Solar;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 八字测试
|
||||
*/
|
||||
@@ -58,6 +59,15 @@ public class BaZiTest {
|
||||
System.out.println(lunar.getBaZi());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBazi2Solar6() {
|
||||
List<Solar> l = Solar.fromBaZi("庚子", "戊寅", "戊寅", "癸亥");
|
||||
// [2020-02-05 22:00:00, 1960-02-20 22:00:00]
|
||||
for (Solar solar : l) {
|
||||
System.out.println(solar.toFullString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test7(){
|
||||
Solar solar = new Solar(2020,5,26,23,43,0);
|
||||
@@ -66,4 +76,22 @@ public class BaZiTest {
|
||||
System.out.println(lunar.getBaZi());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBazi2Solar7() {
|
||||
List<Solar> l = Solar.fromBaZi("庚子", "辛巳", "庚午", "丙子");
|
||||
// [2020-07-21 22:00:00, 1960-08-05 22:00:00]
|
||||
for (Solar solar : l) {
|
||||
System.out.println(solar.toFullString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBazi2Solar() {
|
||||
List<Solar> l = Solar.fromBaZi("庚子", "癸未", "乙丑", "丁亥");
|
||||
// [2020-05-26 23:00:00, 2020-05-27 00:00:00]
|
||||
for (Solar solar : l) {
|
||||
System.out.println(solar.toFullString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user