Skip to content

Commit 9eee11e

Browse files
committed
see 04/08 log
1 parent b9c7daa commit 9eee11e

File tree

2 files changed

+117
-65
lines changed

2 files changed

+117
-65
lines changed

subutil/src/main/java/com/blankj/subutil/util/LunarUtils.java

Lines changed: 87 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private LunarUtils() {
1717
/*
1818
* |----4位闰月|-------------13位1为30天,0为29天|
1919
*/
20-
private static int[] lunar_month_days = {1887, 0x1694, 0x16aa, 0x4ad5, 0xab6, 0xc4b7, 0x4ae, 0xa56, 0xb52a, 0x1d2a,
20+
private static final int[] LUNAR_MONTH_DAYS = {1887, 0x1694, 0x16aa, 0x4ad5, 0xab6, 0xc4b7, 0x4ae, 0xa56, 0xb52a, 0x1d2a,
2121
0xd54, 0x75aa, 0x156a, 0x1096d, 0x95c, 0x14ae, 0xaa4d, 0x1a4c, 0x1b2a, 0x8d55, 0xad4, 0x135a, 0x495d, 0x95c,
2222
0xd49b, 0x149a, 0x1a4a, 0xbaa5, 0x16a8, 0x1ad4, 0x52da, 0x12b6, 0xe937, 0x92e, 0x1496, 0xb64b, 0xd4a, 0xda8,
2323
0x95b5, 0x56c, 0x12ae, 0x492f, 0x92e, 0xcc96, 0x1a94, 0x1d4a, 0xada9, 0xb5a, 0x56c, 0x726e, 0x125c, 0xf92d,
@@ -35,7 +35,7 @@ private LunarUtils() {
3535
0x14ae, 0x8a4e, 0x1a4c, 0x11d26, 0x1aa4, 0x1b54, 0xcd6a, 0xada, 0x95c, 0x949d, 0x149a, 0x1a2a, 0x5b25,
3636
0x1aa4, 0xfb52, 0x16b4, 0xaba, 0xa95b, 0x936, 0x1496, 0x9a4b, 0x154a, 0x136a5, 0xda4, 0x15ac};
3737

38-
private static int[] solar_1_1 = {1887, 0xec04c, 0xec23f, 0xec435, 0xec649, 0xec83e, 0xeca51, 0xecc46, 0xece3a,
38+
private static final int[] SOLAR_1_1 = {1887, 0xec04c, 0xec23f, 0xec435, 0xec649, 0xec83e, 0xeca51, 0xecc46, 0xece3a,
3939
0xed04d, 0xed242, 0xed436, 0xed64a, 0xed83f, 0xeda53, 0xedc48, 0xede3d, 0xee050, 0xee244, 0xee439, 0xee64d,
4040
0xee842, 0xeea36, 0xeec4a, 0xeee3e, 0xef052, 0xef246, 0xef43a, 0xef64e, 0xef843, 0xefa37, 0xefc4b, 0xefe41,
4141
0xf0054, 0xf0248, 0xf043c, 0xf0650, 0xf0845, 0xf0a38, 0xf0c4d, 0xf0e42, 0xf1037, 0xf124a, 0xf143e, 0xf1651,
@@ -57,62 +57,32 @@ private LunarUtils() {
5757
0x106a3d, 0x106c51, 0x106e47, 0x10703c, 0x10724f, 0x107444, 0x107638, 0x10784c, 0x107a3f, 0x107c53,
5858
0x107e48};
5959

60-
private static int GetBitInt(final int data, final int length, final int shift) {
60+
private static int getBitInt(final int data, final int length, final int shift) {
6161
return (data & (((1 << length) - 1) << shift)) >> shift;
6262
}
6363

64-
/**
65-
* 从1582年10月开始算
66-
*
67-
* @param y 年
68-
* @param m 月
69-
* @param d 日
70-
* @return 阳历
71-
*/
72-
private static long SolarToInt(int y, int m, final int d) {
73-
m = (m + 9) % 12;
74-
y = y - m / 10;
75-
return 365 * y + y / 4 - y / 100 + y / 400 + (m * 306 + 5) / 10 + (d - 1);
76-
}
7764

7865
/**
66+
* 农历年转干支年
67+
*
7968
* @param lunarYear 农历年份
80-
* @return String of Ganzhi: 甲子年 Tiangan:甲乙丙丁戊己庚辛壬癸
81-
* Dizhi: 子丑寅卯辰巳无为申酉戌亥
69+
* @return 干支年
8270
*/
83-
public static String lunarYearToGanZhi(final int lunarYear) {
71+
public static String lunarYear2GanZhi(final int lunarYear) {
8472
final String[] tianGan = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
8573
final String[] diZhi = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
8674
return tianGan[(lunarYear - 4) % 10] + diZhi[(lunarYear - 4) % 12] + "年";
8775
}
8876

89-
private static Solar SolarFromInt(final long g) {
90-
long y = (10000 * g + 14780) / 3652425;
91-
long ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
92-
if (ddd < 0) {
93-
y--;
94-
ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
95-
}
96-
long mi = (100 * ddd + 52) / 3060;
97-
long mm = (mi + 2) % 12 + 1;
98-
y = y + (mi + 2) / 12;
99-
long dd = ddd - (mi * 306 + 5) / 10 + 1;
100-
Solar solar = new Solar();
101-
solar.solarYear = (int) y;
102-
solar.solarMonth = (int) mm;
103-
solar.solarDay = (int) dd;
104-
return solar;
105-
}
106-
10777
/**
10878
* 农历转公历
10979
*
11080
* @param lunar 农历
111-
* @return 阴历
81+
* @return 公历
11282
*/
113-
public static Solar LunarToSolar(final Lunar lunar) {
114-
int days = lunar_month_days[lunar.lunarYear - lunar_month_days[0]];
115-
int leap = GetBitInt(days, 4, 13);
83+
public static Solar lunar2Solar(final Lunar lunar) {
84+
int days = LUNAR_MONTH_DAYS[lunar.lunarYear - LUNAR_MONTH_DAYS[0]];
85+
int leap = getBitInt(days, 4, 13);
11686
int offset = 0;
11787
int loopend = leap;
11888
if (!lunar.isLeap) {
@@ -123,17 +93,17 @@ public static Solar LunarToSolar(final Lunar lunar) {
12393
}
12494
}
12595
for (int i = 0; i < loopend; i++) {
126-
offset += GetBitInt(days, 1, 12 - i) == 1 ? 30 : 29;
96+
offset += getBitInt(days, 1, 12 - i) == 1 ? 30 : 29;
12797
}
12898
offset += lunar.lunarDay;
12999

130-
int solar11 = solar_1_1[lunar.lunarYear - solar_1_1[0]];
100+
int solar11 = SOLAR_1_1[lunar.lunarYear - SOLAR_1_1[0]];
131101

132-
int y = GetBitInt(solar11, 12, 9);
133-
int m = GetBitInt(solar11, 4, 5);
134-
int d = GetBitInt(solar11, 5, 0);
102+
int y = getBitInt(solar11, 12, 9);
103+
int m = getBitInt(solar11, 4, 5);
104+
int d = getBitInt(solar11, 5, 0);
135105

136-
return SolarFromInt(SolarToInt(y, m, d) + offset - 1);
106+
return solarFromInt(solarToInt(y, m, d) + offset - 1);
137107
}
138108

139109
/**
@@ -142,30 +112,30 @@ public static Solar LunarToSolar(final Lunar lunar) {
142112
* @param solar 公历
143113
* @return 阴历
144114
*/
145-
public static Lunar SolarToLunar(final Solar solar) {
115+
public static Lunar solar2Lunar(final Solar solar) {
146116
Lunar lunar = new Lunar();
147-
int index = solar.solarYear - solar_1_1[0];
117+
int index = solar.solarYear - SOLAR_1_1[0];
148118
int data = (solar.solarYear << 9) | (solar.solarMonth << 5) | (solar.solarDay);
149119
int solar11 = 0;
150-
if (solar_1_1[index] > data) {
120+
if (SOLAR_1_1[index] > data) {
151121
index--;
152122
}
153-
solar11 = solar_1_1[index];
154-
int y = GetBitInt(solar11, 12, 9);
155-
int m = GetBitInt(solar11, 4, 5);
156-
int d = GetBitInt(solar11, 5, 0);
157-
long offset = SolarToInt(solar.solarYear, solar.solarMonth, solar.solarDay) - SolarToInt(y, m, d);
123+
solar11 = SOLAR_1_1[index];
124+
int y = getBitInt(solar11, 12, 9);
125+
int m = getBitInt(solar11, 4, 5);
126+
int d = getBitInt(solar11, 5, 0);
127+
long offset = solarToInt(solar.solarYear, solar.solarMonth, solar.solarDay) - solarToInt(y, m, d);
158128

159-
int days = lunar_month_days[index];
160-
int leap = GetBitInt(days, 4, 13);
129+
int days = LUNAR_MONTH_DAYS[index];
130+
int leap = getBitInt(days, 4, 13);
161131

162-
int lunarY = index + solar_1_1[0];
132+
int lunarY = index + SOLAR_1_1[0];
163133
int lunarM = 1;
164134
int lunarD = 1;
165135
offset += 1;
166136

167137
for (int i = 0; i < 13; i++) {
168-
int dm = GetBitInt(days, 1, 12 - i) == 1 ? 30 : 29;
138+
int dm = getBitInt(days, 1, 12 - i) == 1 ? 30 : 29;
169139
if (offset > dm) {
170140
lunarM++;
171141
offset -= dm;
@@ -183,22 +153,74 @@ public static Lunar SolarToLunar(final Solar solar) {
183153
lunar.isLeap = true;
184154
}
185155
}
186-
187156
lunar.lunarDay = lunarD;
188157
return lunar;
189158
}
190159

160+
private static Solar solarFromInt(final long g) {
161+
long y = (10000 * g + 14780) / 3652425;
162+
long ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
163+
if (ddd < 0) {
164+
y--;
165+
ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
166+
}
167+
long mi = (100 * ddd + 52) / 3060;
168+
long mm = (mi + 2) % 12 + 1;
169+
y = y + (mi + 2) / 12;
170+
long dd = ddd - (mi * 306 + 5) / 10 + 1;
171+
Solar solar = new Solar();
172+
solar.solarYear = (int) y;
173+
solar.solarMonth = (int) mm;
174+
solar.solarDay = (int) dd;
175+
return solar;
176+
}
177+
178+
private static long solarToInt(int y, int m, final int d) {
179+
m = (m + 9) % 12;
180+
y = y - m / 10;
181+
return 365 * y + y / 4 - y / 100 + y / 400 + (m * 306 + 5) / 10 + (d - 1);
182+
}
183+
191184
public static class Lunar {
192-
public boolean isLeap;
193-
public int lunarDay;
194-
public int lunarMonth;
195185
public int lunarYear;
186+
public int lunarMonth;
187+
public int lunarDay;
188+
public boolean isLeap;
189+
190+
Lunar() {
191+
}
192+
193+
public Lunar(int lunarYear, int lunarMonth, int lunarDay, boolean isLeap) {
194+
this.lunarYear = lunarYear;
195+
this.lunarMonth = lunarMonth;
196+
this.lunarDay = lunarDay;
197+
this.isLeap = isLeap;
198+
}
199+
200+
@Override
201+
public String toString() {
202+
return "" + lunarYear + ", " + lunarMonth + ", " + lunarDay + ", " + isLeap;
203+
}
196204
}
197205

198206
public static class Solar {
199-
public int solarDay;
200-
public int solarMonth;
201207
public int solarYear;
208+
public int solarMonth;
209+
public int solarDay;
210+
211+
Solar() {
212+
}
213+
214+
public Solar(int solarYear, int solarMonth, int solarDay) {
215+
this.solarYear = solarYear;
216+
this.solarMonth = solarMonth;
217+
this.solarDay = solarDay;
218+
}
219+
220+
@Override
221+
public String toString() {
222+
return "" + solarYear + ", " + solarMonth + ", " + solarDay;
223+
}
202224
}
203225
}
204226

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.blankj.subutil.util;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* <pre>
7+
* author: Blankj
8+
* blog : http://blankj.com
9+
* time : 2018/04/08
10+
* desc :
11+
* </pre>
12+
*/
13+
public class LunarUtilsTest {
14+
15+
@Test
16+
public void lunarYear2GanZhi() throws Exception {
17+
System.out.println(LunarUtils.lunarYear2GanZhi(2018));
18+
}
19+
20+
@Test
21+
public void lunar2Solar() throws Exception {
22+
System.out.println(LunarUtils.lunar2Solar(new LunarUtils.Lunar(2018, 2, 23, false)));
23+
}
24+
25+
@Test
26+
public void solar2Lunar() throws Exception {
27+
System.out.println(LunarUtils.solar2Lunar(new LunarUtils.Solar(2018, 4, 8)));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)