@@ -17,7 +17,7 @@ private LunarUtils() {
17
17
/*
18
18
* |----4位闰月|-------------13位1为30天,0为29天|
19
19
*/
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 ,
21
21
0xd54 , 0x75aa , 0x156a , 0x1096d , 0x95c , 0x14ae , 0xaa4d , 0x1a4c , 0x1b2a , 0x8d55 , 0xad4 , 0x135a , 0x495d , 0x95c ,
22
22
0xd49b , 0x149a , 0x1a4a , 0xbaa5 , 0x16a8 , 0x1ad4 , 0x52da , 0x12b6 , 0xe937 , 0x92e , 0x1496 , 0xb64b , 0xd4a , 0xda8 ,
23
23
0x95b5 , 0x56c , 0x12ae , 0x492f , 0x92e , 0xcc96 , 0x1a94 , 0x1d4a , 0xada9 , 0xb5a , 0x56c , 0x726e , 0x125c , 0xf92d ,
@@ -35,7 +35,7 @@ private LunarUtils() {
35
35
0x14ae , 0x8a4e , 0x1a4c , 0x11d26 , 0x1aa4 , 0x1b54 , 0xcd6a , 0xada , 0x95c , 0x949d , 0x149a , 0x1a2a , 0x5b25 ,
36
36
0x1aa4 , 0xfb52 , 0x16b4 , 0xaba , 0xa95b , 0x936 , 0x1496 , 0x9a4b , 0x154a , 0x136a5 , 0xda4 , 0x15ac };
37
37
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 ,
39
39
0xed04d , 0xed242 , 0xed436 , 0xed64a , 0xed83f , 0xeda53 , 0xedc48 , 0xede3d , 0xee050 , 0xee244 , 0xee439 , 0xee64d ,
40
40
0xee842 , 0xeea36 , 0xeec4a , 0xeee3e , 0xef052 , 0xef246 , 0xef43a , 0xef64e , 0xef843 , 0xefa37 , 0xefc4b , 0xefe41 ,
41
41
0xf0054 , 0xf0248 , 0xf043c , 0xf0650 , 0xf0845 , 0xf0a38 , 0xf0c4d , 0xf0e42 , 0xf1037 , 0xf124a , 0xf143e , 0xf1651 ,
@@ -57,62 +57,32 @@ private LunarUtils() {
57
57
0x106a3d , 0x106c51 , 0x106e47 , 0x10703c , 0x10724f , 0x107444 , 0x107638 , 0x10784c , 0x107a3f , 0x107c53 ,
58
58
0x107e48 };
59
59
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 ) {
61
61
return (data & (((1 << length ) - 1 ) << shift )) >> shift ;
62
62
}
63
63
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
- }
77
64
78
65
/**
66
+ * 农历年转干支年
67
+ *
79
68
* @param lunarYear 农历年份
80
- * @return String of Ganzhi: 甲子年 Tiangan:甲乙丙丁戊己庚辛壬癸
81
- * Dizhi: 子丑寅卯辰巳无为申酉戌亥
69
+ * @return 干支年
82
70
*/
83
- public static String lunarYearToGanZhi (final int lunarYear ) {
71
+ public static String lunarYear2GanZhi (final int lunarYear ) {
84
72
final String [] tianGan = {"甲" , "乙" , "丙" , "丁" , "戊" , "己" , "庚" , "辛" , "壬" , "癸" };
85
73
final String [] diZhi = {"子" , "丑" , "寅" , "卯" , "辰" , "巳" , "午" , "未" , "申" , "酉" , "戌" , "亥" };
86
74
return tianGan [(lunarYear - 4 ) % 10 ] + diZhi [(lunarYear - 4 ) % 12 ] + "年" ;
87
75
}
88
76
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
-
107
77
/**
108
78
* 农历转公历
109
79
*
110
80
* @param lunar 农历
111
- * @return 阴历
81
+ * @return 公历
112
82
*/
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 );
116
86
int offset = 0 ;
117
87
int loopend = leap ;
118
88
if (!lunar .isLeap ) {
@@ -123,17 +93,17 @@ public static Solar LunarToSolar(final Lunar lunar) {
123
93
}
124
94
}
125
95
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 ;
127
97
}
128
98
offset += lunar .lunarDay ;
129
99
130
- int solar11 = solar_1_1 [lunar .lunarYear - solar_1_1 [0 ]];
100
+ int solar11 = SOLAR_1_1 [lunar .lunarYear - SOLAR_1_1 [0 ]];
131
101
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 );
135
105
136
- return SolarFromInt ( SolarToInt (y , m , d ) + offset - 1 );
106
+ return solarFromInt ( solarToInt (y , m , d ) + offset - 1 );
137
107
}
138
108
139
109
/**
@@ -142,30 +112,30 @@ public static Solar LunarToSolar(final Lunar lunar) {
142
112
* @param solar 公历
143
113
* @return 阴历
144
114
*/
145
- public static Lunar SolarToLunar (final Solar solar ) {
115
+ public static Lunar solar2Lunar (final Solar solar ) {
146
116
Lunar lunar = new Lunar ();
147
- int index = solar .solarYear - solar_1_1 [0 ];
117
+ int index = solar .solarYear - SOLAR_1_1 [0 ];
148
118
int data = (solar .solarYear << 9 ) | (solar .solarMonth << 5 ) | (solar .solarDay );
149
119
int solar11 = 0 ;
150
- if (solar_1_1 [index ] > data ) {
120
+ if (SOLAR_1_1 [index ] > data ) {
151
121
index --;
152
122
}
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 );
158
128
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 );
161
131
162
- int lunarY = index + solar_1_1 [0 ];
132
+ int lunarY = index + SOLAR_1_1 [0 ];
163
133
int lunarM = 1 ;
164
134
int lunarD = 1 ;
165
135
offset += 1 ;
166
136
167
137
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 ;
169
139
if (offset > dm ) {
170
140
lunarM ++;
171
141
offset -= dm ;
@@ -183,22 +153,74 @@ public static Lunar SolarToLunar(final Solar solar) {
183
153
lunar .isLeap = true ;
184
154
}
185
155
}
186
-
187
156
lunar .lunarDay = lunarD ;
188
157
return lunar ;
189
158
}
190
159
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
+
191
184
public static class Lunar {
192
- public boolean isLeap ;
193
- public int lunarDay ;
194
- public int lunarMonth ;
195
185
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
+ }
196
204
}
197
205
198
206
public static class Solar {
199
- public int solarDay ;
200
- public int solarMonth ;
201
207
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
+ }
202
224
}
203
225
}
204
226
0 commit comments