Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
midjourney-proxy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
徐少华
midjourney-proxy
Commits
d6fa8033
Commit
d6fa8033
authored
Nov 28, 2019
by
zhangdongwei-intern
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 月份的干支有误
parent
63f40978
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
26 deletions
+46
-26
LunarCalendarUtils.java
.../java/com/pcloud/book/util/common/LunarCalendarUtils.java
+46
-26
No files found.
pcloud-service-book/src/main/java/com/pcloud/book/util/common/LunarCalendarUtils.java
View file @
d6fa8033
...
...
@@ -4,6 +4,7 @@ import com.pcloud.common.utils.string.StringUtil;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Arrays
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.GregorianCalendar
;
...
...
@@ -244,6 +245,11 @@ public class LunarCalendarUtils {
daysOfMonth
=
daysInLunarMonth
(
iYear
,
iMonth
);
offset
-=
daysOfMonth
;
}
// offset小于0时,也要校正
if
(
offset
<
0
)
{
offset
+=
daysOfMonth
;
--
iMonth
;
}
// 当前月超过闰月,要校正
if
(
leapMonth
!=
0
&&
iMonth
>
leapMonth
)
{
--
iMonth
;
...
...
@@ -251,11 +257,6 @@ public class LunarCalendarUtils {
isLeap
=
true
;
}
}
// offset小于0时,也要校正
if
(
offset
<
0
)
{
offset
+=
daysOfMonth
;
--
iMonth
;
}
lunarDate
[
1
]
=
iMonth
;
lunarDate
[
2
]
=
offset
+
1
;
lunarDate
[
3
]
=
isLeap
?
1
:
0
;
...
...
@@ -373,11 +374,20 @@ public class LunarCalendarUtils {
* 传入年份,返回干支年
* 公元元年是辛酉年,干的周期是10,支的周期是12
* @param year
* @param month
* @param day
* @return
*/
private
static
String
ganZhiYear
(
int
year
){
private
static
String
ganZhiYear
(
int
year
,
int
month
,
int
day
){
int
gan
=
year
%
10
>
3
?
(
year
%
10
-
3
)
:
(
year
%
10
-
3
+
10
);
int
zhi
=
year
%
12
>
3
?
(
year
%
12
-
3
)
:
(
year
%
12
-
3
+
12
);
//计算该年的正月初一是几月几日
int
[]
date
=
lunarToSolar
(
year
,
1
,
1
,
false
);
if
(
month
<
date
[
1
]
||
(
month
==
date
[
1
]
&&
day
<
date
[
2
])){
//如果当日在今年正月初一之前,干支都要减一
gan
=
gan
==
1
?
10
:
(
gan
-
1
);
zhi
=
zhi
==
1
?
12
:
(
zhi
-
1
);
}
return
GAN
[
gan
-
1
]
+
ZHI
[
zhi
-
1
]
+
"年"
;
}
...
...
@@ -385,26 +395,29 @@ public class LunarCalendarUtils {
* 传入年月日和农历年月,返回干支月
* 支的周期是12, 正好对应12个月
* 干的周期是10,每一年的一月的“干”间隔2(即只能是:甲丙戊庚壬),每年一月的周期是5,
* 1990年1月是丁丑月
* @param month
* @return
*/
private
static
String
ganZhiMonth
(
int
year
,
int
month
,
int
day
,
int
lunarYear
,
int
lunarMonth
){
private
static
String
ganZhiMonth
(
int
year
,
int
month
,
int
day
){
//计算该月与1900年1月相差的月数
int
monthNum
=
(
year
-
1900
)
*
12
+
month
;
//该月第一个节气是一年里的第几个节气
int
num
=
month
*
2
-
2
;
//计算该节气在几日
int
d
=
sTerm
(
year
,
num
);
i
nt
m
=
lunarMonth
;
int
y
=
lunarYear
;
if
(
day
<
d
){
//如果今天在本月第一个节气前 农历月份取上一月
y
=
lunarMonth
==
1
?
lunarYear
-
1
:
lunarYear
;
m
=
lunarMonth
==
1
?
12
:
lunarMonth
-
1
;
}
int
gan
Year
=
y
%
10
>
3
?
(
y
%
10
-
3
)
:
(
y
%
10
-
3
+
10
)
;
int
gan
=
(
ganYear
*
2
+
m
)
%
10
;
gan
=
gan
==
0
?
10
:
gan
;
int
zhi
=
(
m
+
1
)%
12
;
return
GAN
[
gan
-
1
]
+
ZHI
[
zhi
]
+
"月"
;
int
d
1
=
sTerm
(
year
,
num
);
i
f
(
day
<
d1
){
//如果当天在该月的第一个节气前,月数减一
monthNum
-=
1
;
}
//计算干支数
monthNum
%=
60
;
//由于1900年1月是丁丑月,所以干支数需要加13
int
gan
Zhi
=
monthNum
+
13
;
ganZhi
=
ganZhi
>
60
?
ganZhi
-
60
:
ganZhi
;
int
gan
=
ganZhi
%
10
==
0
?
ganZhi
%
10
+
10
:
ganZhi
%
10
;
int
zhi
=
ganZhi
%
12
==
0
?
ganZhi
%
12
+
12
:
ganZhi
%
12
;
return
GAN
[
gan
-
1
]
+
ZHI
[
zhi
-
1
]
+
"月"
;
}
/**
...
...
@@ -416,6 +429,7 @@ public class LunarCalendarUtils {
* @return
*/
private
static
String
ganZhiDay
(
int
year
,
int
month
,
int
day
){
//先计算当天与1900年元旦相差的天数
int
ganZhi
=
0
;
int
num
=
0
;
for
(
int
i
=
1900
;
i
<
year
;
i
++
){
...
...
@@ -432,7 +446,9 @@ public class LunarCalendarUtils {
num
+=
1
;
}
num
+=
day
;
//计算干支数
num
%=
60
;
//由于1900年元旦是甲戌日,所以干支数需要加10
ganZhi
=
num
+
10
;
ganZhi
=
ganZhi
>
60
?
ganZhi
-
60
:
ganZhi
;
int
gan
=
ganZhi
%
10
==
0
?
ganZhi
%
10
+
10
:
ganZhi
%
10
;
...
...
@@ -466,26 +482,30 @@ public class LunarCalendarUtils {
}
lunar
+=
CHINESE_NUM
[
result
[
2
]
%
10
==
0
?
10
:
result
[
2
]
%
10
]
+
"日"
;
strMap
.
put
(
"lunar"
,
lunar
);
String
ganZhi
=
ganZhiYear
(
year
)
+
" "
+
ganZhiMonth
(
year
,
month
,
day
,
result
[
0
],
result
[
1
]
)
+
" "
+
ganZhiDay
(
year
,
month
,
day
);
String
ganZhi
=
ganZhiYear
(
year
,
month
,
day
)
+
" "
+
ganZhiMonth
(
year
,
month
,
day
)
+
" "
+
ganZhiDay
(
year
,
month
,
day
);
strMap
.
put
(
"ganZhi"
,
ganZhi
);
return
strMap
;
}
/*
public static void main(String[] args){
public
static
void
main
(
String
[]
args
){
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy/MM/dd HH:mm:ss"
);
String str = "20
23/2/
1 00:00:00";
String
str
=
"20
19/02/0
1 00:00:00"
;
try
{
Date
d
=
sdf
.
parse
(
str
);
*//*
Calendar calendar = Calendar.getInstance();
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
setTime
(
d
);
System.out.println(calendar.get(Calendar.MONTH) + 1);
*//*
System
.
out
.
println
(
calendar
.
get
(
Calendar
.
MONTH
)
+
1
);
System
.
out
.
printf
(
"字符串 %s 通过格式 yyyy/MM/dd HH:mm:ss %n转换为日期对象: %s\n"
,
str
,
d
.
toString
());
//System.out.println(leapMonth(calendar.get(Calendar.YEAR)));
System
.
out
.
println
(
dataToLunar
(
d
));
/*for (int year = 1900; year < 2099; year ++) {
System.out.println(Arrays.toString(lunarToSolar(year, 1, 1, false)));
}*/
}
catch
(
ParseException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
}
*/
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment