1
0
mirror of synced 2026-02-04 12:07:59 +08:00

Merge remote-tracking branch 'remotes/origin/v1.1.0-SNAPSHOT'

This commit is contained in:
6tail
2020-05-26 23:07:59 +08:00
4 changed files with 133 additions and 101 deletions

View File

@@ -332,8 +332,17 @@ public class Lunar{
* 干支纪时计算
*/
private void computeTime(){
timeZhiIndex = LunarUtil.getTimeZhiIndex((hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute);
timeGanIndex = timeZhiIndex%10;
String hm = (hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute;
timeZhiIndex = LunarUtil.getTimeZhiIndex(hm);
int dayGan = dayGanIndex;
// 晚子时(夜子/子夜)应算作第二天
if(hm.compareTo("23:00")>=0&&hm.compareTo("23:59")<=0){
dayGan++;
if(dayGan>=10){
dayGan -= 10;
}
}
timeGanIndex = (dayGan%5*2+timeZhiIndex)%10;
}
/**
@@ -681,7 +690,7 @@ public class Lunar{
}
/**
* 获取时辰干支(时柱)
* 获取时辰干支(时柱),支持早子时和晚子时
* @return 时辰干支(时柱)
*/
public String getTimeInGanZhi(){

View File

@@ -15,7 +15,7 @@ public class LunarTest {
public void test(){
Lunar date = new Lunar(2019,3,27,0,0,0);
Assert.assertEquals("贰零壹玖年叁月廿七",date.toString());
Assert.assertEquals("贰零壹玖年叁月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 海中金] 星期三 (七殿泰山王诞) 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]",date.toFullString());
Assert.assertEquals("贰零壹玖年叁月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 桑柘木] 星期三 (七殿泰山王诞) 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]",date.toFullString());
Assert.assertEquals("2019-05-01",date.getSolar().toString());
Assert.assertEquals("2019-05-01 00:00:00 星期三 (劳动节) 金牛座",date.getSolar().toFullString());
}

View File

@@ -17,7 +17,7 @@ public class SolarTest {
Assert.assertEquals("2019-05-01",date.toString());
Assert.assertEquals("2019-05-01 00:00:00 星期三 (劳动节) 金牛座",date.toFullString());
Assert.assertEquals("贰零壹玖年叁月廿七",date.getLunar().toString());
Assert.assertEquals("贰零壹玖年叁月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 海中金] 星期三 (七殿泰山王诞) 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]",date.getLunar().toFullString());
Assert.assertEquals("贰零壹玖年叁月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 桑柘木] 星期三 (七殿泰山王诞) 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]",date.getLunar().toFullString());
}
}

View File

@@ -6,6 +6,7 @@ import org.junit.Assert;
import org.junit.Test;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
@@ -15,7 +16,7 @@ import java.util.Map;
*/
public class TimeTest {
private static final Map<String,String> ZHI = new HashMap<String, String>(){
private static final Map<String,String> ZHI = new LinkedHashMap<String, String>(){
private static final long serialVersionUID = -1L;
{
put("23:00","");
@@ -76,129 +77,143 @@ public class TimeTest {
}
};
private static final Map<String,String> GAN = new HashMap<String, String>(){
private static final Map<String,String> GAN = new LinkedHashMap<String, String>(){
private static final long serialVersionUID = -1L;
{
put("23:00","");
put("00:59","");
put("23:30","");
//晚子时
put("2020-4,5,23:00","");
//早子时
put("2020-4,5,00:59","");
//晚子时
put("2020-4,5,23:30","");
put("01:00","");
put("02:59","");
put("01:30","");
put("2020-4,5,01:00","");
put("2020-4,5,02:59","");
put("2020-4,5,01:30","");
put("03:00","");
put("04:59","");
put("03:30","");
put("2020-4,5,03:00","");
put("2020-4,5,04:59","");
put("2020-4,5,03:30","");
put("05:00","");
put("06:59","");
put("05:30","");
put("2020-4,5,05:00","");
put("2020-4,5,06:59","");
put("2020-4,5,05:30","");
put("07:00","");
put("08:59","");
put("07:30","");
put("2020-4,5,07:00","");
put("2020-4,5,08:59","");
put("2020-4,5,07:30","");
put("09:00","");
put("10:59","");
put("09:30","");
put("2020-4,5,09:00","");
put("2020-4,5,10:59","");
put("2020-4,5,09:30","");
put("11:00","");
put("12:59","");
put("11:30","");
put("2020-4,5,11:00","");
put("2020-4,5,12:59","");
put("2020-4,5,11:30","");
put("13:00","");
put("14:59","");
put("13:30","");
put("2020-4,5,13:00","");
put("2020-4,5,14:59","");
put("2020-4,5,13:30","");
put("15:00","");
put("16:59","");
put("15:30","");
put("2020-4,5,15:00","");
put("2020-4,5,16:59","");
put("2020-4,5,15:30","");
put("17:00","");
put("18:59","");
put("17:30","");
put("2020-4,5,17:00","");
put("2020-4,5,18:59","");
put("2020-4,5,17:30","");
put("19:00","");
put("20:59","");
put("19:30","");
put("2020-4,5,19:00","");
put("2020-4,5,20:59","");
put("2020-4,5,19:30","");
put("21:00","");
put("22:59","");
put("21:30","");
put("2020-4,5,21:00","");
put("2020-4,5,22:59","");
put("2020-4,5,21:30","");
put(null,"");
put("2020-4,5,null","");
put("","");
put("23:01:01","");
put("其他","");
put("80:90","");
put("2020-4,5,","");
put("2020-4,5,23:01:01","");
put("2020-4,5,其他","");
put("2020-4,5,80:90","");
put("21:01:01","");
put("2020-4,5,21:01:01","");
put("2020-4,2,23:00","");
put("2020-4,2,11:20","");
}
};
private static final Map<String,String> GAN_ZHI = new HashMap<String, String>(){
private static final long serialVersionUID = -1L;
{
put("23:00","甲子");
put("00:59","");
put("23:30","甲子");
//晚子时
put("2020-4,5,23:00","");
//早子时
put("2020-4,5,00:59","丙子");
//晚子时
put("2020-4,5,23:30","戊子");
put("01:00","");
put("02:59","");
put("01:30","");
put("2020-4,5,01:00","");
put("2020-4,5,02:59","");
put("2020-4,5,01:30","");
put("03:00","");
put("04:59","");
put("03:30","");
put("2020-4,5,03:00","");
put("2020-4,5,04:59","");
put("2020-4,5,03:30","");
put("05:00","");
put("06:59","");
put("05:30","");
put("2020-4,5,05:00","");
put("2020-4,5,06:59","");
put("2020-4,5,05:30","");
put("07:00","");
put("08:59","");
put("07:30","");
put("2020-4,5,07:00","");
put("2020-4,5,08:59","");
put("2020-4,5,07:30","");
put("09:00","");
put("10:59","");
put("09:30","");
put("2020-4,5,09:00","");
put("2020-4,5,10:59","");
put("2020-4,5,09:30","");
put("11:00","");
put("12:59","");
put("11:30","");
put("2020-4,5,11:00","");
put("2020-4,5,12:59","");
put("2020-4,5,11:30","");
put("13:00","");
put("14:59","");
put("13:30","");
put("2020-4,5,13:00","");
put("2020-4,5,14:59","");
put("2020-4,5,13:30","");
put("15:00","");
put("16:59","");
put("15:30","");
put("2020-4,5,15:00","");
put("2020-4,5,16:59","");
put("2020-4,5,15:30","");
put("17:00","");
put("18:59","");
put("17:30","");
put("2020-4,5,17:00","");
put("2020-4,5,18:59","");
put("2020-4,5,17:30","");
put("19:00","");
put("20:59","");
put("19:30","");
put("2020-4,5,19:00","");
put("2020-4,5,20:59","");
put("2020-4,5,19:30","");
put("21:00","");
put("22:59","");
put("21:30","");
put("2020-4,5,21:00","");
put("2020-4,5,22:59","");
put("2020-4,5,21:30","");
put(null,"");
put("2020-4,5,null","");
put("","");
put("23:01:01","");
put("其他","");
put("80:90","");
put("2020-4,5,","");
put("2020-4,5,23:01:01","");
put("2020-4,5,其他","");
put("2020-4,5,80:90","");
put("20:21:01","");
put("21:01:01","");
put("01:21:01","");
put("2020-4,5,20:21:01","");
put("2020-4,5,21:01:01","");
put("2020-4,5,01:21:01","");
put("2020-4,2,23:00","壬子");
put("2020-4,2,11:20","丙午");
put("20204,28,23:20","甲子");
put("20204,29,00:20","甲子");
}
};
@@ -206,28 +221,36 @@ public class TimeTest {
public void testLunarTimeGanZhi(){
for(Map.Entry<String,String> entry:GAN_ZHI.entrySet()){
int hour = 0,minute = 0;
String hm = entry.getKey();
if(null!=hm&&hm.length()>=5){
String time = entry.getKey();
int year = Integer.parseInt(time.substring(0,4));
int month = Integer.parseInt(time.substring(4,time.indexOf(",")));
int day = Integer.parseInt(time.substring(time.indexOf(",")+1,time.lastIndexOf(",")));
String hm = time.substring(time.lastIndexOf(",")+1);
if(hm.length()>=5){
hour = Integer.parseInt(hm.substring(0,2),10);
minute = Integer.parseInt(hm.substring(3,5),10);
}
Lunar lunar = Lunar.fromYmdHms(2020,1,1,hour,minute,0);
Lunar lunar = Lunar.fromYmdHms(year,month,day,hour,minute,0);
String ganZhi = entry.getValue();
Assert.assertEquals(hm, ganZhi, lunar.getTimeInGanZhi());
Assert.assertEquals(lunar.getYear()+""+lunar.getMonthInChinese()+""+lunar.getDayInChinese()+" "+hm, ganZhi, lunar.getTimeInGanZhi());
}
}
@Test
public void testLunarTimeGan(){
for(Map.Entry<String,String> entry:GAN.entrySet()){
int hour = 0,minute = 0;
String hm = entry.getKey();
if(null!=hm&&hm.length()>=5){
String time = entry.getKey();
int year = Integer.parseInt(time.substring(0,4));
int month = Integer.parseInt(time.substring(4,time.indexOf(",")));
int day = Integer.parseInt(time.substring(time.indexOf(",")+1,time.lastIndexOf(",")));
String hm = time.substring(time.lastIndexOf(",")+1);
if(hm.length()>=5){
hour = Integer.parseInt(hm.substring(0,2),10);
minute = Integer.parseInt(hm.substring(3,5),10);
}
Lunar lunar = Lunar.fromYmdHms(2020,1,1,hour,minute,0);
Lunar lunar = Lunar.fromYmdHms(year,month,day,hour,minute,0);
String gan = entry.getValue();
Assert.assertEquals(hm, gan, lunar.getTimeGan());
Assert.assertEquals(lunar.getYear()+""+lunar.getMonthInChinese()+""+lunar.getDayInChinese()+" "+hm, gan, lunar.getTimeGan());
}
}