Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc2bbb5049 | ||
|
|
9bb808da35 | ||
|
|
e29ab49618 | ||
|
|
60634d7cd8 | ||
|
|
fba9a85135 | ||
|
|
913e7b40fb | ||
|
|
c7738352eb |
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
|
||||
<dependency>
|
||||
<groupId>cn.6tail</groupId>
|
||||
<artifactId>lunar</artifactId>
|
||||
<version>1.1.3</version>
|
||||
<version>1.1.5</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
|
||||
<dependency>
|
||||
<groupId>cn.6tail</groupId>
|
||||
<artifactId>lunar</artifactId>
|
||||
<version>1.1.3</version>
|
||||
<version>1.1.5</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
||||
4
pom.xml
4
pom.xml
@@ -7,7 +7,7 @@
|
||||
<groupId>cn.6tail</groupId>
|
||||
<artifactId>lunar</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.1.3</version>
|
||||
<version>1.1.5</version>
|
||||
<name>${project.groupId}:${project.artifactId}</name>
|
||||
<url>https://github.com/6tail/lunar-java</url>
|
||||
<description>a calendar library for Solar and Chinese Lunar</description>
|
||||
@@ -60,7 +60,7 @@
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.nlf.calendar;
|
||||
|
||||
import com.nlf.calendar.util.LunarUtil;
|
||||
|
||||
/**
|
||||
* 节气
|
||||
*
|
||||
@@ -13,11 +15,22 @@ public class JieQi {
|
||||
/** 阳历日期 */
|
||||
private Solar solar;
|
||||
|
||||
/** 是否节令 */
|
||||
private boolean jie;
|
||||
|
||||
/** 是否气令 */
|
||||
private boolean qi;
|
||||
|
||||
public JieQi() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param name 名称
|
||||
* @param solar 阳历日期
|
||||
*/
|
||||
public JieQi(String name, Solar solar) {
|
||||
this.name = name;
|
||||
setName(name);
|
||||
this.solar = solar;
|
||||
}
|
||||
|
||||
@@ -35,6 +48,18 @@ public class JieQi {
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
for(String key: LunarUtil.JIE){
|
||||
if(key.equals(name)){
|
||||
this.jie = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
for(String key: LunarUtil.QI){
|
||||
if(key.equals(name)){
|
||||
this.qi = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,4 +78,24 @@ public class JieQi {
|
||||
this.solar = solar;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否节令
|
||||
* @return true/false
|
||||
*/
|
||||
public boolean isJie(){
|
||||
return jie;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否气令
|
||||
* @return true/false
|
||||
*/
|
||||
public boolean isQi() {
|
||||
return qi;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -414,6 +414,7 @@ public class Lunar{
|
||||
* 计算干支纪年
|
||||
*/
|
||||
private void computeYear(){
|
||||
//以正月初一开始
|
||||
yearGanIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%10;
|
||||
yearZhiIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%12;
|
||||
|
||||
@@ -425,32 +426,57 @@ public class Lunar{
|
||||
int gExact = yearGanIndex;
|
||||
int zExact = yearZhiIndex;
|
||||
|
||||
//获取立春的阳历时刻
|
||||
Solar liChun = jieQi.get("立春");
|
||||
|
||||
//阳历和阴历年份相同代表正月初一及以后
|
||||
if(year==solar.getYear()){
|
||||
//获取立春的阳历时刻
|
||||
Solar liChun = jieQi.get("立春");
|
||||
//立春日期判断
|
||||
if(solar.toYmd().compareTo(liChun.toYmd())<0) {
|
||||
g--;
|
||||
if(g<0){
|
||||
g += 10;
|
||||
}
|
||||
z--;
|
||||
if(z<0){
|
||||
z += 12;
|
||||
}
|
||||
}
|
||||
//立春交接时刻判断
|
||||
if(solar.toYmdHms().compareTo(liChun.toYmdHms())<0) {
|
||||
gExact--;
|
||||
if(gExact<0){
|
||||
gExact += 10;
|
||||
}
|
||||
zExact--;
|
||||
if(zExact<0){
|
||||
zExact += 12;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
if(solar.toYmd().compareTo(liChun.toYmd())>=0) {
|
||||
g++;
|
||||
z++;
|
||||
}
|
||||
if(solar.toYmdHms().compareTo(liChun.toYmdHms())>=0) {
|
||||
gExact++;
|
||||
zExact++;
|
||||
}
|
||||
}
|
||||
|
||||
if(g<0){
|
||||
g += 10;
|
||||
}
|
||||
if(g>=10){
|
||||
g-=10;
|
||||
}
|
||||
if(z<0){
|
||||
z += 12;
|
||||
}
|
||||
if(z>=12){
|
||||
z-=12;
|
||||
}
|
||||
if(gExact<0){
|
||||
gExact += 10;
|
||||
}
|
||||
if(gExact>=10){
|
||||
gExact-=10;
|
||||
}
|
||||
if(zExact<0){
|
||||
zExact += 12;
|
||||
}
|
||||
if(zExact>=12){
|
||||
zExact-=12;
|
||||
}
|
||||
|
||||
yearGanIndexByLiChun = g;
|
||||
yearZhiIndexByLiChun = z;
|
||||
|
||||
@@ -1949,6 +1975,22 @@ public class Lunar{
|
||||
return getNearJieQi(false,LunarUtil.JIE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一气令(顺推的第一个气令)
|
||||
* @return 节气
|
||||
*/
|
||||
public JieQi getNextQi(){
|
||||
return getNearJieQi(true,LunarUtil.QI);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上一气令(逆推的第一个气令)
|
||||
* @return 节气
|
||||
*/
|
||||
public JieQi getPrevQi(){
|
||||
return getNearJieQi(false,LunarUtil.QI);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一节气(顺推的第一个节气)
|
||||
* @return 节气
|
||||
@@ -2019,6 +2061,54 @@ public class Lunar{
|
||||
return new JieQi(name, near);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取节气名称,如果无节气,返回空字符串
|
||||
* @return 节气名称
|
||||
*/
|
||||
public String getJieQi(){
|
||||
String name = "";
|
||||
for(Map.Entry<String,Solar> jq:jieQi.entrySet()){
|
||||
Solar d = jq.getValue();
|
||||
if(d.getYear()==solar.getYear()&&d.getMonth()==solar.getMonth()&&d.getDay()==solar.getDay()){
|
||||
name = jq.getKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(JIE_QI_APPEND.equals(name)){
|
||||
name = JIE_QI_FIRST;
|
||||
}else if(JIE_QI_PREPEND.equals(name)){
|
||||
name = JIE_QI_LAST;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当天节气对象,如果无节气,返回null
|
||||
* @return 节气对象
|
||||
*/
|
||||
public JieQi getCurrentJieQi(){
|
||||
String name = getJieQi();
|
||||
return name.length()>0 ? new JieQi(name,solar) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当天节令对象,如果无节令,返回null
|
||||
* @return 节气对象
|
||||
*/
|
||||
public JieQi getCurrentJie(){
|
||||
String name = getJie();
|
||||
return name.length()>0 ? new JieQi(name,solar) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当天气令对象,如果无气令,返回null
|
||||
* @return 节气对象
|
||||
*/
|
||||
public JieQi getCurrentQi(){
|
||||
String name = getQi();
|
||||
return name.length()>0 ? new JieQi(name,solar) : null;
|
||||
}
|
||||
|
||||
public String toFullString(){
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append(toString());
|
||||
@@ -2058,7 +2148,7 @@ public class Lunar{
|
||||
s.append(f);
|
||||
s.append(")");
|
||||
}
|
||||
String jq = getJie()+getQi();
|
||||
String jq = getJieQi();
|
||||
if(jq.length()>0){
|
||||
s.append(" [");
|
||||
s.append(jq);
|
||||
@@ -2207,4 +2297,55 @@ public class Lunar{
|
||||
}
|
||||
return eightChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取往后推几天的农历日期,如果要往前推,则天数用负数
|
||||
* @param days 天数
|
||||
* @return 农历日期
|
||||
*/
|
||||
public Lunar next(int days){
|
||||
int y = year;
|
||||
int m = month;
|
||||
int d = day;
|
||||
if(days>0){
|
||||
int daysInMonth = LunarUtil.getDaysOfMonth(y,m);
|
||||
int rest = day+days;
|
||||
while(daysInMonth < rest) {
|
||||
if(m>0){
|
||||
if(LunarUtil.getLeapMonth(y)!=m){
|
||||
m++;
|
||||
}else{
|
||||
m = -m;
|
||||
}
|
||||
}else{
|
||||
m = 1-m;
|
||||
}
|
||||
if(13==m){
|
||||
y++;
|
||||
m=1;
|
||||
}
|
||||
rest -= daysInMonth;
|
||||
daysInMonth = LunarUtil.getDaysOfMonth(y,m);
|
||||
}
|
||||
d = rest;
|
||||
}else if(days<0){
|
||||
int daysInMonth = day;
|
||||
int rest = -days;
|
||||
while(daysInMonth <= rest) {
|
||||
if(m>0){
|
||||
m--;
|
||||
if(0==m){
|
||||
y--;
|
||||
m = LunarUtil.getLeapMonth(y)!=12?12:-12;
|
||||
}
|
||||
}else{
|
||||
m = -m;
|
||||
}
|
||||
rest -= daysInMonth;
|
||||
daysInMonth = LunarUtil.getDaysOfMonth(y,m);
|
||||
}
|
||||
d = daysInMonth - rest;
|
||||
}
|
||||
return new Lunar(y,m,d,hour,minute,second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1142,17 +1142,11 @@ public class LunarUtil{
|
||||
public static int nextMonth(int y,int m){
|
||||
int n = Math.abs(m)+1;
|
||||
if(m>0){
|
||||
int index = y-BASE_YEAR+BASE_INDEX;
|
||||
int v = LUNAR_MONTH[2*index+1];
|
||||
v = (v>>4)&0x0F;
|
||||
if(v==m){
|
||||
if(m==getLeapMonth(y)){
|
||||
n = -m;
|
||||
}
|
||||
}
|
||||
if(n==13){
|
||||
n = 1;
|
||||
}
|
||||
return n;
|
||||
return 13!=n?n:1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,33 @@ public class BaZiTest {
|
||||
Assert.assertEquals("时柱", "壬辰", eightChar.getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGanZhi2() {
|
||||
Solar solar = new Solar(1988, 2, 15, 23, 30, 0);
|
||||
Lunar lunar = solar.getLunar();
|
||||
EightChar eightChar = lunar.getEightChar();
|
||||
Assert.assertEquals("年柱", "戊辰", eightChar.getYear());
|
||||
Assert.assertEquals("月柱", "甲寅", eightChar.getMonth());
|
||||
Assert.assertEquals("日柱", "辛丑", eightChar.getDay());
|
||||
Assert.assertEquals("时柱", "戊子", eightChar.getTime());
|
||||
|
||||
solar = new Solar(1988, 2, 15, 22, 30, 0);
|
||||
lunar = solar.getLunar();
|
||||
eightChar = lunar.getEightChar();
|
||||
Assert.assertEquals("年柱", "戊辰", eightChar.getYear());
|
||||
Assert.assertEquals("月柱", "甲寅", eightChar.getMonth());
|
||||
Assert.assertEquals("日柱", "庚子", eightChar.getDay());
|
||||
Assert.assertEquals("时柱", "丁亥", eightChar.getTime());
|
||||
|
||||
solar = new Solar(1988, 2, 2, 22, 30, 0);
|
||||
lunar = solar.getLunar();
|
||||
eightChar = lunar.getEightChar();
|
||||
Assert.assertEquals("年柱", "丁卯", eightChar.getYear());
|
||||
Assert.assertEquals("月柱", "癸丑", eightChar.getMonth());
|
||||
Assert.assertEquals("日柱", "丁亥", eightChar.getDay());
|
||||
Assert.assertEquals("时柱", "辛亥", eightChar.getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHideGan() {
|
||||
Solar solar = new Solar(2005, 12, 23, 8, 37, 0);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package test;
|
||||
|
||||
import com.nlf.calendar.Lunar;
|
||||
import com.nlf.calendar.Solar;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -95,4 +96,63 @@ public class JieQiTest {
|
||||
Assert.assertEquals(name, entry.getValue(), result.get(name).toYmdHms());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Solar solar = Solar.fromYmd(1986,1,5);
|
||||
Lunar lunar = solar.getLunar();
|
||||
Assert.assertEquals("小寒", lunar.getJie());
|
||||
Assert.assertEquals("小寒", lunar.getJieQi());
|
||||
Assert.assertEquals("小寒", lunar.getCurrentJieQi().getName());
|
||||
Assert.assertEquals("小寒", lunar.getCurrentJie().getName());
|
||||
Assert.assertNull(lunar.getCurrentQi());
|
||||
Assert.assertEquals("", lunar.getQi());
|
||||
Assert.assertEquals("大雪", lunar.getPrevJie().getName());
|
||||
Assert.assertEquals("冬至", lunar.getPrevQi().getName());
|
||||
Assert.assertEquals("冬至", lunar.getPrevJieQi().getName());
|
||||
|
||||
solar = Solar.fromYmdHms(1986,1,20,17,0,0);
|
||||
lunar = solar.getLunar();
|
||||
Assert.assertEquals("大寒", lunar.getQi());
|
||||
Assert.assertEquals("大寒", lunar.getJieQi());
|
||||
Assert.assertEquals("大寒", lunar.getCurrentJieQi().getName());
|
||||
Assert.assertEquals("大寒", lunar.getCurrentQi().getName());
|
||||
Assert.assertNull(lunar.getCurrentJie());
|
||||
Assert.assertEquals("", lunar.getJie());
|
||||
Assert.assertEquals("立春", lunar.getNextJie().getName());
|
||||
Assert.assertEquals("雨水", lunar.getNextQi().getName());
|
||||
Assert.assertEquals("立春", lunar.getNextJieQi().getName());
|
||||
solar = Solar.fromYmdHms(1986,1,20,14,0,0);
|
||||
lunar = solar.getLunar();
|
||||
Assert.assertEquals("小寒", lunar.getPrevJie().getName());
|
||||
Assert.assertEquals("冬至", lunar.getPrevQi().getName());
|
||||
Assert.assertEquals("小寒", lunar.getPrevJieQi().getName());
|
||||
|
||||
solar = Solar.fromYmd(1986,12,7);
|
||||
lunar = solar.getLunar();
|
||||
Assert.assertEquals("大雪", lunar.getJie());
|
||||
Assert.assertEquals("大雪", lunar.getJieQi());
|
||||
Assert.assertEquals("大雪", lunar.getCurrentJieQi().getName());
|
||||
Assert.assertEquals("大雪", lunar.getCurrentJie().getName());
|
||||
Assert.assertNull(lunar.getCurrentQi());
|
||||
Assert.assertEquals("", lunar.getQi());
|
||||
Assert.assertEquals("大雪", lunar.getNextJie().getName());
|
||||
Assert.assertEquals("冬至", lunar.getNextQi().getName());
|
||||
Assert.assertEquals("大雪", lunar.getNextJieQi().getName());
|
||||
|
||||
solar = Solar.fromYmd(1986,1,1);
|
||||
lunar = solar.getLunar();
|
||||
Assert.assertEquals("", lunar.getJie());
|
||||
Assert.assertEquals("", lunar.getQi());
|
||||
Assert.assertEquals("", lunar.getJieQi());
|
||||
Assert.assertNull(lunar.getCurrentJieQi());
|
||||
Assert.assertNull(lunar.getCurrentJie());
|
||||
Assert.assertNull(lunar.getCurrentQi());
|
||||
Assert.assertEquals("大雪", lunar.getPrevJie().getName());
|
||||
Assert.assertEquals("冬至", lunar.getPrevQi().getName());
|
||||
Assert.assertEquals("冬至", lunar.getPrevJieQi().getName());
|
||||
Assert.assertEquals("小寒", lunar.getNextJie().getName());
|
||||
Assert.assertEquals("大寒", lunar.getNextQi().getName());
|
||||
Assert.assertEquals("小寒", lunar.getNextJieQi().getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package test;
|
||||
|
||||
import com.nlf.calendar.Lunar;
|
||||
import com.nlf.calendar.Solar;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -20,4 +21,13 @@ public class LunarTest {
|
||||
Assert.assertEquals("2019-05-01 00:00:00 星期三 (劳动节) 金牛座",date.getSolar().toFullString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNext(){
|
||||
Solar solar = new Solar(2020,1,10,12,0,0);
|
||||
Lunar lunar = solar.getLunar();
|
||||
for(int i=-500;i<500;i++){
|
||||
Assert.assertEquals("推移天数:"+i,solar.next(i).getLunar().toFullString(),lunar.next(i).toFullString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user