├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── README_EN.md ├── pom.xml └── src ├── assembly └── assembly.xml ├── main └── java │ └── com │ └── nlf │ └── calendar │ ├── EightChar.java │ ├── Foto.java │ ├── FotoFestival.java │ ├── Fu.java │ ├── Holiday.java │ ├── JieQi.java │ ├── Lunar.java │ ├── LunarMonth.java │ ├── LunarTime.java │ ├── LunarYear.java │ ├── NineStar.java │ ├── ShuJiu.java │ ├── Solar.java │ ├── SolarHalfYear.java │ ├── SolarMonth.java │ ├── SolarSeason.java │ ├── SolarWeek.java │ ├── SolarYear.java │ ├── Tao.java │ ├── TaoFestival.java │ ├── eightchar │ ├── DaYun.java │ ├── LiuNian.java │ ├── LiuYue.java │ ├── XiaoYun.java │ └── Yun.java │ └── util │ ├── FotoUtil.java │ ├── HolidayUtil.java │ ├── LunarUtil.java │ ├── ShouXingUtil.java │ ├── SolarUtil.java │ └── TaoUtil.java └── test └── java ├── sample ├── BaZiTest.java ├── BaZiTestNew.java ├── HalfYearTest.java ├── HolidayTest.java ├── LunarTest.java ├── MonthTest.java ├── SeasonTest.java ├── SolarTest.java ├── WeekTest.java └── YearTest.java └── test ├── BaZiTest.java ├── FestivalTest.java ├── FotoTest.java ├── FuTest.java ├── GanZhiTest.java ├── HalfYearTest.java ├── HolidayTest.java ├── JieQiTest.java ├── JulianDayTest.java ├── LiuYaoTest.java ├── LunarMonthTest.java ├── LunarTest.java ├── MonthTest.java ├── NineStarTest.java ├── SeasonTest.java ├── ShuJiuTest.java ├── SolarTest.java ├── SolarWeekTest.java ├── TaoTest.java ├── TimeTest.java ├── WeekTest.java ├── WuHouTest.java ├── XingZuoTest.java ├── XunTest.java ├── YearTest.java └── YunTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | target/ 3 | 4 | #idea 5 | .idea/ 6 | /idea/ 7 | *.ipr 8 | *.iml 9 | *.iws 10 | 11 | # temp file 12 | *.log 13 | *.cache 14 | *.diff 15 | *.patch 16 | *.tmp 17 | .DS_Store -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | 4 | ## [1.3.12] - 2024-02-06 5 | 1. 八字转阳历结果按时间先后排序,转换速度大幅提升。 6 | 7 | ## [1.3.13] - 2024-02-29 8 | 1. 修复节气当天获取下一节气仍为当前节气的问题。 9 | 2. 修复每日宜忌存在重复项的问题。 10 | 3. 修复八字转阳历存在遗漏的问题。 11 | 12 | ## [1.3.14] - 2024-03-17 13 | 1. 修复八字转阳历存在遗漏的问题。 14 | 15 | ## [1.3.15] - 2024-11-12 16 | 1. 新增2025年法定假日数据。 17 | 18 | ## [1.4.0] - 2025-02-06 19 | 1. 修复九星错别字。 20 | 2. 优化每日宜忌算法。 21 | 3. 去除每日宜忌的重复项。 22 | > 不建议使用该版本 23 | 24 | ## [1.7.0] - 2025-02-12 25 | 1. 修复每日宜忌错别字。 26 | > 不建议使用该版本 27 | 28 | ## [1.7.1] - 2025-02-27 29 | 1. 修复每日宜忌错误。 30 | > 不建议使用该版本 31 | 32 | ## [1.7.2] - 2025-03-05 33 | 1. 修复每日宜忌错误。 34 | 2. 更新2018之后的△T参数。 35 | 36 | ## [1.7.3] - 2025-04-07 37 | 1. 修复个别日太岁方位的错误。 38 | 2. 修复八字身宫计算错误的问题。 39 | 40 | ## [1.7.4] - 2025-04-29 41 | 1. 修复身宫报错的问题。 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 6tail 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lunar [![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/6tail/lunar-java/blob/master/LICENSE) 2 | 3 | lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)、道历、佛历工具,支持星座、儒略日、干支、生肖、节气、节日、彭祖百忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道日及吉凶、法定节假日及调休等。 4 | 5 | > 支持java 1.5及以上版本。 6 | 7 | > 干支纪年:支持以正月初一计,支持以立春当日计,支持以立春交接时刻计。 8 | 9 | > 干支纪月:支持以节当日计,支持以节交接时刻计。 10 | 11 | [English](https://github.com/6tail/lunar-java/blob/master/README_EN.md) 12 | 13 | ### Maven 14 | 15 | ```xml 16 | 17 | cn.6tail 18 | lunar 19 | 1.7.4 20 | 21 | ``` 22 | 23 | ### 下载jar 24 | 25 | 如果使用jar,建议下载最新的版本,bug将得到及时的修复。 26 | 27 | [Download](https://github.com/6tail/lunar-java/releases) 28 | 29 | ## 示例 30 | 31 | import com.nlf.calendar.Lunar; 32 | 33 | /** 34 | * 阴历示例 35 | * 36 | */ 37 | public class LunarSample{ 38 | public static void main(String[] args){ 39 | //今天 40 | //Lunar date = new Lunar(); 41 | 42 | //指定阴历的某一天 43 | Lunar date = new Lunar(1986,4,21); 44 | System.out.println(date.toFullString()); 45 | System.out.println(date.getSolar().toFullString()); 46 | } 47 | } 48 | 49 | 输出结果: 50 | 51 | 一九八六年四月廿一 丙寅(虎)年 癸巳(蛇)月 癸酉(鸡)日 子(鼠)时 纳音[炉中火 长流水 剑锋金 桑柘木] 星期四 北方玄武 星宿[斗木獬](吉) 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 煞[东] 52 | 1986-05-29 00:00:00 星期四 双子座 53 | 54 | ## 文档 55 | 56 | 请移步至 [https://6tail.cn/calendar/api.html](https://6tail.cn/calendar/api.html "https://6tail.cn/calendar/api.html") 57 | 58 | ## Star History 59 | 60 | [![Star History Chart](https://api.star-history.com/svg?repos=6tail/lunar-java&type=Date)](https://star-history.com/#6tail/lunar-java&Date) 61 | -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | # lunar [![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/6tail/lunar-java/blob/master/LICENSE) 2 | 3 | lunar is a calendar library for Solar and Chinese Lunar. 4 | 5 | > Support since java 1.5 6 | 7 | [简体中文](https://github.com/6tail/lunar-java/blob/master/README.md) 8 | 9 | ### Maven 10 | 11 | ```xml 12 | 13 | cn.6tail 14 | lunar 15 | 1.7.4 16 | 17 | ``` 18 | 19 | ### Download 20 | 21 | If you will use jars in your projects, I suggest you to download latest versions with less bugs. 22 | 23 | [Download](https://github.com/6tail/lunar-java/releases) 24 | 25 | ## Example 26 | 27 | import com.nlf.calendar.Lunar; 28 | 29 | /** 30 | * 阴历示例 31 | * 32 | */ 33 | public class LunarSample{ 34 | public static void main(String[] args){ 35 | //今天 36 | //Lunar date = new Lunar(); 37 | 38 | //指定阴历的某一天 39 | Lunar date = new Lunar(1986,4,21); 40 | System.out.println(date.toFullString()); 41 | System.out.println(date.getSolar().toFullString()); 42 | } 43 | } 44 | 45 | Output: 46 | 47 | 一九八六年四月廿一 丙寅(虎)年 癸巳(蛇)月 癸酉(鸡)日 子(鼠)时 纳音[炉中火 长流水 剑锋金 桑柘木] 星期四 北方玄武 星宿[斗木獬](吉) 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 煞[东] 48 | 1986-05-29 00:00:00 星期四 双子座 49 | 50 | ## Documentation 51 | 52 | Please visit [https://6tail.cn/calendar/api.html](https://6tail.cn/calendar/api.html "https://6tail.cn/calendar/api.html") 53 | 54 | ## Star History 55 | 56 | [![Star History Chart](https://api.star-history.com/svg?repos=6tail/lunar-java&type=Date)](https://star-history.com/#6tail/lunar-java&Date) 57 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | cn.6tail 8 | lunar 9 | jar 10 | 1.7.4 11 | ${project.groupId}:${project.artifactId} 12 | https://github.com/6tail/lunar-java 13 | a calendar library for Solar and Chinese Lunar 14 | 15 | 16 | The MIT License 17 | http://opensource.org/licenses/MIT 18 | 19 | 20 | 21 | 22 | 6tail 23 | 6tail@6tail.cn 24 | +8 25 | 26 | 27 | 28 | master 29 | scm:git:git@github.com:6tail/lunar-java.git 30 | scm:git:git@github.com:6tail/lunar-java.git 31 | git@github.com:6tail/lunar-java.git 32 | 33 | 34 | 35 | sonatype-nexus-snapshots 36 | Sonatype Nexus snapshot repository 37 | https://oss.sonatype.org/content/repositories/snapshots 38 | 39 | 40 | sonatype-nexus-staging 41 | Sonatype Nexus release repository 42 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 43 | 44 | 45 | 46 | UTF-8 47 | UTF-8 48 | UTF-8 49 | 1.5 50 | 1.5 51 | 6tail 52 | 3.3 53 | 2.5 54 | 2.9 55 | 2.4 56 | 2.2.2 57 | 1.6 58 | 59 | 60 | 61 | 62 | junit 63 | junit 64 | 4.13.2 65 | test 66 | 67 | 68 | 69 | src/test/java 70 | 71 | 72 | src/test/resources 73 | 74 | 75 | target/test-classes 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-compiler-plugin 80 | ${maven-compiler-plugin-version} 81 | 82 | ${source-version} 83 | ${target-version} 84 | ${encoding} 85 | 86 | 87 | 88 | org.apache.maven.plugins 89 | maven-surefire-plugin 90 | ${maven-surefire-plugin-version} 91 | 92 | -Dfile.encoding=UTF-8 93 | false 94 | 95 | 96 | 97 | org.apache.maven.plugins 98 | maven-javadoc-plugin 99 | ${maven-javadoc-plugin-version} 100 | 101 | 102 | attach-javadocs 103 | 104 | jar 105 | 106 | 107 | ${java.home}/../bin/javadoc 108 | -Xdoclint:none 109 | ${source-version} 110 | 111 | 112 | 113 | 114 | 115 | org.apache.maven.plugins 116 | maven-source-plugin 117 | ${maven-source-plugin-version} 118 | 119 | 120 | attach-sources 121 | 122 | jar-no-fork 123 | 124 | 125 | 126 | 127 | 128 | org.apache.maven.plugins 129 | maven-release-plugin 130 | ${maven-release-plugin-version} 131 | 132 | -Dgpg.passphrase=${gpg.passphrase} 133 | 134 | 135 | 136 | org.apache.maven.plugins 137 | maven-gpg-plugin 138 | ${maven-gpg-plugin-version} 139 | 140 | ${gpg.passphrase} 141 | 142 | 143 | 144 | sign-artifacts 145 | verify 146 | 147 | sign 148 | 149 | 150 | 151 | 152 | 153 | org.apache.maven.plugins 154 | maven-assembly-plugin 155 | 2.2-beta-5 156 | 157 | false 158 | ${project.artifactId}-${project.version} 159 | 160 | ${basedir}/src/assembly/assembly.xml 161 | 162 | 163 | 164 | ${creator} 165 | ${creator} 166 | 167 | 168 | 169 | 170 | 171 | ${project.artifactId}-${project.version} 172 | 173 | 174 | -------------------------------------------------------------------------------- /src/assembly/assembly.xml: -------------------------------------------------------------------------------- 1 | 2 | jar-with-dependencies 3 | 4 | jar 5 | 6 | false 7 | 8 | 9 | ${project.build.directory}/classes 10 | / 11 | 12 | 13 | ${basedir}/src/main/java 14 | / 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/Foto.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | import com.nlf.calendar.util.FotoUtil; 4 | import com.nlf.calendar.util.LunarUtil; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * 佛历 11 | * 12 | * @author 6tail 13 | */ 14 | public class Foto { 15 | 16 | public static final int DEAD_YEAR = -543; 17 | 18 | /** 19 | * 阴历 20 | */ 21 | private final Lunar lunar; 22 | 23 | public Foto(Lunar lunar) { 24 | this.lunar = lunar; 25 | } 26 | 27 | public static Foto fromLunar(Lunar lunar) { 28 | return new Foto(lunar); 29 | } 30 | 31 | public static Foto fromYmdHms(int year, int month, int day, int hour, int minute, int second) { 32 | return Foto.fromLunar(Lunar.fromYmdHms(year + DEAD_YEAR - 1, month, day, hour, minute, second)); 33 | } 34 | 35 | public static Foto fromYmd(int year, int month, int day) { 36 | return fromYmdHms(year, month, day, 0, 0, 0); 37 | } 38 | 39 | public Lunar getLunar() { 40 | return lunar; 41 | } 42 | 43 | public int getYear() { 44 | int sy = lunar.getSolar().getYear(); 45 | int y = sy - DEAD_YEAR; 46 | if (sy == lunar.getYear()) { 47 | y++; 48 | } 49 | return y; 50 | } 51 | 52 | public int getMonth() { 53 | return lunar.getMonth(); 54 | } 55 | 56 | public int getDay() { 57 | return lunar.getDay(); 58 | } 59 | 60 | public String getYearInChinese() { 61 | String y = getYear() + ""; 62 | StringBuilder s = new StringBuilder(); 63 | for (int i = 0, j = y.length(); i < j; i++) { 64 | s.append(LunarUtil.NUMBER[y.charAt(i) - '0']); 65 | } 66 | return s.toString(); 67 | } 68 | 69 | public String getMonthInChinese() { 70 | return lunar.getMonthInChinese(); 71 | } 72 | 73 | public String getDayInChinese() { 74 | return lunar.getDayInChinese(); 75 | } 76 | 77 | /** 78 | * 获取因果犯忌 79 | * @return 因果犯忌 80 | */ 81 | public List getFestivals() { 82 | List l = new ArrayList(); 83 | List fs = FotoUtil.FESTIVAL.get(getMonth() + "-" + getDay()); 84 | if (null != fs) { 85 | l.addAll(fs); 86 | } 87 | return l; 88 | } 89 | 90 | /** 91 | * 获取纪念日 92 | * @return 纪念日 93 | */ 94 | public List getOtherFestivals() { 95 | List l = new ArrayList(); 96 | List fs = FotoUtil.OTHER_FESTIVAL.get(getMonth() + "-" + getDay()); 97 | if (null != fs) { 98 | l.addAll(fs); 99 | } 100 | return l; 101 | } 102 | 103 | /** 104 | * 是否月斋 105 | * 106 | * @return true/false 107 | */ 108 | public boolean isMonthZhai() { 109 | int m = getMonth(); 110 | return 1 == m || 5 == m || 9 == m; 111 | } 112 | 113 | /** 114 | * 是否杨公忌 115 | * 116 | * @return true/false 117 | */ 118 | public boolean isDayYangGong() { 119 | for (FotoFestival f : getFestivals()) { 120 | if ("杨公忌".equals(f.getName())) { 121 | return true; 122 | } 123 | } 124 | return false; 125 | } 126 | 127 | /** 128 | * 是否朔望斋 129 | * 130 | * @return true/false 131 | */ 132 | public boolean isDayZhaiShuoWang() { 133 | int d = getDay(); 134 | return 1 == d || 15 == d; 135 | } 136 | 137 | /** 138 | * 是否六斋日 139 | * 140 | * @return true/false 141 | */ 142 | public boolean isDayZhaiSix() { 143 | int d = getDay(); 144 | if (8 == d || 14 == d || 15 == d || 23 == d || 29 == d || 30 == d) { 145 | return true; 146 | } else if (28 == d) { 147 | LunarMonth m = LunarMonth.fromYm(lunar.getYear(), getMonth()); 148 | return null != m && 30 != m.getDayCount(); 149 | } 150 | return false; 151 | } 152 | 153 | /** 154 | * 是否十斋日 155 | * 156 | * @return true/false 157 | */ 158 | public boolean isDayZhaiTen() { 159 | int d = getDay(); 160 | return 1 == d || 8 == d || 14 == d || 15 == d || 18 == d || 23 == d || 24 == d || 28 == d || 29 == d || 30 == d; 161 | } 162 | 163 | /** 164 | * 是否观音斋 165 | * 166 | * @return true/false 167 | */ 168 | public boolean isDayZhaiGuanYin() { 169 | String k = getMonth() + "-" + getDay(); 170 | for (String d : FotoUtil.DAY_ZHAI_GUAN_YIN) { 171 | if (k.equals(d)) { 172 | return true; 173 | } 174 | } 175 | return false; 176 | } 177 | 178 | /** 179 | * 获取星宿 180 | * 181 | * @return 星宿 182 | */ 183 | public String getXiu() { 184 | return FotoUtil.getXiu(getMonth(), getDay()); 185 | } 186 | 187 | /** 188 | * 获取宿吉凶 189 | * 190 | * @return 吉/凶 191 | */ 192 | public String getXiuLuck() { 193 | return LunarUtil.XIU_LUCK.get(getXiu()); 194 | } 195 | 196 | /** 197 | * 获取宿歌诀 198 | * 199 | * @return 宿歌诀 200 | */ 201 | public String getXiuSong() { 202 | return LunarUtil.XIU_SONG.get(getXiu()); 203 | } 204 | 205 | /** 206 | * 获取政 207 | * 208 | * @return 政 209 | */ 210 | public String getZheng() { 211 | return LunarUtil.ZHENG.get(getXiu()); 212 | } 213 | 214 | /** 215 | * 获取动物 216 | * 217 | * @return 动物 218 | */ 219 | public String getAnimal() { 220 | return LunarUtil.ANIMAL.get(getXiu()); 221 | } 222 | 223 | /** 224 | * 获取宫 225 | * 226 | * @return 宫 227 | */ 228 | public String getGong() { 229 | return LunarUtil.GONG.get(getXiu()); 230 | } 231 | 232 | /** 233 | * 获取兽 234 | * 235 | * @return 兽 236 | */ 237 | public String getShou() { 238 | return LunarUtil.SHOU.get(getGong()); 239 | } 240 | 241 | @Override 242 | public String toString() { 243 | return getYearInChinese() + "年" + getMonthInChinese() + "月" + getDayInChinese(); 244 | } 245 | 246 | public String toFullString() { 247 | StringBuilder s = new StringBuilder(); 248 | s.append(this); 249 | for (FotoFestival f : getFestivals()) { 250 | s.append(" ("); 251 | s.append(f); 252 | s.append(")"); 253 | } 254 | return s.toString(); 255 | } 256 | 257 | } 258 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/FotoFestival.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | /** 4 | * 佛历因果犯忌 5 | * 6 | * @author 6tail 7 | */ 8 | public class FotoFestival { 9 | 10 | /** 11 | * 是日何日,如:雷斋日 12 | */ 13 | private final String name; 14 | 15 | /** 16 | * 犯之因果,如:犯者夺纪 17 | */ 18 | private final String result; 19 | 20 | /** 21 | * 是否每月同 22 | */ 23 | private final boolean everyMonth; 24 | 25 | /** 26 | * 备注,如:宜先一日即戒 27 | */ 28 | private final String remark; 29 | 30 | public FotoFestival(String name, String result, boolean everyMonth, String remark) { 31 | this.name = name; 32 | this.result = null == result ? "" : result; 33 | this.everyMonth = everyMonth; 34 | this.remark = null == remark ? "" : remark; 35 | } 36 | 37 | public FotoFestival(String name) { 38 | this(name, null); 39 | } 40 | 41 | public FotoFestival(String name, String result) { 42 | this(name, result, false); 43 | } 44 | 45 | public FotoFestival(String name, String result, boolean everyMonth) { 46 | this(name, result, everyMonth, null); 47 | } 48 | 49 | public String getName() { 50 | return name; 51 | } 52 | 53 | public String getResult() { 54 | return result; 55 | } 56 | 57 | public boolean isEveryMonth() { 58 | return everyMonth; 59 | } 60 | 61 | public String getRemark() { 62 | return remark; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return name; 68 | } 69 | 70 | public String toFullString() { 71 | StringBuilder s = new StringBuilder(); 72 | s.append(name); 73 | if (null != result && result.length() > 0) { 74 | s.append(" "); 75 | s.append(result); 76 | } 77 | if (null != remark && remark.length() > 0) { 78 | s.append(" "); 79 | s.append(remark); 80 | } 81 | return s.toString(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/Fu.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | /** 4 | * 三伏 5 | *

从夏至后第3个庚日算起,初伏为10天,中伏为10天或20天,末伏为10天。当夏至与立秋之间出现4个庚日时中伏为10天,出现5个庚日则为20天。

6 | * 7 | * @author 6tail 8 | */ 9 | public class Fu { 10 | /** 11 | * 名称:初伏、中伏、末伏 12 | */ 13 | private String name; 14 | 15 | /** 16 | * 当前入伏第几天,1-20 17 | */ 18 | private int index; 19 | 20 | 21 | public Fu() { 22 | } 23 | 24 | public Fu(String name, int index) { 25 | this.name = name; 26 | this.index = index; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | public int getIndex() { 38 | return index; 39 | } 40 | 41 | public void setIndex(int index) { 42 | this.index = index; 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return name; 48 | } 49 | 50 | public String toFullString() { 51 | return name + "第" + index + "天"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/Holiday.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | /** 4 | * 节假日 5 | * 6 | * @author 6tail 7 | */ 8 | public class Holiday { 9 | 10 | /** 日期,YYYY-MM-DD格式 */ 11 | private String day; 12 | 13 | /** 名称,如:国庆 */ 14 | private String name; 15 | 16 | /** 是否调休,即是否要上班 */ 17 | private boolean work; 18 | 19 | /** 关联的节日,YYYY-MM-DD格式 */ 20 | private String target; 21 | 22 | public Holiday(){} 23 | 24 | /** 25 | * 初始化 26 | * @param day 日期 27 | * @param name 名称 28 | * @param work 是否调休 29 | * @param target 关联的节日 30 | */ 31 | public Holiday(String day,String name,boolean work,String target){ 32 | if(!day.contains("-")){ 33 | this.day = day.substring(0,4)+"-"+day.substring(4,6)+"-"+day.substring(6); 34 | }else { 35 | this.day = day; 36 | } 37 | this.name = name; 38 | this.work = work; 39 | if(!target.contains("-")){ 40 | this.target = target.substring(0,4)+"-"+target.substring(4,6)+"-"+target.substring(6); 41 | }else { 42 | this.target = target; 43 | } 44 | } 45 | 46 | /** 47 | * 获取日期 48 | * @return 日期 49 | */ 50 | public String getDay() { 51 | return day; 52 | } 53 | 54 | /** 55 | * 设置日期 56 | * @param day 日期 57 | */ 58 | public void setDay(String day) { 59 | this.day = day; 60 | } 61 | 62 | /** 63 | * 获取名称 64 | * @return 名称 65 | */ 66 | public String getName() { 67 | return name; 68 | } 69 | 70 | /** 71 | * 设置名称 72 | * @param name 名称 73 | */ 74 | public void setName(String name) { 75 | this.name = name; 76 | } 77 | 78 | /** 79 | * 是否调休 80 | * @return true/false 81 | */ 82 | public boolean isWork() { 83 | return work; 84 | } 85 | 86 | /** 87 | * 设置是否调休 88 | * @param work true/false 89 | */ 90 | public void setWork(boolean work) { 91 | this.work = work; 92 | } 93 | 94 | /** 95 | * 获取关联的节日 96 | * @return 节日 97 | */ 98 | public String getTarget() { 99 | return target; 100 | } 101 | 102 | /** 103 | * 设置关联的节日 104 | * @param target 节日 105 | */ 106 | public void setTarget(String target) { 107 | this.target = target; 108 | } 109 | 110 | @Override 111 | public String toString(){ 112 | return day+" "+name+(work?"调休":"")+" "+target; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/JieQi.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | /** 4 | * 节气 5 | * 6 | * @author 6tail 7 | */ 8 | public class JieQi { 9 | 10 | /** 名称 */ 11 | private String name; 12 | 13 | /** 阳历日期 */ 14 | private Solar solar; 15 | 16 | /** 是否节令 */ 17 | private boolean jie; 18 | 19 | /** 是否气令 */ 20 | private boolean qi; 21 | 22 | public JieQi() { 23 | } 24 | 25 | /** 26 | * 初始化 27 | * @param name 名称 28 | * @param solar 阳历日期 29 | */ 30 | public JieQi(String name, Solar solar) { 31 | setName(name); 32 | this.solar = solar; 33 | } 34 | 35 | /** 36 | * 获取名称 37 | * @return 名称 38 | */ 39 | public String getName() { 40 | return name; 41 | } 42 | 43 | /** 44 | * 设置名称 45 | * @param name 名称 46 | */ 47 | public void setName(String name) { 48 | this.name = name; 49 | for(int i=0,j=Lunar.JIE_QI.length;i 0) { 241 | int rest = n; 242 | int ny = year; 243 | int iy = ny; 244 | int im = month; 245 | int index = 0; 246 | List months = LunarYear.fromYear(ny).getMonths(); 247 | while (true) { 248 | int size = months.size(); 249 | for (int i = 0; i < size; i++) { 250 | LunarMonth m = months.get(i); 251 | if (m.getYear() == iy && m.getMonth() == im) { 252 | index = i; 253 | break; 254 | } 255 | } 256 | int more = size - index - 1; 257 | if (rest < more) { 258 | break; 259 | } 260 | rest -= more; 261 | LunarMonth lastMonth = months.get(size - 1); 262 | iy = lastMonth.getYear(); 263 | im = lastMonth.getMonth(); 264 | ny++; 265 | months = LunarYear.fromYear(ny).getMonths(); 266 | } 267 | return months.get(index + rest); 268 | } else { 269 | int rest = -n; 270 | int ny = year; 271 | int iy = ny; 272 | int im = month; 273 | int index = 0; 274 | List months = LunarYear.fromYear(ny).getMonths(); 275 | while (true) { 276 | int size = months.size(); 277 | for (int i = 0; i < size; i++) { 278 | LunarMonth m = months.get(i); 279 | if (m.getYear() == iy && m.getMonth() == im) { 280 | index = i; 281 | break; 282 | } 283 | } 284 | if (rest <= index) { 285 | break; 286 | } 287 | rest -= index; 288 | LunarMonth firstMonth = months.get(0); 289 | iy = firstMonth.getYear(); 290 | im = firstMonth.getMonth(); 291 | ny--; 292 | months = LunarYear.fromYear(ny).getMonths(); 293 | } 294 | return months.get(index - rest); 295 | } 296 | } 297 | 298 | } 299 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/LunarTime.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | import com.nlf.calendar.util.LunarUtil; 4 | 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | /** 9 | * 时辰 10 | * 11 | * @author 6tail 12 | */ 13 | public class LunarTime { 14 | 15 | /** 16 | * 天干下标,0-9 17 | */ 18 | private final int ganIndex; 19 | 20 | /** 21 | * 地支下标,0-11 22 | */ 23 | private final int zhiIndex; 24 | 25 | /** 26 | * 阴历 27 | */ 28 | private final Lunar lunar; 29 | 30 | public LunarTime(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute, int second) { 31 | this.lunar = Lunar.fromYmdHms(lunarYear, lunarMonth, lunarDay, hour, minute, second); 32 | this.zhiIndex = LunarUtil.getTimeZhiIndex(String.format("%02d:%02d", hour, minute)); 33 | this.ganIndex = (lunar.getDayGanIndexExact() % 5 * 2 + zhiIndex) % 10; 34 | } 35 | 36 | public static LunarTime fromYmdHms(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute, int second) { 37 | return new LunarTime(lunarYear, lunarMonth, lunarDay, hour, minute, second); 38 | } 39 | 40 | public int getGanIndex() { 41 | return ganIndex; 42 | } 43 | 44 | public int getZhiIndex() { 45 | return zhiIndex; 46 | } 47 | 48 | /** 49 | * 获取生肖 50 | * 51 | * @return 生肖,如虎 52 | */ 53 | public String getShengXiao() { 54 | return LunarUtil.SHENGXIAO[zhiIndex + 1]; 55 | } 56 | 57 | /** 58 | * 获取地支 59 | * 60 | * @return 地支 61 | */ 62 | public String getZhi() { 63 | return LunarUtil.ZHI[zhiIndex + 1]; 64 | } 65 | 66 | /** 67 | * 获取天干 68 | * 69 | * @return 天干 70 | */ 71 | public String getGan() { 72 | return LunarUtil.GAN[ganIndex + 1]; 73 | } 74 | 75 | /** 76 | * 获取干支(时柱) 77 | * 78 | * @return 干支(时柱) 79 | */ 80 | public String getGanZhi() { 81 | return getGan() + getZhi(); 82 | } 83 | 84 | /** 85 | * 获取喜神方位 86 | * 87 | * @return 喜神方位,如艮 88 | */ 89 | public String getPositionXi() { 90 | return LunarUtil.POSITION_XI[ganIndex + 1]; 91 | } 92 | 93 | /** 94 | * 获取喜神方位描述 95 | * 96 | * @return 喜神方位描述,如东北 97 | */ 98 | public String getPositionXiDesc() { 99 | return LunarUtil.POSITION_DESC.get(getPositionXi()); 100 | } 101 | 102 | /** 103 | * 获取阳贵神方位 104 | * 105 | * @return 阳贵神方位,如艮 106 | */ 107 | public String getPositionYangGui() { 108 | return LunarUtil.POSITION_YANG_GUI[ganIndex + 1]; 109 | } 110 | 111 | /** 112 | * 获取阳贵神方位描述 113 | * 114 | * @return 阳贵神方位描述,如东北 115 | */ 116 | public String getPositionYangGuiDesc() { 117 | return LunarUtil.POSITION_DESC.get(getPositionYangGui()); 118 | } 119 | 120 | /** 121 | * 获取阴贵神方位 122 | * 123 | * @return 阴贵神方位,如艮 124 | */ 125 | public String getPositionYinGui() { 126 | return LunarUtil.POSITION_YIN_GUI[ganIndex + 1]; 127 | } 128 | 129 | /** 130 | * 获取阴贵神方位描述 131 | * 132 | * @return 阴贵神方位描述,如东北 133 | */ 134 | public String getPositionYinGuiDesc() { 135 | return LunarUtil.POSITION_DESC.get(getPositionYinGui()); 136 | } 137 | 138 | /** 139 | * 获取福神方位(默认流派:2) 140 | * 141 | * @return 福神方位,如艮 142 | */ 143 | public String getPositionFu() { 144 | return getPositionFu(2); 145 | } 146 | 147 | /** 148 | * 获取福神方位 149 | * 150 | * @param sect 流派,1或2 151 | * @return 福神方位,如艮 152 | */ 153 | public String getPositionFu(int sect) { 154 | return (1 == sect ? LunarUtil.POSITION_FU : LunarUtil.POSITION_FU_2)[ganIndex + 1]; 155 | } 156 | 157 | /** 158 | * 获取福神方位描述(默认流派:2) 159 | * 160 | * @return 福神方位描述,如东北 161 | */ 162 | public String getPositionFuDesc() { 163 | return getPositionFuDesc(2); 164 | } 165 | 166 | /** 167 | * 获取福神方位描述 168 | * 169 | * @param sect 流派,1或2 170 | * @return 福神方位描述,如东北 171 | */ 172 | public String getPositionFuDesc(int sect) { 173 | return LunarUtil.POSITION_DESC.get(getPositionFu(sect)); 174 | } 175 | 176 | /** 177 | * 获取财神方位 178 | * 179 | * @return 财神方位,如艮 180 | */ 181 | public String getPositionCai() { 182 | return LunarUtil.POSITION_CAI[ganIndex + 1]; 183 | } 184 | 185 | /** 186 | * 获取财神方位描述 187 | * 188 | * @return 财神方位描述,如东北 189 | */ 190 | public String getPositionCaiDesc() { 191 | return LunarUtil.POSITION_DESC.get(getPositionCai()); 192 | } 193 | 194 | /** 195 | * 获取纳音 196 | * 197 | * @return 纳音,如剑锋金 198 | */ 199 | public String getNaYin() { 200 | return LunarUtil.NAYIN.get(getGanZhi()); 201 | } 202 | 203 | /** 204 | * 获取值时天神 205 | * 206 | * @return 值时天神 207 | */ 208 | public String getTianShen() { 209 | return LunarUtil.TIAN_SHEN[(zhiIndex + LunarUtil.ZHI_TIAN_SHEN_OFFSET.get(lunar.getDayZhiExact())) % 12 + 1]; 210 | } 211 | 212 | /** 213 | * 获取值时天神类型:黄道/黑道 214 | * 215 | * @return 值时天神类型:黄道/黑道 216 | */ 217 | public String getTianShenType() { 218 | return LunarUtil.TIAN_SHEN_TYPE.get(getTianShen()); 219 | } 220 | 221 | /** 222 | * 获取值时天神吉凶 223 | * 224 | * @return 吉/凶 225 | */ 226 | public String getTianShenLuck() { 227 | return LunarUtil.TIAN_SHEN_TYPE_LUCK.get(getTianShenType()); 228 | } 229 | 230 | /** 231 | * 获取时冲 232 | * 233 | * @return 时冲,如申 234 | */ 235 | public String getChong() { 236 | return LunarUtil.CHONG[zhiIndex]; 237 | } 238 | 239 | /** 240 | * 获取时煞 241 | * 242 | * @return 时煞,如北 243 | */ 244 | public String getSha() { 245 | return LunarUtil.SHA.get(getZhi()); 246 | } 247 | 248 | /** 249 | * 获取时冲生肖 250 | * 251 | * @return 时冲生肖,如猴 252 | */ 253 | public String getChongShengXiao() { 254 | String chong = getChong(); 255 | for (int i = 0, j = LunarUtil.ZHI.length; i < j; i++) { 256 | if (LunarUtil.ZHI[i].equals(chong)) { 257 | return LunarUtil.SHENGXIAO[i]; 258 | } 259 | } 260 | return ""; 261 | } 262 | 263 | /** 264 | * 获取时冲描述 265 | * 266 | * @return 时冲描述,如(壬申)猴 267 | */ 268 | public String getChongDesc() { 269 | return "(" + getChongGan() + getChong() + ")" + getChongShengXiao(); 270 | } 271 | 272 | /** 273 | * 获取无情之克的时冲天干 274 | * 275 | * @return 无情之克的时冲天干,如甲 276 | */ 277 | public String getChongGan() { 278 | return LunarUtil.CHONG_GAN[ganIndex]; 279 | } 280 | 281 | /** 282 | * 获取有情之克的时冲天干 283 | * 284 | * @return 有情之克的时冲天干,如甲 285 | */ 286 | public String getChongGanTie() { 287 | return LunarUtil.CHONG_GAN_TIE[ganIndex]; 288 | } 289 | 290 | /** 291 | * 获取宜,如果没有,返回["无"] 292 | * 293 | * @return 宜 294 | */ 295 | public List getYi() { 296 | return LunarUtil.getTimeYi(lunar.getDayInGanZhiExact(), getGanZhi()); 297 | } 298 | 299 | /** 300 | * 获取忌,如果没有,返回["无"] 301 | * 302 | * @return 忌 303 | */ 304 | public List getJi() { 305 | return LunarUtil.getTimeJi(lunar.getDayInGanZhiExact(), getGanZhi()); 306 | } 307 | 308 | /** 309 | * 获取值时九星(时家紫白星歌诀:三元时白最为佳,冬至阳生顺莫差,孟日七宫仲一白,季日四绿发萌芽,每把时辰起甲子,本时星耀照光华,时星移入中宫去,顺飞八方逐细查。夏至阴生逆回首,孟归三碧季加六,仲在九宫时起甲,依然掌中逆轮跨。) 310 | * 311 | * @return 值时九星 312 | */ 313 | public NineStar getNineStar() { 314 | //顺逆 315 | String solarYmd = lunar.getSolar().toYmd(); 316 | Map jieQi = lunar.getJieQiTable(); 317 | boolean asc = solarYmd.compareTo(jieQi.get("冬至").toYmd()) >= 0 && solarYmd.compareTo(jieQi.get("夏至").toYmd()) < 0; 318 | int start = asc ? 7 : 3; 319 | String dayZhi = lunar.getDayZhi(); 320 | if ("子午卯酉".contains(dayZhi)) { 321 | start = asc ? 1 : 9; 322 | } else if ("辰戌丑未".contains(dayZhi)) { 323 | start = asc ? 4 : 6; 324 | } 325 | int index = asc ? start + zhiIndex - 1 : start - zhiIndex - 1; 326 | if (index > 8) { 327 | index -= 9; 328 | } 329 | if (index < 0) { 330 | index += 9; 331 | } 332 | return new NineStar(index); 333 | } 334 | 335 | /** 336 | * 获取所在旬 337 | * 338 | * @return 旬 339 | */ 340 | public String getXun() { 341 | return LunarUtil.getXun(getGanZhi()); 342 | } 343 | 344 | /** 345 | * 获取值时空亡 346 | * 347 | * @return 空亡(旬空) 348 | */ 349 | public String getXunKong() { 350 | return LunarUtil.getXunKong(getGanZhi()); 351 | } 352 | 353 | /** 354 | * 获取当前时辰的最早时分 355 | * 356 | * @return 时分,如:21:00 357 | */ 358 | public String getMinHm() { 359 | int hour = lunar.getHour(); 360 | if (hour < 1) { 361 | return "00:00"; 362 | } else if (hour > 22) { 363 | return "23:00"; 364 | } 365 | return String.format("%02d:00", hour % 2 == 0 ? hour - 1 : hour); 366 | } 367 | 368 | /** 369 | * 获取当前时辰的最晚时分 370 | * 371 | * @return 时分,如:22:59 372 | */ 373 | public String getMaxHm() { 374 | int hour = lunar.getHour(); 375 | if (hour < 1) { 376 | return "00:59"; 377 | } else if (hour > 22) { 378 | return "23:59"; 379 | } 380 | return String.format("%02d:59", hour % 2 == 0 ? hour : hour + 1); 381 | } 382 | 383 | @Override 384 | public String toString() { 385 | return getGanZhi(); 386 | } 387 | 388 | } 389 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/NineStar.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | import com.nlf.calendar.util.LunarUtil; 4 | 5 | /** 6 | * 九星 7 | *

玄空九星、奇门九星都来源于北斗九星,九数、七色、五行、后天八卦方位都是相通的。

8 | * 9 | * @author 6tail 10 | */ 11 | public class NineStar { 12 | 13 | /** 14 | * 九数 15 | */ 16 | public static final String[] NUMBER = {"一", "二", "三", "四", "五", "六", "七", "八", "九"}; 17 | 18 | /** 19 | * 七色 20 | */ 21 | public static final String[] COLOR = {"白", "黑", "碧", "绿", "黄", "白", "赤", "白", "紫"}; 22 | 23 | /** 24 | * 五行 25 | */ 26 | public static final String[] WU_XING = {"水", "土", "木", "木", "土", "金", "金", "土", "火"}; 27 | 28 | /** 29 | * 后天八卦方位 30 | */ 31 | public static final String[] POSITION = {"坎", "坤", "震", "巽", "中", "乾", "兑", "艮", "离"}; 32 | 33 | /** 34 | * 北斗九星 35 | */ 36 | public static final String[] NAME_BEI_DOU = {"天枢", "天璇", "天玑", "天权", "玉衡", "开阳", "摇光", "洞明", "隐元"}; 37 | 38 | /** 39 | * 玄空九星(玄空风水) 40 | */ 41 | public static final String[] NAME_XUAN_KONG = {"贪狼", "巨门", "禄存", "文曲", "廉贞", "武曲", "破军", "左辅", "右弼"}; 42 | 43 | /** 44 | * 奇门九星(奇门遁甲,也称天盘九星) 45 | */ 46 | public static final String[] NAME_QI_MEN = {"天蓬", "天芮", "天冲", "天辅", "天禽", "天心", "天柱", "天任", "天英"}; 47 | 48 | /** 49 | * 八门(奇门遁甲) 50 | */ 51 | public static final String[] BA_MEN_QI_MEN = {"休", "死", "伤", "杜", "", "开", "惊", "生", "景"}; 52 | 53 | /** 54 | * 太乙九神(太乙神数) 55 | */ 56 | public static final String[] NAME_TAI_YI = {"太乙", "摄提", "轩辕", "招摇", "天符", "青龙", "咸池", "太阴", "天乙"}; 57 | 58 | /** 59 | * 太乙九神对应类型 60 | */ 61 | public static final String[] TYPE_TAI_YI = {"吉神", "凶神", "安神", "安神", "凶神", "吉神", "凶神", "吉神", "吉神"}; 62 | 63 | /** 64 | * 太乙九神歌诀(太乙神数) 65 | */ 66 | public static final String[] SONG_TAI_YI = {"门中太乙明,星官号贪狼,赌彩财喜旺,婚姻大吉昌,出入无阻挡,参谒见贤良,此行三五里,黑衣别阴阳。", "门前见摄提,百事必忧疑,相生犹自可,相克祸必临,死门并相会,老妇哭悲啼,求谋并吉事,尽皆不相宜,只可藏隐遁,若动伤身疾。", "出入会轩辕,凡事必缠牵,相生全不美,相克更忧煎,远行多不利,博彩尽输钱,九天玄女法,句句不虚言。", "招摇号木星,当之事莫行,相克行人阻,阴人口舌迎,梦寐多惊惧,屋响斧自鸣,阴阳消息理,万法弗违情。", "五鬼为天符,当门阴女谋,相克无好事,行路阻中途,走失难寻觅,道逢有尼姑,此星当门值,万事有灾除。", "神光跃青龙,财气喜重重,投入有酒食,赌彩最兴隆,更逢相生旺,休言克破凶,见贵安营寨,万事总吉同。", "吾将为咸池,当之尽不宜,出入多不利,相克有灾情,赌彩全输尽,求财空手回,仙人真妙语,愚人莫与知,动用虚惊退,反复逆风吹。", "坐临太阴星,百祸不相侵,求谋悉成就,知交有觅寻,回风归来路,恐有殃伏起,密语中记取,慎乎莫轻行。", "迎来天乙星,相逢百事兴,运用和合庆,茶酒喜相迎,求谋并嫁娶,好合有天成,祸福如神验,吉凶甚分明。"}; 67 | 68 | /** 69 | * 吉凶(玄空风水) 70 | */ 71 | public static final String[] LUCK_XUAN_KONG = {"吉", "凶", "凶", "吉", "凶", "吉", "凶", "吉", "吉"}; 72 | 73 | /** 74 | * 吉凶(奇门遁甲) 75 | */ 76 | public static final String[] LUCK_QI_MEN = {"大凶", "大凶", "小吉", "大吉", "大吉", "大吉", "小凶", "小吉", "小凶"}; 77 | 78 | /** 79 | * 阴阳(奇门遁甲) 80 | */ 81 | public static final String[] YIN_YANG_QI_MEN = {"阳", "阴", "阳", "阳", "阳", "阴", "阴", "阳", "阴"}; 82 | 83 | /** 84 | * 序号,0到8 85 | */ 86 | protected int index; 87 | 88 | public NineStar(int index) { 89 | this.index = index; 90 | } 91 | 92 | public static NineStar fromIndex(int index) { 93 | return new NineStar(index); 94 | } 95 | 96 | /** 97 | * 获取九数 98 | * 99 | * @return 九数 100 | */ 101 | public String getNumber() { 102 | return NUMBER[index]; 103 | } 104 | 105 | /** 106 | * 获取七色 107 | * 108 | * @return 七色 109 | */ 110 | public String getColor() { 111 | return COLOR[index]; 112 | } 113 | 114 | /** 115 | * 获取五行 116 | * 117 | * @return 五行 118 | */ 119 | public String getWuXing() { 120 | return WU_XING[index]; 121 | } 122 | 123 | 124 | /** 125 | * 获取方位 126 | * 127 | * @return 方位 128 | */ 129 | public String getPosition() { 130 | return POSITION[index]; 131 | } 132 | 133 | /** 134 | * 获取方位描述 135 | * 136 | * @return 方位描述 137 | */ 138 | public String getPositionDesc() { 139 | return LunarUtil.POSITION_DESC.get(getPosition()); 140 | } 141 | 142 | /** 143 | * 获取玄空九星名称 144 | * 145 | * @return 玄空九星名称 146 | */ 147 | public String getNameInXuanKong() { 148 | return NAME_XUAN_KONG[index]; 149 | } 150 | 151 | /** 152 | * 获取北斗九星名称 153 | * 154 | * @return 北斗九星名称 155 | */ 156 | public String getNameInBeiDou() { 157 | return NAME_BEI_DOU[index]; 158 | } 159 | 160 | /** 161 | * 获取奇门九星名称 162 | * 163 | * @return 奇门九星名称 164 | */ 165 | public String getNameInQiMen() { 166 | return NAME_QI_MEN[index]; 167 | } 168 | 169 | /** 170 | * 获取太乙九神名称 171 | * 172 | * @return 太乙九神名称 173 | */ 174 | public String getNameInTaiYi() { 175 | return NAME_TAI_YI[index]; 176 | } 177 | 178 | /** 179 | * 获取奇门九星吉凶 180 | * 181 | * @return 大吉/小吉/大凶/小凶 182 | */ 183 | public String getLuckInQiMen() { 184 | return LUCK_QI_MEN[index]; 185 | } 186 | 187 | /** 188 | * 获取玄空九星吉凶 189 | * 190 | * @return 吉/凶 191 | */ 192 | public String getLuckInXuanKong() { 193 | return LUCK_XUAN_KONG[index]; 194 | } 195 | 196 | /** 197 | * 获取奇门九星阴阳 198 | * 199 | * @return 阴/阳 200 | */ 201 | public String getYinYangInQiMen() { 202 | return YIN_YANG_QI_MEN[index]; 203 | } 204 | 205 | /** 206 | * 获取太乙九神类型 207 | * 208 | * @return 吉神/凶神/安神 209 | */ 210 | public String getTypeInTaiYi() { 211 | return TYPE_TAI_YI[index]; 212 | } 213 | 214 | /** 215 | * 获取八门(奇门遁甲) 216 | * 217 | * @return 八门 218 | */ 219 | public String getBaMenInQiMen() { 220 | return BA_MEN_QI_MEN[index]; 221 | } 222 | 223 | /** 224 | * 获取太乙九神歌诀 225 | * 226 | * @return 太乙九神歌诀 227 | */ 228 | public String getSongInTaiYi() { 229 | return SONG_TAI_YI[index]; 230 | } 231 | 232 | /** 233 | * 获取九星序号,从0开始 234 | * 235 | * @return 序号 236 | */ 237 | public int getIndex() { 238 | return index; 239 | } 240 | 241 | @Override 242 | public String toString() { 243 | return getNumber() + getColor() + getWuXing() + getNameInBeiDou(); 244 | } 245 | 246 | /** 247 | * 获取详细描述 248 | * 249 | * @return 详细描述 250 | */ 251 | public String toFullString() { 252 | StringBuilder s = new StringBuilder(); 253 | s.append(getNumber()); 254 | s.append(getColor()); 255 | s.append(getWuXing()); 256 | s.append(" "); 257 | s.append(getPosition()); 258 | s.append("("); 259 | s.append(getPositionDesc()); 260 | s.append(") "); 261 | s.append(getNameInBeiDou()); 262 | s.append(" 玄空["); 263 | s.append(getNameInXuanKong()); 264 | s.append(" "); 265 | s.append(getLuckInXuanKong()); 266 | s.append("] 奇门["); 267 | s.append(getNameInQiMen()); 268 | s.append(" "); 269 | s.append(getLuckInQiMen()); 270 | if (getBaMenInQiMen().length() > 0) { 271 | s.append(" "); 272 | s.append(getBaMenInQiMen()); 273 | s.append("门"); 274 | } 275 | s.append(" "); 276 | s.append(getYinYangInQiMen()); 277 | s.append("] 太乙["); 278 | s.append(getNameInTaiYi()); 279 | s.append(" "); 280 | s.append(getTypeInTaiYi()); 281 | s.append("]"); 282 | return s.toString(); 283 | } 284 | } 285 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/ShuJiu.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | /** 4 | * 数九 5 | * @author 6tail 6 | */ 7 | public class ShuJiu { 8 | 9 | /** 10 | * 名称,如一九、二九 11 | */ 12 | private String name; 13 | 14 | /** 15 | * 当前数九第几天,1-9 16 | */ 17 | private int index; 18 | 19 | public ShuJiu() { 20 | } 21 | 22 | public ShuJiu(String name, int index) { 23 | this.name = name; 24 | this.index = index; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public int getIndex() { 36 | return index; 37 | } 38 | 39 | public void setIndex(int index) { 40 | this.index = index; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return name; 46 | } 47 | 48 | public String toFullString() { 49 | return name + "第" + index + "天"; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/SolarHalfYear.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | /** 9 | * 阳历半年 10 | * 11 | * @author 6tail 12 | */ 13 | public class SolarHalfYear { 14 | /** 15 | * 年 16 | */ 17 | private final int year; 18 | /** 19 | * 月 20 | */ 21 | private final int month; 22 | 23 | /** 24 | * 半年的月数 25 | */ 26 | public static final int MONTH_COUNT = 6; 27 | 28 | /** 29 | * 默认当月 30 | */ 31 | public SolarHalfYear() { 32 | this(new Date()); 33 | } 34 | 35 | /** 36 | * 通过日期初始化 37 | */ 38 | public SolarHalfYear(Date date) { 39 | Solar solar = Solar.fromDate(date); 40 | year = solar.getYear(); 41 | month = solar.getMonth(); 42 | } 43 | 44 | /** 45 | * 通过日历初始化 46 | */ 47 | @Deprecated 48 | public SolarHalfYear(Calendar calendar) { 49 | year = calendar.get(Calendar.YEAR); 50 | month = calendar.get(Calendar.MONTH) + 1; 51 | } 52 | 53 | /** 54 | * 通过年月初始化 55 | * 56 | * @param year 年 57 | * @param month 月 58 | */ 59 | public SolarHalfYear(int year, int month) { 60 | this.year = year; 61 | this.month = month; 62 | } 63 | 64 | /** 65 | * 通过指定日期获取阳历半年 66 | * 67 | * @param date 日期 68 | * @return 阳历半年 69 | */ 70 | public static SolarHalfYear fromDate(Date date) { 71 | return new SolarHalfYear(date); 72 | } 73 | 74 | /** 75 | * 通过指定日历获取阳历半年 76 | * 77 | * @param calendar 日历 78 | * @return 阳历半年 79 | */ 80 | @Deprecated 81 | public static SolarHalfYear fromCalendar(Calendar calendar) { 82 | return new SolarHalfYear(calendar); 83 | } 84 | 85 | /** 86 | * 通过指定年月获取阳历半年 87 | * 88 | * @param year 年 89 | * @param month 月 90 | * @return 阳历半年 91 | */ 92 | public static SolarHalfYear fromYm(int year, int month) { 93 | return new SolarHalfYear(year, month); 94 | } 95 | 96 | /** 97 | * 获取年 98 | * 99 | * @return 年 100 | */ 101 | public int getYear() { 102 | return year; 103 | } 104 | 105 | /** 106 | * 获取月 107 | * 108 | * @return 月 109 | */ 110 | public int getMonth() { 111 | return month; 112 | } 113 | 114 | /** 115 | * 获取当月是第几半年 116 | * 117 | * @return 半年序号,从1开始 118 | */ 119 | public int getIndex() { 120 | return (int) Math.ceil(month * 1D / MONTH_COUNT); 121 | } 122 | 123 | /** 124 | * 半年推移 125 | * 126 | * @param halfYears 推移的半年数,负数为倒推 127 | * @return 推移后的半年 128 | */ 129 | public SolarHalfYear next(int halfYears) { 130 | SolarMonth m = SolarMonth.fromYm(year, month).next(MONTH_COUNT * halfYears); 131 | return new SolarHalfYear(m.getYear(), m.getMonth()); 132 | } 133 | 134 | /** 135 | * 获取本半年的月份 136 | * 137 | * @return 本半年的月份列表 138 | */ 139 | public List getMonths() { 140 | List l = new ArrayList(); 141 | int index = getIndex() - 1; 142 | for (int i = 0; i < MONTH_COUNT; i++) { 143 | l.add(new SolarMonth(year, MONTH_COUNT * index + i + 1)); 144 | } 145 | return l; 146 | } 147 | 148 | @Override 149 | public String toString() { 150 | return year + "." + getIndex(); 151 | } 152 | 153 | public String toFullString() { 154 | return year + "年" + (getIndex() == 1 ? "上" : "下") + "半年"; 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/SolarMonth.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | import com.nlf.calendar.util.SolarUtil; 9 | 10 | /** 11 | * 阳历月 12 | * 13 | * @author 6tail 14 | */ 15 | public class SolarMonth { 16 | /** 17 | * 年 18 | */ 19 | private final int year; 20 | /** 21 | * 月 22 | */ 23 | private final int month; 24 | 25 | /** 26 | * 默认当月 27 | */ 28 | public SolarMonth() { 29 | this(new Date()); 30 | } 31 | 32 | /** 33 | * 通过日期初始化 34 | */ 35 | public SolarMonth(Date date) { 36 | Solar solar = Solar.fromDate(date); 37 | year = solar.getYear(); 38 | month = solar.getMonth(); 39 | } 40 | 41 | /** 42 | * 通过日历初始化 43 | */ 44 | @Deprecated 45 | public SolarMonth(Calendar calendar) { 46 | year = calendar.get(Calendar.YEAR); 47 | month = calendar.get(Calendar.MONTH) + 1; 48 | } 49 | 50 | /** 51 | * 通过年月初始化 52 | * 53 | * @param year 年 54 | * @param month 月 55 | */ 56 | public SolarMonth(int year, int month) { 57 | this.year = year; 58 | this.month = month; 59 | } 60 | 61 | /** 62 | * 通过指定日期获取阳历月 63 | * 64 | * @param date 日期 65 | * @return 阳历月 66 | */ 67 | public static SolarMonth fromDate(Date date) { 68 | return new SolarMonth(date); 69 | } 70 | 71 | /** 72 | * 通过指定日历获取阳历月 73 | * 74 | * @param calendar 日历 75 | * @return 阳历月 76 | */ 77 | @Deprecated 78 | public static SolarMonth fromCalendar(Calendar calendar) { 79 | return new SolarMonth(calendar); 80 | } 81 | 82 | /** 83 | * 通过指定年月获取阳历月 84 | * 85 | * @param year 年 86 | * @param month 月 87 | * @return 阳历月 88 | */ 89 | public static SolarMonth fromYm(int year, int month) { 90 | return new SolarMonth(year, month); 91 | } 92 | 93 | /** 94 | * 获取年 95 | * 96 | * @return 年 97 | */ 98 | public int getYear() { 99 | return year; 100 | } 101 | 102 | /** 103 | * 获取月 104 | * 105 | * @return 月 106 | */ 107 | public int getMonth() { 108 | return month; 109 | } 110 | 111 | /** 112 | * 获取本月的阳历日期列表 113 | * 114 | * @return 阳历日期列表 115 | */ 116 | public List getDays() { 117 | List l = new ArrayList(31); 118 | Solar d = new Solar(year, month, 1); 119 | l.add(d); 120 | int days = SolarUtil.getDaysOfMonth(year, month); 121 | for (int i = 1; i < days; i++) { 122 | l.add(d.next(i)); 123 | } 124 | return l; 125 | } 126 | 127 | /** 128 | * 获取本月的阳历周列表 129 | * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 130 | * @return 周列表 131 | */ 132 | public List getWeeks(int start) { 133 | List l = new ArrayList(); 134 | SolarWeek week = SolarWeek.fromYmd(year, month, 1, start); 135 | while (true) { 136 | l.add(week); 137 | week = week.next(1, false); 138 | Solar firstDay = week.getFirstDay(); 139 | if (firstDay.getYear() > year || firstDay.getMonth() > month) { 140 | break; 141 | } 142 | } 143 | return l; 144 | } 145 | 146 | /** 147 | * 获取往后推几个月的阳历月,如果要往前推,则月数用负数 148 | * 149 | * @param months 月数 150 | * @return 阳历月 151 | */ 152 | public SolarMonth next(int months) { 153 | int n = months < 0 ? -1 : 1; 154 | int m = Math.abs(months); 155 | int y = year + m / 12 * n; 156 | m = month + m % 12 * n; 157 | if (m > 12) { 158 | m -= 12; 159 | y++; 160 | } else if (m < 1) { 161 | m += 12; 162 | y--; 163 | } 164 | return new SolarMonth(y, m); 165 | } 166 | 167 | @Override 168 | public String toString() { 169 | return year + "-" + month; 170 | } 171 | 172 | public String toFullString() { 173 | return year + "年" + month + "月"; 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/SolarSeason.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | /** 9 | * 阳历季度 10 | * 11 | * @author 6tail 12 | */ 13 | public class SolarSeason { 14 | /** 15 | * 年 16 | */ 17 | private final int year; 18 | 19 | /** 20 | * 月 21 | */ 22 | private final int month; 23 | 24 | /** 25 | * 一个季度的月数 26 | */ 27 | public static final int MONTH_COUNT = 3; 28 | 29 | /** 30 | * 默认当月 31 | */ 32 | public SolarSeason() { 33 | this(new Date()); 34 | } 35 | 36 | /** 37 | * 通过日期初始化 38 | */ 39 | public SolarSeason(Date date) { 40 | Solar solar = Solar.fromDate(date); 41 | year = solar.getYear(); 42 | month = solar.getMonth(); 43 | } 44 | 45 | /** 46 | * 通过日历初始化 47 | */ 48 | @Deprecated 49 | public SolarSeason(Calendar calendar) { 50 | year = calendar.get(Calendar.YEAR); 51 | month = calendar.get(Calendar.MONTH) + 1; 52 | } 53 | 54 | /** 55 | * 通过年月初始化 56 | * 57 | * @param year 年 58 | * @param month 月 59 | */ 60 | public SolarSeason(int year, int month) { 61 | this.year = year; 62 | this.month = month; 63 | } 64 | 65 | /** 66 | * 通过指定日期获取阳历季度 67 | * 68 | * @param date 日期 69 | * @return 阳历季度 70 | */ 71 | public static SolarSeason fromDate(Date date) { 72 | return new SolarSeason(date); 73 | } 74 | 75 | /** 76 | * 通过指定日历获取阳历季度 77 | * 78 | * @param calendar 日历 79 | * @return 阳历季度 80 | */ 81 | @Deprecated 82 | public static SolarSeason fromCalendar(Calendar calendar) { 83 | return new SolarSeason(calendar); 84 | } 85 | 86 | /** 87 | * 通过指定年月获取阳历季度 88 | * 89 | * @param year 年 90 | * @param month 月 91 | * @return 阳历季度 92 | */ 93 | public static SolarSeason fromYm(int year, int month) { 94 | return new SolarSeason(year, month); 95 | } 96 | 97 | /** 98 | * 获取年 99 | * 100 | * @return 年 101 | */ 102 | public int getYear() { 103 | return year; 104 | } 105 | 106 | /** 107 | * 获取月 108 | * 109 | * @return 月 110 | */ 111 | public int getMonth() { 112 | return month; 113 | } 114 | 115 | /** 116 | * 获取当月是第几季度 117 | * 118 | * @return 季度序号,从1开始 119 | */ 120 | public int getIndex() { 121 | return (int) Math.ceil(month * 1D / MONTH_COUNT); 122 | } 123 | 124 | /** 125 | * 季度推移 126 | * 127 | * @param seasons 推移的季度数,负数为倒推 128 | * @return 推移后的季度 129 | */ 130 | public SolarSeason next(int seasons) { 131 | SolarMonth m = SolarMonth.fromYm(year, month).next(MONTH_COUNT * seasons); 132 | return new SolarSeason(m.getYear(), m.getMonth()); 133 | } 134 | 135 | /** 136 | * 获取本季度的月份 137 | * 138 | * @return 本季度的月份列表 139 | */ 140 | public List getMonths() { 141 | List l = new ArrayList(); 142 | int index = getIndex() - 1; 143 | for (int i = 0; i < MONTH_COUNT; i++) { 144 | l.add(new SolarMonth(year, MONTH_COUNT * index + i + 1)); 145 | } 146 | return l; 147 | } 148 | 149 | @Override 150 | public String toString() { 151 | return year + "." + getIndex(); 152 | } 153 | 154 | public String toFullString() { 155 | return year + "年" + getIndex() + "季度"; 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/SolarWeek.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | import com.nlf.calendar.util.SolarUtil; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Calendar; 7 | import java.util.Date; 8 | import java.util.List; 9 | 10 | /** 11 | * 阳历周 12 | * 13 | * @author 6tail 14 | */ 15 | public class SolarWeek { 16 | /** 17 | * 年 18 | */ 19 | private final int year; 20 | 21 | /** 22 | * 月 23 | */ 24 | private final int month; 25 | 26 | /** 27 | * 日 28 | */ 29 | private final int day; 30 | 31 | /** 32 | * 星期几作为一周的开始,1234560分别代表星期一至星期天 33 | */ 34 | private final int start; 35 | 36 | /** 37 | * 默认当月 38 | * 39 | * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 40 | */ 41 | public SolarWeek(int start) { 42 | this(new Date(), start); 43 | } 44 | 45 | /** 46 | * 通过日期初始化 47 | * 48 | * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 49 | */ 50 | public SolarWeek(Date date, int start) { 51 | Solar solar = Solar.fromDate(date); 52 | year = solar.getYear(); 53 | month = solar.getMonth(); 54 | day = solar.getDay(); 55 | this.start = start; 56 | } 57 | 58 | /** 59 | * 通过日历初始化 60 | * 61 | * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 62 | */ 63 | @Deprecated 64 | public SolarWeek(Calendar calendar, int start) { 65 | year = calendar.get(Calendar.YEAR); 66 | month = calendar.get(Calendar.MONTH) + 1; 67 | day = calendar.get(Calendar.DATE); 68 | this.start = start; 69 | } 70 | 71 | /** 72 | * 通过年月初始化 73 | * 74 | * @param year 年 75 | * @param month 月,1到12 76 | * @param day 日,1到31 77 | * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 78 | */ 79 | public SolarWeek(int year, int month, int day, int start) { 80 | this.year = year; 81 | this.month = month; 82 | this.day = day; 83 | this.start = start; 84 | } 85 | 86 | /** 87 | * 通过指定日期获取阳历周 88 | * 89 | * @param date 日期 90 | * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 91 | * @return 阳历周 92 | */ 93 | public static SolarWeek fromDate(Date date, int start) { 94 | return new SolarWeek(date, start); 95 | } 96 | 97 | /** 98 | * 通过指定日历获取阳历周 99 | * 100 | * @param calendar 日历 101 | * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 102 | * @return 阳历周 103 | */ 104 | @Deprecated 105 | public static SolarWeek fromCalendar(Calendar calendar, int start) { 106 | return new SolarWeek(calendar, start); 107 | } 108 | 109 | /** 110 | * 通过指定年月日获取阳历周 111 | * 112 | * @param year 年 113 | * @param month 月,1到12 114 | * @param day 日,1到31 115 | * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 116 | * @return 阳历周 117 | */ 118 | public static SolarWeek fromYmd(int year, int month, int day, int start) { 119 | return new SolarWeek(year, month, day, start); 120 | } 121 | 122 | /** 123 | * 获取年 124 | * 125 | * @return 年 126 | */ 127 | public int getYear() { 128 | return year; 129 | } 130 | 131 | /** 132 | * 获取月 133 | * 134 | * @return 1到12 135 | */ 136 | public int getMonth() { 137 | return month; 138 | } 139 | 140 | /** 141 | * 获取日期 142 | * 143 | * @return 1到31之间的数字 144 | */ 145 | public int getDay() { 146 | return day; 147 | } 148 | 149 | /** 150 | * 获取星期几作为一周的开始,1234560分别代表星期一至星期天 151 | * 152 | * @return 1234560分别代表星期一至星期天 153 | */ 154 | public int getStart() { 155 | return start; 156 | } 157 | 158 | /** 159 | * 获取当前日期是在当月第几周 160 | * 161 | * @return 周序号,从1开始 162 | */ 163 | public int getIndex() { 164 | int offset = Solar.fromYmd(year, month, 1).getWeek() - start; 165 | if(offset < 0) { 166 | offset += 7; 167 | } 168 | return (int) Math.ceil((day + offset) / 7D); 169 | } 170 | 171 | /** 172 | * 获取当前日期是在当年第几周 173 | * 174 | * @return 周序号,从1开始 175 | */ 176 | public int getIndexInYear() { 177 | int offset = Solar.fromYmd(year, 1, 1).getWeek() - start; 178 | if(offset < 0) { 179 | offset += 7; 180 | } 181 | return (int) Math.ceil((SolarUtil.getDaysInYear(year, month, day) + offset) / 7D); 182 | } 183 | 184 | /** 185 | * 周推移 186 | * 187 | * @param weeks 推移的周数,负数为倒推 188 | * @param separateMonth 是否按月单独计算 189 | * @return 推移后的阳历周 190 | */ 191 | public SolarWeek next(int weeks, boolean separateMonth) { 192 | if (0 == weeks) { 193 | return new SolarWeek(year, month, day, start); 194 | } 195 | Solar solar = Solar.fromYmd(year, month, day); 196 | if (separateMonth) { 197 | int n = weeks; 198 | SolarWeek week = new SolarWeek(solar.getYear(), solar.getMonth(), solar.getDay(), start); 199 | int month = this.month; 200 | boolean plus = n > 0; 201 | while (0 != n) { 202 | solar = solar.next(plus ? 7 : -7); 203 | week = new SolarWeek(solar.getYear(), solar.getMonth(), solar.getDay(), start); 204 | int weekMonth = week.getMonth(); 205 | if (month != weekMonth) { 206 | int index = week.getIndex(); 207 | if (plus) { 208 | if (1 == index) { 209 | Solar firstDay = week.getFirstDay(); 210 | week = new SolarWeek(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay(), start); 211 | weekMonth = week.getMonth(); 212 | } else { 213 | solar = Solar.fromYmd(week.getYear(), week.getMonth(), 1); 214 | week = new SolarWeek(solar.getYear(), solar.getMonth(), solar.getDay(), start); 215 | } 216 | } else { 217 | if (SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start) == index) { 218 | Solar lastDay = week.getFirstDay().next(6); 219 | week = new SolarWeek(lastDay.getYear(), lastDay.getMonth(), lastDay.getDay(), start); 220 | weekMonth = week.getMonth(); 221 | } else { 222 | solar = Solar.fromYmd(week.getYear(), week.getMonth(), SolarUtil.getDaysOfMonth(week.getYear(), week.getMonth())); 223 | week = new SolarWeek(solar.getYear(), solar.getMonth(), solar.getDay(), start); 224 | } 225 | } 226 | month = weekMonth; 227 | } 228 | n -= plus ? 1 : -1; 229 | } 230 | return week; 231 | } else { 232 | solar = solar.next(weeks * 7); 233 | return new SolarWeek(solar.getYear(), solar.getMonth(), solar.getDay(), start); 234 | } 235 | } 236 | 237 | /** 238 | * 获取本周第一天的阳历日期(可能跨月) 239 | * 240 | * @return 本周第一天的阳历日期 241 | */ 242 | public Solar getFirstDay() { 243 | Solar solar = Solar.fromYmd(year, month, day); 244 | int prev = solar.getWeek() - start; 245 | if (prev < 0) { 246 | prev += 7; 247 | } 248 | return solar.next(-prev); 249 | } 250 | 251 | /** 252 | * 获取本周第一天的阳历日期(仅限当月) 253 | * 254 | * @return 本周第一天的阳历日期 255 | */ 256 | public Solar getFirstDayInMonth() { 257 | List days = getDays(); 258 | for (Solar day : days) { 259 | if (month == day.getMonth()) { 260 | return day; 261 | } 262 | } 263 | return null; 264 | } 265 | 266 | /** 267 | * 获取本周的阳历日期列表(可能跨月) 268 | * 269 | * @return 本周的阳历日期列表 270 | */ 271 | @SuppressWarnings("all") 272 | public List getDays() { 273 | Solar firstDay = getFirstDay(); 274 | List l = new ArrayList(); 275 | l.add(firstDay); 276 | for (int i = 1; i < 7; i++) { 277 | l.add(firstDay.next(i)); 278 | } 279 | return l; 280 | } 281 | 282 | /** 283 | * 获取本周的阳历日期列表(仅限当月) 284 | * 285 | * @return 本周的阳历日期列表(仅限当月) 286 | */ 287 | public List getDaysInMonth() { 288 | List days = this.getDays(); 289 | List l = new ArrayList(); 290 | for (Solar day : days) { 291 | if (month != day.getMonth()) { 292 | continue; 293 | } 294 | l.add(day); 295 | } 296 | return l; 297 | } 298 | 299 | @Override 300 | public String toString() { 301 | return year + "." + month + "." + getIndex(); 302 | } 303 | 304 | public String toFullString() { 305 | return year + "年" + month + "月第" + getIndex() + "周"; 306 | } 307 | } 308 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/SolarYear.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | /** 9 | * 阳历年 10 | * 11 | * @author 6tail 12 | */ 13 | public class SolarYear { 14 | /** 15 | * 年 16 | */ 17 | private final int year; 18 | 19 | /** 20 | * 一年的月数 21 | */ 22 | public static final int MONTH_COUNT = 12; 23 | 24 | /** 25 | * 默认当年 26 | */ 27 | public SolarYear() { 28 | this(new Date()); 29 | } 30 | 31 | /** 32 | * 通过日期初始化 33 | */ 34 | public SolarYear(Date date) { 35 | year = Solar.fromDate(date).getYear(); 36 | } 37 | 38 | /** 39 | * 通过日历初始化 40 | */ 41 | @Deprecated 42 | public SolarYear(Calendar calendar) { 43 | year = calendar.get(Calendar.YEAR); 44 | } 45 | 46 | /** 47 | * 通过年初始化 48 | * 49 | * @param year 年 50 | */ 51 | public SolarYear(int year) { 52 | this.year = year; 53 | } 54 | 55 | /** 56 | * 通过指定日期获取阳历年 57 | * 58 | * @param date 日期 59 | * @return 阳历年 60 | */ 61 | public static SolarYear fromDate(Date date) { 62 | return new SolarYear(date); 63 | } 64 | 65 | /** 66 | * 通过指定日历获取阳历年 67 | * 68 | * @param calendar 日历 69 | * @return 阳历年 70 | */ 71 | @Deprecated 72 | public static SolarYear fromCalendar(Calendar calendar) { 73 | return new SolarYear(calendar); 74 | } 75 | 76 | /** 77 | * 通过指定年份获取阳历年 78 | * 79 | * @param year 年 80 | * @return 阳历年 81 | */ 82 | public static SolarYear fromYear(int year) { 83 | return new SolarYear(year); 84 | } 85 | 86 | /** 87 | * 获取年 88 | * 89 | * @return 年 90 | */ 91 | public int getYear() { 92 | return year; 93 | } 94 | 95 | /** 96 | * 获取本年的阳历月列表 97 | * 98 | * @return 阳历月列表 99 | */ 100 | public List getMonths() { 101 | List l = new ArrayList(MONTH_COUNT); 102 | SolarMonth m = new SolarMonth(year, 1); 103 | l.add(m); 104 | for (int i = 1; i < MONTH_COUNT; i++) { 105 | l.add(m.next(i)); 106 | } 107 | return l; 108 | } 109 | 110 | /** 111 | * 获取往后推几年的阳历年,如果要往前推,则年数用负数 112 | * 113 | * @param years 年数 114 | * @return 阳历年 115 | */ 116 | public SolarYear next(int years) { 117 | return new SolarYear(year + years); 118 | } 119 | 120 | @Override 121 | public String toString() { 122 | return year + ""; 123 | } 124 | 125 | public String toFullString() { 126 | return year + "年"; 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/Tao.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | import com.nlf.calendar.util.LunarUtil; 4 | import com.nlf.calendar.util.TaoUtil; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | /** 10 | * 道历 11 | * 12 | * @author 6tail 13 | */ 14 | public class Tao { 15 | 16 | public static final int BIRTH_YEAR = -2697; 17 | 18 | /** 19 | * 阴历 20 | */ 21 | private final Lunar lunar; 22 | 23 | public Tao(Lunar lunar) { 24 | this.lunar = lunar; 25 | } 26 | 27 | public static Tao fromLunar(Lunar lunar) { 28 | return new Tao(lunar); 29 | } 30 | 31 | public static Tao fromYmdHms(int year, int month, int day, int hour, int minute, int second) { 32 | return Tao.fromLunar(Lunar.fromYmdHms(year + BIRTH_YEAR, month, day, hour, minute, second)); 33 | } 34 | 35 | public static Tao fromYmd(int year, int month, int day) { 36 | return fromYmdHms(year, month, day, 0, 0, 0); 37 | } 38 | 39 | public Lunar getLunar() { 40 | return lunar; 41 | } 42 | 43 | public int getYear() { 44 | return lunar.getYear() - BIRTH_YEAR; 45 | } 46 | 47 | public int getMonth() { 48 | return lunar.getMonth(); 49 | } 50 | 51 | public int getDay() { 52 | return lunar.getDay(); 53 | } 54 | 55 | public String getYearInChinese() { 56 | String y = getYear() + ""; 57 | StringBuilder s = new StringBuilder(); 58 | for (int i = 0, j = y.length(); i < j; i++) { 59 | s.append(LunarUtil.NUMBER[y.charAt(i) - '0']); 60 | } 61 | return s.toString(); 62 | } 63 | 64 | public String getMonthInChinese() { 65 | return lunar.getMonthInChinese(); 66 | } 67 | 68 | public String getDayInChinese() { 69 | return lunar.getDayInChinese(); 70 | } 71 | 72 | public List getFestivals() { 73 | List l = new ArrayList(); 74 | List fs = TaoUtil.FESTIVAL.get(getMonth() + "-" + getDay()); 75 | if (null != fs) { 76 | l.addAll(fs); 77 | } 78 | String jq = lunar.getJieQi(); 79 | if ("冬至".equals(jq)) { 80 | l.add(new TaoFestival("元始天尊圣诞")); 81 | } else if ("夏至".equals(jq)) { 82 | l.add(new TaoFestival("灵宝天尊圣诞")); 83 | } 84 | // 八节日 85 | String f = TaoUtil.BA_JIE.get(jq); 86 | if (null != f) { 87 | l.add(new TaoFestival(f)); 88 | } 89 | // 八会日 90 | f = TaoUtil.BA_HUI.get(lunar.getDayInGanZhi()); 91 | if (null != f) { 92 | l.add(new TaoFestival(f)); 93 | } 94 | return l; 95 | } 96 | 97 | private boolean isDayIn(String[] days) { 98 | String md = getMonth() + "-" + getDay(); 99 | for (String d : days) { 100 | if (md.equals(d)) { 101 | return true; 102 | } 103 | } 104 | return false; 105 | } 106 | 107 | /** 108 | * 是否三会日 109 | * 110 | * @return true/false 111 | */ 112 | public boolean isDaySanHui() { 113 | return isDayIn(TaoUtil.SAN_HUI); 114 | } 115 | 116 | /** 117 | * 是否三元日 118 | * 119 | * @return true/false 120 | */ 121 | public boolean isDaySanYuan() { 122 | return isDayIn(TaoUtil.SAN_YUAN); 123 | } 124 | 125 | /** 126 | * 是否八节日 127 | * 128 | * @return true/false 129 | */ 130 | public boolean isDayBaJie() { 131 | return TaoUtil.BA_JIE.containsKey(lunar.getJieQi()); 132 | } 133 | 134 | /** 135 | * 是否五腊日 136 | * 137 | * @return true/false 138 | */ 139 | public boolean isDayWuLa() { 140 | return isDayIn(TaoUtil.WU_LA); 141 | } 142 | 143 | /** 144 | * 是否八会日 145 | * 146 | * @return true/false 147 | */ 148 | public boolean isDayBaHui() { 149 | return TaoUtil.BA_HUI.containsKey(lunar.getDayInGanZhi()); 150 | } 151 | 152 | /** 153 | * 是否明戊日 154 | * 155 | * @return true/false 156 | */ 157 | public boolean isDayMingWu() { 158 | return "戊".equals(lunar.getDayGan()); 159 | } 160 | 161 | /** 162 | * 是否暗戊日 163 | * 164 | * @return true/false 165 | */ 166 | public boolean isDayAnWu() { 167 | return lunar.getDayZhi().equals(TaoUtil.AN_WU[Math.abs(getMonth()) - 1]); 168 | } 169 | 170 | /** 171 | * 是否戊日 172 | * 173 | * @return true/false 174 | */ 175 | public boolean isDayWu() { 176 | return isDayMingWu() || isDayAnWu(); 177 | } 178 | 179 | /** 180 | * 是否天赦日 181 | * 182 | * @return true/false 183 | */ 184 | public boolean isDayTianShe() { 185 | boolean ret = false; 186 | String mz = lunar.getMonthZhi(); 187 | String dgz = lunar.getDayInGanZhi(); 188 | if ("寅卯辰".contains(mz)) { 189 | if ("戊寅".equals(dgz)) { 190 | ret = true; 191 | } 192 | } else if ("巳午未".contains(mz)) { 193 | if ("甲午".equals(dgz)) { 194 | ret = true; 195 | } 196 | } else if ("申酉戌".contains(mz)) { 197 | if ("戊申".equals(dgz)) { 198 | ret = true; 199 | } 200 | } else if ("亥子丑".contains(mz)) { 201 | if ("甲子".equals(dgz)) { 202 | ret = true; 203 | } 204 | } 205 | return ret; 206 | } 207 | 208 | @Override 209 | public String toString() { 210 | return String.format("%s年%s月%s", getYearInChinese(), getMonthInChinese(), getDayInChinese()); 211 | } 212 | 213 | public String toFullString() { 214 | return String.format("道歷%s年,天運%s年,%s月,%s日。%s月%s日,%s時。", getYearInChinese(), lunar.getYearInGanZhi(), lunar.getMonthInGanZhi(), lunar.getDayInGanZhi(), getMonthInChinese(), getDayInChinese(), lunar.getTimeZhi()); 215 | } 216 | 217 | } 218 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/TaoFestival.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar; 2 | 3 | /** 4 | * 道历节日 5 | * 6 | * @author 6tail 7 | */ 8 | public class TaoFestival { 9 | 10 | /** 11 | * 名称 12 | */ 13 | private final String name; 14 | 15 | /** 16 | * 备注 17 | */ 18 | private final String remark; 19 | 20 | public TaoFestival(String name, String remark) { 21 | this.name = name; 22 | this.remark = null == remark ? "" : remark; 23 | } 24 | 25 | public TaoFestival(String name) { 26 | this(name, null); 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public String getRemark() { 34 | return remark; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return name; 40 | } 41 | 42 | public String toFullString() { 43 | StringBuilder s = new StringBuilder(); 44 | s.append(name); 45 | if (null != remark && remark.length() > 0) { 46 | s.append("["); 47 | s.append(remark); 48 | s.append("]"); 49 | } 50 | return s.toString(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/eightchar/DaYun.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar.eightchar; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.util.LunarUtil; 5 | 6 | /** 7 | * 大运 8 | * 9 | * @author 6tail 10 | */ 11 | public class DaYun { 12 | /** 13 | * 开始年(含) 14 | */ 15 | private final int startYear; 16 | 17 | /** 18 | * 结束年(含) 19 | */ 20 | private final int endYear; 21 | 22 | /** 23 | * 开始年龄(含) 24 | */ 25 | private final int startAge; 26 | 27 | /** 28 | * 结束年龄(含) 29 | */ 30 | private final int endAge; 31 | 32 | /** 33 | * 序数,0-9 34 | */ 35 | private final int index; 36 | 37 | /** 38 | * 运 39 | */ 40 | private final Yun yun; 41 | 42 | private final Lunar lunar; 43 | 44 | public DaYun(Yun yun, int index) { 45 | this.yun = yun; 46 | this.lunar = yun.getLunar(); 47 | this.index = index; 48 | int birthYear = lunar.getSolar().getYear(); 49 | int year = yun.getStartSolar().getYear(); 50 | if (index < 1) { 51 | this.startYear = birthYear; 52 | this.startAge = 1; 53 | this.endYear = year - 1; 54 | this.endAge = year - birthYear; 55 | } else { 56 | int add = (index - 1) * 10; 57 | this.startYear = year + add; 58 | this.startAge = this.startYear - birthYear + 1; 59 | this.endYear = this.startYear + 9; 60 | this.endAge = this.startAge + 9; 61 | } 62 | } 63 | 64 | public int getStartYear() { 65 | return startYear; 66 | } 67 | 68 | public int getEndYear() { 69 | return endYear; 70 | } 71 | 72 | public int getStartAge() { 73 | return startAge; 74 | } 75 | 76 | public int getEndAge() { 77 | return endAge; 78 | } 79 | 80 | public int getIndex() { 81 | return index; 82 | } 83 | 84 | public Lunar getLunar() { 85 | return lunar; 86 | } 87 | 88 | /** 89 | * 获取干支 90 | * 91 | * @return 干支 92 | */ 93 | public String getGanZhi() { 94 | if (index < 1) { 95 | return ""; 96 | } 97 | int offset = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()); 98 | offset += yun.isForward() ? index : -index; 99 | int size = LunarUtil.JIA_ZI.length; 100 | if (offset >= size) { 101 | offset -= size; 102 | } 103 | if (offset < 0) { 104 | offset += size; 105 | } 106 | return LunarUtil.JIA_ZI[offset]; 107 | } 108 | 109 | /** 110 | * 获取所在旬 111 | * @return 旬 112 | */ 113 | public String getXun(){ 114 | return LunarUtil.getXun(getGanZhi()); 115 | } 116 | 117 | /** 118 | * 获取旬空(空亡) 119 | * @return 旬空(空亡) 120 | */ 121 | public String getXunKong(){ 122 | return LunarUtil.getXunKong(getGanZhi()); 123 | } 124 | 125 | /** 126 | * 获取10轮流年 127 | * 128 | * @return 流年 129 | */ 130 | public LiuNian[] getLiuNian() { 131 | return getLiuNian(10); 132 | } 133 | 134 | /** 135 | * 获取流年 136 | * 137 | * @param n 轮数 138 | * @return 流年 139 | */ 140 | public LiuNian[] getLiuNian(int n) { 141 | if (index < 1) { 142 | n = endYear-startYear+1; 143 | } 144 | LiuNian[] l = new LiuNian[n]; 145 | for (int i = 0; i < n; i++) { 146 | l[i] = new LiuNian(this, i); 147 | } 148 | return l; 149 | } 150 | 151 | /** 152 | * 获取10轮小运 153 | * 154 | * @return 小运 155 | */ 156 | public XiaoYun[] getXiaoYun() { 157 | return getXiaoYun(10); 158 | } 159 | 160 | /** 161 | * 获取小运 162 | * @param n 轮数 163 | * @return 小运 164 | */ 165 | public XiaoYun[] getXiaoYun(int n) { 166 | if (index < 1) { 167 | n = endYear-startYear+1; 168 | } 169 | XiaoYun[] l = new XiaoYun[n]; 170 | for (int i = 0; i < n; i++) { 171 | l[i] = new XiaoYun(this, i, yun.isForward()); 172 | } 173 | return l; 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/eightchar/LiuNian.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar.eightchar; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.util.LunarUtil; 5 | 6 | /** 7 | * 流年 8 | * 9 | * @author 6tail 10 | */ 11 | public class LiuNian { 12 | /** 13 | * 序数,0-9 14 | */ 15 | private final int index; 16 | 17 | /** 18 | * 大运 19 | */ 20 | private final DaYun daYun; 21 | 22 | /** 23 | * 年 24 | */ 25 | private final int year; 26 | 27 | /** 28 | * 年龄 29 | */ 30 | private final int age; 31 | 32 | private final Lunar lunar; 33 | 34 | public LiuNian(DaYun daYun, int index) { 35 | this.daYun = daYun; 36 | this.lunar = daYun.getLunar(); 37 | this.index = index; 38 | this.year = daYun.getStartYear() + index; 39 | this.age = daYun.getStartAge() + index; 40 | } 41 | 42 | public int getIndex() { 43 | return index; 44 | } 45 | 46 | public int getYear() { 47 | return year; 48 | } 49 | 50 | public int getAge() { 51 | return age; 52 | } 53 | 54 | /** 55 | * 获取干支 56 | * 57 | * @return 干支 58 | */ 59 | public String getGanZhi() { 60 | // 干支与出生日期和起运日期都没关系 61 | int offset = LunarUtil.getJiaZiIndex(lunar.getJieQiTable().get("立春").getLunar().getYearInGanZhiExact()) + this.index; 62 | if (daYun.getIndex() > 0) { 63 | offset += daYun.getStartAge() - 1; 64 | } 65 | offset %= LunarUtil.JIA_ZI.length; 66 | return LunarUtil.JIA_ZI[offset]; 67 | } 68 | 69 | /** 70 | * 获取所在旬 71 | * @return 旬 72 | */ 73 | public String getXun(){ 74 | return LunarUtil.getXun(getGanZhi()); 75 | } 76 | 77 | /** 78 | * 获取旬空(空亡) 79 | * @return 旬空(空亡) 80 | */ 81 | public String getXunKong(){ 82 | return LunarUtil.getXunKong(getGanZhi()); 83 | } 84 | 85 | /** 86 | * 获取流月 87 | * @return 流月 88 | */ 89 | public LiuYue[] getLiuYue() { 90 | int n = 12; 91 | LiuYue[] l = new LiuYue[n]; 92 | for (int i = 0; i < n; i++) { 93 | l[i] = new LiuYue(this, i); 94 | } 95 | return l; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/eightchar/LiuYue.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar.eightchar; 2 | 3 | import com.nlf.calendar.util.LunarUtil; 4 | 5 | /** 6 | * 流月 7 | * 8 | * @author 6tail 9 | */ 10 | public class LiuYue { 11 | /** 12 | * 序数,0-11 13 | */ 14 | private final int index; 15 | 16 | private final LiuNian liuNian; 17 | 18 | public LiuYue(LiuNian liuNian, int index) { 19 | this.liuNian = liuNian; 20 | this.index = index; 21 | } 22 | 23 | public int getIndex() { 24 | return index; 25 | } 26 | 27 | /** 28 | * 获取中文的月 29 | * 30 | * @return 中文月,如正 31 | */ 32 | public String getMonthInChinese() { 33 | return LunarUtil.MONTH[index + 1]; 34 | } 35 | 36 | /** 37 | * 获取干支 38 | *

39 | * 《五虎遁》 40 | * 甲己之年丙作首, 41 | * 乙庚之年戊为头, 42 | * 丙辛之年寻庚上, 43 | * 丁壬壬寅顺水流, 44 | * 若问戊癸何处走, 45 | * 甲寅之上好追求。 46 | * 47 | * @return 干支 48 | */ 49 | public String getGanZhi() { 50 | int offset = 0; 51 | String yearGan = liuNian.getGanZhi().substring(0, 1); 52 | if ("甲".equals(yearGan) || "己".equals(yearGan)) { 53 | offset = 2; 54 | } else if ("乙".equals(yearGan) || "庚".equals(yearGan)) { 55 | offset = 4; 56 | } else if ("丙".equals(yearGan) || "辛".equals(yearGan)) { 57 | offset = 6; 58 | } else if ("丁".equals(yearGan) || "壬".equals(yearGan)) { 59 | offset = 8; 60 | } 61 | String gan = LunarUtil.GAN[(index + offset) % 10 + 1]; 62 | String zhi = LunarUtil.ZHI[(index + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12 + 1]; 63 | return gan + zhi; 64 | } 65 | 66 | /** 67 | * 获取所在旬 68 | * @return 旬 69 | */ 70 | public String getXun(){ 71 | return LunarUtil.getXun(getGanZhi()); 72 | } 73 | 74 | /** 75 | * 获取旬空(空亡) 76 | * @return 旬空(空亡) 77 | */ 78 | public String getXunKong(){ 79 | return LunarUtil.getXunKong(getGanZhi()); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/eightchar/XiaoYun.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar.eightchar; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.util.LunarUtil; 5 | 6 | /** 7 | * 小运 8 | * 9 | * @author 6tail 10 | */ 11 | public class XiaoYun { 12 | /** 13 | * 序数,0-9 14 | */ 15 | private final int index; 16 | 17 | /** 18 | * 大运 19 | */ 20 | private final DaYun daYun; 21 | 22 | /** 23 | * 年 24 | */ 25 | private final int year; 26 | 27 | /** 28 | * 年龄 29 | */ 30 | private final int age; 31 | 32 | /** 33 | * 是否顺推 34 | */ 35 | private final boolean forward; 36 | 37 | private final Lunar lunar; 38 | 39 | public XiaoYun(DaYun daYun, int index, boolean forward) { 40 | this.daYun = daYun; 41 | this.lunar = daYun.getLunar(); 42 | this.index = index; 43 | this.year = daYun.getStartYear() + index; 44 | this.age = daYun.getStartAge() + index; 45 | this.forward = forward; 46 | } 47 | 48 | public int getIndex() { 49 | return index; 50 | } 51 | 52 | public int getYear() { 53 | return year; 54 | } 55 | 56 | public int getAge() { 57 | return age; 58 | } 59 | 60 | /** 61 | * 获取干支 62 | * 63 | * @return 干支 64 | */ 65 | public String getGanZhi() { 66 | int offset = LunarUtil.getJiaZiIndex(lunar.getTimeInGanZhi()); 67 | int add = this.index + 1; 68 | if (daYun.getIndex() > 0) { 69 | add += daYun.getStartAge() - 1; 70 | } 71 | offset += forward ? add : -add; 72 | int size = LunarUtil.JIA_ZI.length; 73 | while (offset < 0) { 74 | offset += size; 75 | } 76 | offset %= size; 77 | return LunarUtil.JIA_ZI[offset]; 78 | } 79 | 80 | /** 81 | * 获取所在旬 82 | * @return 旬 83 | */ 84 | public String getXun(){ 85 | return LunarUtil.getXun(getGanZhi()); 86 | } 87 | 88 | /** 89 | * 获取旬空(空亡) 90 | * @return 旬空(空亡) 91 | */ 92 | public String getXunKong(){ 93 | return LunarUtil.getXunKong(getGanZhi()); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/eightchar/Yun.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar.eightchar; 2 | 3 | import com.nlf.calendar.EightChar; 4 | import com.nlf.calendar.JieQi; 5 | import com.nlf.calendar.Lunar; 6 | import com.nlf.calendar.Solar; 7 | import com.nlf.calendar.util.LunarUtil; 8 | 9 | /** 10 | * 运 11 | * 12 | * @author 6tail 13 | */ 14 | public class Yun { 15 | /** 16 | * 性别(1男,0女) 17 | */ 18 | private final int gender; 19 | 20 | /** 21 | * 起运年数 22 | */ 23 | private int startYear; 24 | 25 | /** 26 | * 起运月数 27 | */ 28 | private int startMonth; 29 | 30 | /** 31 | * 起运天数 32 | */ 33 | private int startDay; 34 | 35 | /** 36 | * 起运小时数 37 | */ 38 | private int startHour; 39 | 40 | /** 41 | * 是否顺推 42 | */ 43 | private final boolean forward; 44 | 45 | private final Lunar lunar; 46 | 47 | /** 48 | * 使用默认流派1初始化运 49 | * @param eightChar 八字 50 | * @param gender 性别,1男,0女 51 | */ 52 | public Yun(EightChar eightChar, int gender) { 53 | this(eightChar, gender, 1); 54 | } 55 | 56 | /** 57 | * 初始化运 58 | * @param eightChar 八字 59 | * @param gender 性别,1男,0女 60 | * @param sect 流派,1按天数和时辰数计算,3天1年,1天4个月,1时辰10天;2按分钟数计算 61 | */ 62 | public Yun(EightChar eightChar, int gender, int sect) { 63 | this.lunar = eightChar.getLunar(); 64 | this.gender = gender; 65 | // 阳 66 | boolean yang = 0 == lunar.getYearGanIndexExact() % 2; 67 | // 男 68 | boolean man = 1 == gender; 69 | forward = (yang && man) || (!yang && !man); 70 | computeStart(sect); 71 | } 72 | 73 | /** 74 | * 起运计算 75 | */ 76 | private void computeStart(int sect) { 77 | // 上节 78 | JieQi prev = lunar.getPrevJie(); 79 | // 下节 80 | JieQi next = lunar.getNextJie(); 81 | // 出生日期 82 | Solar current = lunar.getSolar(); 83 | // 阳男阴女顺推,阴男阳女逆推 84 | Solar start = forward ? current : prev.getSolar(); 85 | Solar end = forward ? next.getSolar() : current; 86 | 87 | int year; 88 | int month; 89 | int day; 90 | int hour = 0; 91 | 92 | if (2 == sect) { 93 | long minutes = end.subtractMinute(start); 94 | long y = minutes / 4320; 95 | minutes -= y * 4320; 96 | long m = minutes / 360; 97 | minutes -= m * 360; 98 | long d = minutes / 12; 99 | minutes -= d * 12; 100 | long h = minutes * 2; 101 | year = (int)y; 102 | month = (int)m; 103 | day = (int)d; 104 | hour = (int)h; 105 | } else { 106 | int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substring(11, 16)); 107 | int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16)); 108 | // 时辰差 109 | int hourDiff = endTimeZhiIndex - startTimeZhiIndex; 110 | // 天数差 111 | int dayDiff = end.subtract(start); 112 | if (hourDiff < 0) { 113 | hourDiff += 12; 114 | dayDiff--; 115 | } 116 | int monthDiff = hourDiff * 10 / 30; 117 | month = dayDiff * 4 + monthDiff; 118 | day = hourDiff * 10 - monthDiff * 30; 119 | year = month / 12; 120 | month = month - year * 12; 121 | } 122 | this.startYear = year; 123 | this.startMonth = month; 124 | this.startDay = day; 125 | this.startHour = hour; 126 | } 127 | 128 | /** 129 | * 获取性别 130 | * 131 | * @return 性别(1男 , 0女) 132 | */ 133 | public int getGender() { 134 | return gender; 135 | } 136 | 137 | /** 138 | * 获取起运年数 139 | * 140 | * @return 起运年数 141 | */ 142 | public int getStartYear() { 143 | return startYear; 144 | } 145 | 146 | /** 147 | * 获取起运月数 148 | * 149 | * @return 起运月数 150 | */ 151 | public int getStartMonth() { 152 | return startMonth; 153 | } 154 | 155 | /** 156 | * 获取起运天数 157 | * 158 | * @return 起运天数 159 | */ 160 | public int getStartDay() { 161 | return startDay; 162 | } 163 | 164 | /** 165 | * 获取起运小时数 166 | * 167 | * @return 起运小时数 168 | */ 169 | public int getStartHour() { 170 | return startHour; 171 | } 172 | 173 | /** 174 | * 是否顺推 175 | * 176 | * @return true/false 177 | */ 178 | public boolean isForward() { 179 | return forward; 180 | } 181 | 182 | public Lunar getLunar() { 183 | return lunar; 184 | } 185 | 186 | /** 187 | * 获取起运的阳历日期 188 | * 189 | * @return 阳历日期 190 | */ 191 | public Solar getStartSolar() { 192 | Solar solar = lunar.getSolar(); 193 | solar = solar.nextYear(startYear); 194 | solar = solar.nextMonth(startMonth); 195 | solar = solar.next(startDay); 196 | return solar.nextHour(startHour); 197 | } 198 | 199 | /** 200 | * 获取10轮大运 201 | * 202 | * @return 大运 203 | */ 204 | public DaYun[] getDaYun() { 205 | return getDaYun(10); 206 | } 207 | 208 | /** 209 | * 获取大运 210 | * 211 | * @param n 轮数 212 | * @return 大运 213 | */ 214 | public DaYun[] getDaYun(int n) { 215 | DaYun[] l = new DaYun[n]; 216 | for (int i = 0; i < n; i++) { 217 | l[i] = new DaYun(this, i); 218 | } 219 | return l; 220 | } 221 | } 222 | 223 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/util/SolarUtil.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar.util; 2 | 3 | import com.nlf.calendar.Solar; 4 | 5 | import java.util.*; 6 | 7 | /** 8 | * 阳历工具 9 | * 10 | * @author 6tail 11 | */ 12 | public class SolarUtil { 13 | /** 14 | * 星期 15 | */ 16 | public static final String[] WEEK = {"日", "一", "二", "三", "四", "五", "六"}; 17 | /** 18 | * 每月天数 19 | */ 20 | public static final int[] DAYS_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 21 | /** 22 | * 星座 23 | */ 24 | public static final String[] XINGZUO = {"白羊", "金牛", "双子", "巨蟹", "狮子", "处女", "天秤", "天蝎", "射手", "摩羯", "水瓶", "双鱼"}; 25 | /** 26 | * 日期对应的节日 27 | */ 28 | public static final Map FESTIVAL = new HashMap() { 29 | private static final long serialVersionUID = -1; 30 | 31 | { 32 | put("1-1", "元旦节"); 33 | put("2-14", "情人节"); 34 | put("3-8", "妇女节"); 35 | put("3-12", "植树节"); 36 | put("3-15", "消费者权益日"); 37 | put("4-1", "愚人节"); 38 | put("5-1", "劳动节"); 39 | put("5-4", "青年节"); 40 | put("6-1", "儿童节"); 41 | put("7-1", "建党节"); 42 | put("8-1", "建军节"); 43 | put("9-10", "教师节"); 44 | put("10-1", "国庆节"); 45 | put("10-31", "万圣节前夜"); 46 | put("11-1", "万圣节"); 47 | put("12-24", "平安夜"); 48 | put("12-25", "圣诞节"); 49 | } 50 | }; 51 | /** 52 | * 几月第几个星期几对应的节日 53 | */ 54 | public static final Map WEEK_FESTIVAL = new HashMap() { 55 | private static final long serialVersionUID = -1; 56 | 57 | { 58 | put("3-0-1", "全国中小学生安全教育日"); 59 | put("5-2-0", "母亲节"); 60 | put("6-3-0", "父亲节"); 61 | put("11-4-4", "感恩节"); 62 | } 63 | }; 64 | /** 65 | * 日期对应的非正式节日 66 | */ 67 | public static final Map> OTHER_FESTIVAL = new HashMap>() { 68 | private static final long serialVersionUID = -1; 69 | 70 | { 71 | put("1-8", Collections.nCopies(1, "周恩来逝世纪念日")); 72 | put("1-10", Collections.nCopies(1, "中国人民警察节")); 73 | put("1-14", Collections.nCopies(1, "日记情人节")); 74 | put("1-21", Collections.nCopies(1, "列宁逝世纪念日")); 75 | put("1-26", Collections.nCopies(1, "国际海关日")); 76 | put("1-27", Collections.nCopies(1, "国际大屠杀纪念日")); 77 | put("2-2", Collections.nCopies(1, "世界湿地日")); 78 | put("2-4", Collections.nCopies(1, "世界抗癌日")); 79 | put("2-7", Collections.nCopies(1, "京汉铁路罢工纪念日")); 80 | put("2-10", Collections.nCopies(1, "国际气象节")); 81 | put("2-19", Collections.nCopies(1, "邓小平逝世纪念日")); 82 | put("2-20", Collections.nCopies(1, "世界社会公正日")); 83 | put("2-21", Collections.nCopies(1, "国际母语日")); 84 | put("2-24", Collections.nCopies(1, "第三世界青年日")); 85 | put("3-1", Collections.nCopies(1, "国际海豹日")); 86 | put("3-3", Arrays.asList("世界野生动植物日", "全国爱耳日")); 87 | put("3-5", Arrays.asList("周恩来诞辰纪念日", "中国青年志愿者服务日")); 88 | put("3-6", Collections.nCopies(1, "世界青光眼日")); 89 | put("3-7", Collections.nCopies(1, "女生节")); 90 | put("3-12", Collections.nCopies(1, "孙中山逝世纪念日")); 91 | put("3-14", Arrays.asList("马克思逝世纪念日", "白色情人节")); 92 | put("3-17", Collections.nCopies(1, "国际航海日")); 93 | put("3-18", Arrays.asList("全国科技人才活动日", "全国爱肝日")); 94 | put("3-20", Collections.nCopies(1, "国际幸福日")); 95 | put("3-21", Arrays.asList("世界森林日", "世界睡眠日", "国际消除种族歧视日")); 96 | put("3-22", Collections.nCopies(1, "世界水日")); 97 | put("3-23", Collections.nCopies(1, "世界气象日")); 98 | put("3-24", Collections.nCopies(1, "世界防治结核病日")); 99 | put("3-29", Collections.nCopies(1, "中国黄花岗七十二烈士殉难纪念日")); 100 | put("4-2", Arrays.asList("国际儿童图书日", "世界自闭症日")); 101 | put("4-4", Collections.nCopies(1, "国际地雷行动日")); 102 | put("4-7", Collections.nCopies(1, "世界卫生日")); 103 | put("4-8", Collections.nCopies(1, "国际珍稀动物保护日")); 104 | put("4-12", Collections.nCopies(1, "世界航天日")); 105 | put("4-14", Collections.nCopies(1, "黑色情人节")); 106 | put("4-15", Collections.nCopies(1, "全民国家安全教育日")); 107 | put("4-22", Arrays.asList("世界地球日", "列宁诞辰纪念日")); 108 | put("4-23", Collections.nCopies(1, "世界读书日")); 109 | put("4-24", Collections.nCopies(1, "中国航天日")); 110 | put("4-25", Collections.nCopies(1, "儿童预防接种宣传日")); 111 | put("4-26", Arrays.asList("世界知识产权日", "全国疟疾日")); 112 | put("4-28", Collections.nCopies(1, "世界安全生产与健康日")); 113 | put("4-30", Collections.nCopies(1, "全国交通安全反思日")); 114 | put("5-2", Collections.nCopies(1, "世界金枪鱼日")); 115 | put("5-3", Collections.nCopies(1, "世界新闻自由日")); 116 | put("5-5", Collections.nCopies(1, "马克思诞辰纪念日")); 117 | put("5-8", Collections.nCopies(1, "世界红十字日")); 118 | put("5-11", Collections.nCopies(1, "世界肥胖日")); 119 | put("5-12", Arrays.asList("全国防灾减灾日", "护士节")); 120 | put("5-14", Collections.nCopies(1, "玫瑰情人节")); 121 | put("5-15", Collections.nCopies(1, "国际家庭日")); 122 | put("5-19", Collections.nCopies(1, "中国旅游日")); 123 | put("5-20", Collections.nCopies(1, "网络情人节")); 124 | put("5-22", Collections.nCopies(1, "国际生物多样性日")); 125 | put("5-25", Collections.nCopies(1, "525心理健康节")); 126 | put("5-27", Collections.nCopies(1, "上海解放日")); 127 | put("5-29", Collections.nCopies(1, "国际维和人员日")); 128 | put("5-30", Collections.nCopies(1, "中国五卅运动纪念日")); 129 | put("5-31", Collections.nCopies(1, "世界无烟日")); 130 | put("6-3", Collections.nCopies(1, "世界自行车日")); 131 | put("6-5", Collections.nCopies(1, "世界环境日")); 132 | put("6-6", Collections.nCopies(1, "全国爱眼日")); 133 | put("6-8", Collections.nCopies(1, "世界海洋日")); 134 | put("6-11", Collections.nCopies(1, "中国人口日")); 135 | put("6-14", Arrays.asList("世界献血日", "亲亲情人节")); 136 | put("6-17", Collections.nCopies(1, "世界防治荒漠化与干旱日")); 137 | put("6-20", Collections.nCopies(1, "世界难民日")); 138 | put("6-21", Collections.nCopies(1, "国际瑜伽日")); 139 | put("6-25", Collections.nCopies(1, "全国土地日")); 140 | put("6-26", Arrays.asList("国际禁毒日", "联合国宪章日")); 141 | put("7-1", Collections.nCopies(1, "香港回归纪念日")); 142 | put("7-6", Arrays.asList("国际接吻日", "朱德逝世纪念日")); 143 | put("7-7", Collections.nCopies(1, "七七事变纪念日")); 144 | put("7-11", Arrays.asList("世界人口日", "中国航海日")); 145 | put("7-14", Collections.nCopies(1, "银色情人节")); 146 | put("7-18", Collections.nCopies(1, "曼德拉国际日")); 147 | put("7-30", Collections.nCopies(1, "国际友谊日")); 148 | put("8-3", Collections.nCopies(1, "男人节")); 149 | put("8-5", Collections.nCopies(1, "恩格斯逝世纪念日")); 150 | put("8-6", Collections.nCopies(1, "国际电影节")); 151 | put("8-8", Collections.nCopies(1, "全民健身日")); 152 | put("8-9", Collections.nCopies(1, "国际土著人日")); 153 | put("8-12", Collections.nCopies(1, "国际青年节")); 154 | put("8-14", Collections.nCopies(1, "绿色情人节")); 155 | put("8-19", Arrays.asList("世界人道主义日", "中国医师节")); 156 | put("8-22", Collections.nCopies(1, "邓小平诞辰纪念日")); 157 | put("8-29", Collections.nCopies(1, "全国测绘法宣传日")); 158 | put("9-3", Collections.nCopies(1, "中国抗日战争胜利纪念日")); 159 | put("9-5", Collections.nCopies(1, "中华慈善日")); 160 | put("9-8", Collections.nCopies(1, "世界扫盲日")); 161 | put("9-9", Arrays.asList("毛泽东逝世纪念日", "全国拒绝酒驾日")); 162 | put("9-14", Arrays.asList("世界清洁地球日", "相片情人节")); 163 | put("9-15", Collections.nCopies(1, "国际民主日")); 164 | put("9-16", Collections.nCopies(1, "国际臭氧层保护日")); 165 | put("9-17", Collections.nCopies(1, "世界骑行日")); 166 | put("9-18", Collections.nCopies(1, "九一八事变纪念日")); 167 | put("9-20", Collections.nCopies(1, "全国爱牙日")); 168 | put("9-21", Collections.nCopies(1, "国际和平日")); 169 | put("9-27", Collections.nCopies(1, "世界旅游日")); 170 | put("9-30", Collections.nCopies(1, "中国烈士纪念日")); 171 | put("10-1", Collections.nCopies(1, "国际老年人日")); 172 | put("10-2", Collections.nCopies(1, "国际非暴力日")); 173 | put("10-4", Collections.nCopies(1, "世界动物日")); 174 | put("10-11", Collections.nCopies(1, "国际女童日")); 175 | put("10-10", Collections.nCopies(1, "辛亥革命纪念日")); 176 | put("10-13", Arrays.asList("国际减轻自然灾害日", "中国少年先锋队诞辰日")); 177 | put("10-14", Collections.nCopies(1, "葡萄酒情人节")); 178 | put("10-16", Collections.nCopies(1, "世界粮食日")); 179 | put("10-17", Collections.nCopies(1, "全国扶贫日")); 180 | put("10-20", Collections.nCopies(1, "世界统计日")); 181 | put("10-24", Arrays.asList("世界发展信息日", "程序员节")); 182 | put("10-25", Collections.nCopies(1, "抗美援朝纪念日")); 183 | put("11-5", Collections.nCopies(1, "世界海啸日")); 184 | put("11-8", Collections.nCopies(1, "记者节")); 185 | put("11-9", Collections.nCopies(1, "全国消防日")); 186 | put("11-11", Collections.nCopies(1, "光棍节")); 187 | put("11-12", Collections.nCopies(1, "孙中山诞辰纪念日")); 188 | put("11-14", Collections.nCopies(1, "电影情人节")); 189 | put("11-16", Collections.nCopies(1, "国际宽容日")); 190 | put("11-17", Collections.nCopies(1, "国际大学生节")); 191 | put("11-19", Collections.nCopies(1, "世界厕所日")); 192 | put("11-28", Collections.nCopies(1, "恩格斯诞辰纪念日")); 193 | put("11-29", Collections.nCopies(1, "国际声援巴勒斯坦人民日")); 194 | put("12-1", Collections.nCopies(1, "世界艾滋病日")); 195 | put("12-2", Collections.nCopies(1, "全国交通安全日")); 196 | put("12-3", Collections.nCopies(1, "世界残疾人日")); 197 | put("12-4", Collections.nCopies(1, "全国法制宣传日")); 198 | put("12-5", Arrays.asList("世界弱能人士日", "国际志愿人员日")); 199 | put("12-7", Collections.nCopies(1, "国际民航日")); 200 | put("12-9", Arrays.asList("世界足球日", "国际反腐败日")); 201 | put("12-10", Collections.nCopies(1, "世界人权日")); 202 | put("12-11", Collections.nCopies(1, "国际山岳日")); 203 | put("12-12", Collections.nCopies(1, "西安事变纪念日")); 204 | put("12-13", Collections.nCopies(1, "国家公祭日")); 205 | put("12-14", Collections.nCopies(1, "拥抱情人节")); 206 | put("12-18", Collections.nCopies(1, "国际移徙者日")); 207 | put("12-26", Collections.nCopies(1, "毛泽东诞辰纪念日")); 208 | } 209 | }; 210 | 211 | protected SolarUtil() { 212 | } 213 | 214 | /** 215 | * 是否闰年 216 | * 217 | * @param year 年 218 | * @return true/false 闰年/非闰年 219 | */ 220 | public static boolean isLeapYear(int year) { 221 | if (year < 1600) { 222 | return year % 4 == 0; 223 | } 224 | return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 225 | } 226 | 227 | /** 228 | * 获取某年有多少天(平年365天,闰年366天) 229 | * 230 | * @param year 年 231 | * @return 天数 232 | */ 233 | public static int getDaysOfYear(int year) { 234 | if (1582 == year) { 235 | return 355; 236 | } 237 | return isLeapYear(year) ? 366 : 365; 238 | } 239 | 240 | /** 241 | * 获取某年某月有多少天 242 | * 243 | * @param year 年 244 | * @param month 月 245 | * @return 天数 246 | */ 247 | public static int getDaysOfMonth(int year, int month) { 248 | if (1582 == year && 10 == month) { 249 | return 21; 250 | } 251 | int m = month - 1; 252 | int d = DAYS_OF_MONTH[m]; 253 | //公历闰年2月多一天 254 | if (m == Calendar.FEBRUARY && isLeapYear(year)) { 255 | d++; 256 | } 257 | return d; 258 | } 259 | 260 | /** 261 | * 获取某天为当年的第几天 262 | * 263 | * @param year 年 264 | * @param month 月 265 | * @param day 日 266 | * @return 第几天 267 | */ 268 | public static int getDaysInYear(int year, int month, int day) { 269 | int days = 0; 270 | for (int i = 1; i < month; i++) { 271 | days += getDaysOfMonth(year, i); 272 | } 273 | int d = day; 274 | if (1582 == year && 10 == month) { 275 | if (day >= 15) { 276 | d -= 10; 277 | } else if (day > 4) { 278 | throw new IllegalArgumentException(String.format("wrong solar year %d month %d day %d", year, month, day)); 279 | } 280 | } 281 | days += d; 282 | return days; 283 | } 284 | 285 | /** 286 | * 获取某年某月有多少周 287 | * 288 | * @param year 年 289 | * @param month 月 290 | * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 291 | * @return 周数 292 | */ 293 | public static int getWeeksOfMonth(int year, int month, int start) { 294 | return (int) Math.ceil((getDaysOfMonth(year, month) + Solar.fromYmd(year, month, 1).getWeek() - start) * 1D / WEEK.length); 295 | } 296 | 297 | /** 298 | * 获取两个日期之间相差的天数(如果日期a比日期b小,天数为正,如果日期a比日期b大,天数为负) 299 | * 300 | * @param ay 年a 301 | * @param am 月a 302 | * @param ad 日a 303 | * @param by 年b 304 | * @param bm 月b 305 | * @param bd 日b 306 | * @return 天数 307 | */ 308 | public static int getDaysBetween(int ay, int am, int ad, int by, int bm, int bd) { 309 | int n; 310 | int days; 311 | int i; 312 | if (ay == by) { 313 | n = getDaysInYear(by, bm, bd) - getDaysInYear(ay, am, ad); 314 | } else if (ay > by) { 315 | days = getDaysOfYear(by) - getDaysInYear(by, bm, bd); 316 | for (i = by + 1; i < ay; i++) { 317 | days += getDaysOfYear(i); 318 | } 319 | days += getDaysInYear(ay, am, ad); 320 | n = -days; 321 | } else { 322 | days = getDaysOfYear(ay) - getDaysInYear(ay, am, ad); 323 | for (i = ay + 1; i < by; i++) { 324 | days += getDaysOfYear(i); 325 | } 326 | days += getDaysInYear(by, bm, bd); 327 | n = days; 328 | } 329 | return n; 330 | } 331 | 332 | } 333 | -------------------------------------------------------------------------------- /src/main/java/com/nlf/calendar/util/TaoUtil.java: -------------------------------------------------------------------------------- 1 | package com.nlf.calendar.util; 2 | 3 | import com.nlf.calendar.TaoFestival; 4 | 5 | import java.util.*; 6 | 7 | /** 8 | * 道历工具 9 | * 10 | * @author 6tail 11 | */ 12 | public class TaoUtil { 13 | /** 14 | * 日期对应的节日 15 | */ 16 | public static final Map> FESTIVAL = new HashMap>() { 17 | private static final long serialVersionUID = 1; 18 | 19 | { 20 | put("1-1", Collections.nCopies(1, new TaoFestival("天腊之辰", "天腊,此日五帝会于东方九炁青天"))); 21 | put("1-3", Arrays.asList(new TaoFestival("郝真人圣诞"), new TaoFestival("孙真人圣诞"))); 22 | put("1-5", Collections.nCopies(1, new TaoFestival("孙祖清静元君诞"))); 23 | put("1-7", Collections.nCopies(1, new TaoFestival("举迁赏会", "此日上元赐福,天官同地水二官考校罪福"))); 24 | put("1-9", Collections.nCopies(1, new TaoFestival("玉皇上帝圣诞"))); 25 | put("1-13", Collections.nCopies(1, new TaoFestival("关圣帝君飞升"))); 26 | put("1-15", Arrays.asList(new TaoFestival("上元天官圣诞"), new TaoFestival("老祖天师圣诞"))); 27 | put("1-19", Collections.nCopies(1, new TaoFestival("长春邱真人(邱处机)圣诞"))); 28 | put("1-28", Collections.nCopies(1, new TaoFestival("许真君(许逊天师)圣诞"))); 29 | put("2-1", Arrays.asList(new TaoFestival("勾陈天皇大帝圣诞"), new TaoFestival("长春刘真人(刘渊然)圣诞"))); 30 | put("2-2", Arrays.asList(new TaoFestival("土地正神诞"), new TaoFestival("姜太公圣诞"))); 31 | put("2-3", Collections.nCopies(1, new TaoFestival("文昌梓潼帝君圣诞"))); 32 | put("2-6", Collections.nCopies(1, new TaoFestival("东华帝君圣诞"))); 33 | put("2-13", Collections.nCopies(1, new TaoFestival("度人无量葛真君圣诞"))); 34 | put("2-15", Collections.nCopies(1, new TaoFestival("太清道德天尊(太上老君)圣诞"))); 35 | put("2-19", Collections.nCopies(1, new TaoFestival("慈航真人圣诞"))); 36 | put("3-1", Collections.nCopies(1, new TaoFestival("谭祖(谭处端)长真真人圣诞"))); 37 | put("3-3", Collections.nCopies(1, new TaoFestival("玄天上帝圣诞"))); 38 | put("3-6", Collections.nCopies(1, new TaoFestival("眼光娘娘圣诞"))); 39 | put("3-15", Arrays.asList(new TaoFestival("天师张大真人圣诞"), new TaoFestival("财神赵公元帅圣诞"))); 40 | put("3-16", Arrays.asList(new TaoFestival("三茅真君得道之辰"), new TaoFestival("中岳大帝圣诞"))); 41 | put("3-18", Arrays.asList(new TaoFestival("王祖(王处一)玉阳真人圣诞"), new TaoFestival("后土娘娘圣诞"))); 42 | put("3-19", Collections.nCopies(1, new TaoFestival("太阳星君圣诞"))); 43 | put("3-20", Collections.nCopies(1, new TaoFestival("子孙娘娘圣诞"))); 44 | put("3-23", Collections.nCopies(1, new TaoFestival("天后妈祖圣诞"))); 45 | put("3-26", Collections.nCopies(1, new TaoFestival("鬼谷先师诞"))); 46 | put("3-28", Collections.nCopies(1, new TaoFestival("东岳大帝圣诞"))); 47 | put("4-1", Collections.nCopies(1, new TaoFestival("长生谭真君成道之辰"))); 48 | put("4-10", Collections.nCopies(1, new TaoFestival("何仙姑圣诞"))); 49 | put("4-14", Collections.nCopies(1, new TaoFestival("吕祖纯阳祖师圣诞"))); 50 | put("4-15", Collections.nCopies(1, new TaoFestival("钟离祖师圣诞"))); 51 | put("4-18", Arrays.asList(new TaoFestival("北极紫微大帝圣诞"), new TaoFestival("泰山圣母碧霞元君诞"), new TaoFestival("华佗神医先师诞"))); 52 | put("4-20", Collections.nCopies(1, new TaoFestival("眼光圣母娘娘诞"))); 53 | put("4-28", Collections.nCopies(1, new TaoFestival("神农先帝诞"))); 54 | put("5-1", Collections.nCopies(1, new TaoFestival("南极长生大帝圣诞"))); 55 | put("5-5", Arrays.asList(new TaoFestival("地腊之辰", "地腊,此日五帝会于南方三炁丹天"), new TaoFestival("南方雷祖圣诞"), new TaoFestival("地祗温元帅圣诞"), new TaoFestival("雷霆邓天君圣诞"))); 56 | put("5-11", Collections.nCopies(1, new TaoFestival("城隍爷圣诞"))); 57 | put("5-13", Arrays.asList(new TaoFestival("关圣帝君降神"), new TaoFestival("关平太子圣诞"))); 58 | put("5-18", Collections.nCopies(1, new TaoFestival("张天师圣诞"))); 59 | put("5-20", Collections.nCopies(1, new TaoFestival("马祖丹阳真人圣诞"))); 60 | put("5-29", Collections.nCopies(1, new TaoFestival("紫青白祖师圣诞"))); 61 | put("6-1", Collections.nCopies(1, new TaoFestival("南斗星君下降"))); 62 | put("6-2", Collections.nCopies(1, new TaoFestival("南斗星君下降"))); 63 | put("6-3", Collections.nCopies(1, new TaoFestival("南斗星君下降"))); 64 | put("6-4", Collections.nCopies(1, new TaoFestival("南斗星君下降"))); 65 | put("6-5", Collections.nCopies(1, new TaoFestival("南斗星君下降"))); 66 | put("6-6", Collections.nCopies(1, new TaoFestival("南斗星君下降"))); 67 | put("6-10", Collections.nCopies(1, new TaoFestival("刘海蟾祖师圣诞"))); 68 | put("6-15", Collections.nCopies(1, new TaoFestival("灵官王天君圣诞"))); 69 | put("6-19", Collections.nCopies(1, new TaoFestival("慈航(观音)成道日"))); 70 | put("6-23", Collections.nCopies(1, new TaoFestival("火神圣诞"))); 71 | put("6-24", Arrays.asList(new TaoFestival("南极大帝中方雷祖圣诞"), new TaoFestival("关圣帝君圣诞"))); 72 | put("6-26", Collections.nCopies(1, new TaoFestival("二郎真君圣诞"))); 73 | put("7-7", Arrays.asList(new TaoFestival("道德腊之辰", "道德腊,此日五帝会于西方七炁素天"), new TaoFestival("庆生中会", "此日中元赦罪,地官同天水二官考校罪福"))); 74 | put("7-12", Collections.nCopies(1, new TaoFestival("西方雷祖圣诞"))); 75 | put("7-15", Collections.nCopies(1, new TaoFestival("中元地官大帝圣诞"))); 76 | put("7-18", Collections.nCopies(1, new TaoFestival("王母娘娘圣诞"))); 77 | put("7-20", Collections.nCopies(1, new TaoFestival("刘祖(刘处玄)长生真人圣诞"))); 78 | put("7-22", Collections.nCopies(1, new TaoFestival("财帛星君文财神增福相公李诡祖圣诞"))); 79 | put("7-26", Collections.nCopies(1, new TaoFestival("张三丰祖师圣诞"))); 80 | put("8-1", Collections.nCopies(1, new TaoFestival("许真君飞升日"))); 81 | put("8-3", Collections.nCopies(1, new TaoFestival("九天司命灶君诞"))); 82 | put("8-5", Collections.nCopies(1, new TaoFestival("北方雷祖圣诞"))); 83 | put("8-10", Collections.nCopies(1, new TaoFestival("北岳大帝诞辰"))); 84 | put("8-15", Collections.nCopies(1, new TaoFestival("太阴星君诞"))); 85 | put("9-1", Collections.nCopies(1, new TaoFestival("北斗九皇降世之辰"))); 86 | put("9-2", Collections.nCopies(1, new TaoFestival("北斗九皇降世之辰"))); 87 | put("9-3", Collections.nCopies(1, new TaoFestival("北斗九皇降世之辰"))); 88 | put("9-4", Collections.nCopies(1, new TaoFestival("北斗九皇降世之辰"))); 89 | put("9-5", Collections.nCopies(1, new TaoFestival("北斗九皇降世之辰"))); 90 | put("9-6", Collections.nCopies(1, new TaoFestival("北斗九皇降世之辰"))); 91 | put("9-7", Collections.nCopies(1, new TaoFestival("北斗九皇降世之辰"))); 92 | put("9-8", Collections.nCopies(1, new TaoFestival("北斗九皇降世之辰"))); 93 | put("9-9", Arrays.asList(new TaoFestival("北斗九皇降世之辰"), new TaoFestival("斗姥元君圣诞"), new TaoFestival("重阳帝君圣诞"), new TaoFestival("玄天上帝飞升"), new TaoFestival("酆都大帝圣诞"))); 94 | put("9-22", Collections.nCopies(1, new TaoFestival("增福财神诞"))); 95 | put("9-23", Collections.nCopies(1, new TaoFestival("萨翁真君圣诞"))); 96 | put("9-28", Collections.nCopies(1, new TaoFestival("五显灵官马元帅圣诞"))); 97 | put("10-1", Arrays.asList(new TaoFestival("民岁腊之辰", "民岁腊,此日五帝会于北方五炁黑天"), new TaoFestival("东皇大帝圣诞"))); 98 | put("10-3", Collections.nCopies(1, new TaoFestival("三茅应化真君圣诞"))); 99 | put("10-6", Collections.nCopies(1, new TaoFestival("天曹诸司五岳五帝圣诞"))); 100 | put("10-15", Arrays.asList(new TaoFestival("下元水官大帝圣诞"), new TaoFestival("建生大会", "此日下元解厄,水官同天地二官考校罪福"))); 101 | put("10-18", Collections.nCopies(1, new TaoFestival("地母娘娘圣诞"))); 102 | put("10-19", Collections.nCopies(1, new TaoFestival("长春邱真君飞升"))); 103 | put("10-20", Collections.nCopies(1, new TaoFestival("虚靖天师(即三十代天师弘悟张真人)诞"))); 104 | put("11-6", Collections.nCopies(1, new TaoFestival("西岳大帝圣诞"))); 105 | put("11-9", Collections.nCopies(1, new TaoFestival("湘子韩祖圣诞"))); 106 | put("11-11", Collections.nCopies(1, new TaoFestival("太乙救苦天尊圣诞"))); 107 | put("11-26", Collections.nCopies(1, new TaoFestival("北方五道圣诞"))); 108 | put("12-8", Collections.nCopies(1, new TaoFestival("王侯腊之辰", "王侯腊,此日五帝会于上方玄都玉京"))); 109 | put("12-16", Arrays.asList(new TaoFestival("南岳大帝圣诞"), new TaoFestival("福德正神诞"))); 110 | put("12-20", Collections.nCopies(1, new TaoFestival("鲁班先师圣诞"))); 111 | put("12-21", Collections.nCopies(1, new TaoFestival("天猷上帝圣诞"))); 112 | put("12-22", Collections.nCopies(1, new TaoFestival("重阳祖师圣诞"))); 113 | put("12-23", Collections.nCopies(1, new TaoFestival("祭灶王", "最适宜谢旧年太岁,开启拜新年太岁"))); 114 | put("12-25", Arrays.asList(new TaoFestival("玉帝巡天"), new TaoFestival("天神下降"))); 115 | put("12-29", Collections.nCopies(1, new TaoFestival("清静孙真君(孙不二)成道"))); 116 | } 117 | }; 118 | 119 | /** 120 | * 八会日 121 | */ 122 | public static final Map BA_HUI = new HashMap() { 123 | 124 | private static final long serialVersionUID = 1; 125 | 126 | { 127 | put("丙午", "天会"); 128 | put("壬午", "地会"); 129 | put("壬子", "人会"); 130 | put("庚午", "日会"); 131 | put("庚申", "月会"); 132 | put("辛酉", "星辰会"); 133 | put("甲辰", "五行会"); 134 | put("甲戌", "四时会"); 135 | } 136 | }; 137 | 138 | /** 139 | * 八节日 140 | */ 141 | public static final Map BA_JIE = new HashMap() { 142 | 143 | private static final long serialVersionUID = 1; 144 | 145 | { 146 | put("立春", "东北方度仙上圣天尊同梵炁始青天君下降"); 147 | put("春分", "东方玉宝星上天尊同青帝九炁天君下降"); 148 | put("立夏", "东南方好生度命天尊同梵炁始丹天君下降"); 149 | put("夏至", "南方玄真万福天尊同赤帝三炁天君下降"); 150 | put("立秋", "西南方太灵虚皇天尊同梵炁始素天君下降"); 151 | put("秋分", "西方太妙至极天尊同白帝七炁天君下降"); 152 | put("立冬", "西北方无量太华天尊同梵炁始玄天君下降"); 153 | put("冬至", "北方玄上玉宸天尊同黑帝五炁天君下降"); 154 | } 155 | }; 156 | 157 | /** 158 | * 三会日 159 | */ 160 | public static final String[] SAN_HUI = {"1-7", "7-7", "10-15"}; 161 | 162 | /** 163 | * 三元日 164 | */ 165 | public static final String[] SAN_YUAN = {"1-15", "7-15", "10-15"}; 166 | 167 | /** 168 | * 五腊日 169 | */ 170 | public static final String[] WU_LA = {"1-1", "5-5", "7-7", "10-1", "12-8"}; 171 | 172 | /** 173 | * 暗戊 174 | */ 175 | public static final String[] AN_WU = {"未", "戌", "辰", "寅", "午", "子", "酉", "申", "巳", "亥", "卯", "丑"}; 176 | 177 | } 178 | -------------------------------------------------------------------------------- /src/test/java/sample/BaZiTest.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.Solar; 5 | import org.junit.Test; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * 八字测试 11 | * @author 6tail 12 | * @deprecated 见BaZiTestNew 13 | */ 14 | @Deprecated 15 | public class BaZiTest { 16 | 17 | @Test 18 | public void test1(){ 19 | Solar solar = new Solar(2020,1,1,22,35,0); 20 | Lunar lunar = solar.getLunar(); 21 | //[己亥, 丙子, 癸卯, 癸亥] 22 | System.out.println(lunar.getBaZi()); 23 | } 24 | 25 | @Test 26 | public void test2(){ 27 | Solar solar = new Solar(2020,1,6,14,35,0); 28 | Lunar lunar = solar.getLunar(); 29 | //[己亥, 丁丑, 戊申, 己未] 30 | System.out.println(lunar.getBaZi()); 31 | } 32 | 33 | @Test 34 | public void test3(){ 35 | Solar solar = new Solar(2020,1,6,3,35,0); 36 | Lunar lunar = solar.getLunar(); 37 | //[己亥, 丁丑, 戊辰, 癸亥] 38 | System.out.println(lunar.getBaZi()); 39 | } 40 | 41 | @Test 42 | public void test4(){ 43 | Solar solar = new Solar(2020,1,26,21,41,0); 44 | Lunar lunar = solar.getLunar(); 45 | //[己亥, 丙子, 戊申, 甲寅] 46 | System.out.println(lunar.getBaZi()); 47 | } 48 | 49 | @Test 50 | public void test5(){ 51 | Solar solar = new Solar(2020,2,4,1,42,0); 52 | Lunar lunar = solar.getLunar(); 53 | //[己亥, 丁丑, 丁丑, 辛丑] 54 | System.out.println(lunar.getBaZi()); 55 | } 56 | 57 | @Test 58 | public void test6(){ 59 | Solar solar = new Solar(2020,2,5,21,43,0); 60 | Lunar lunar = solar.getLunar(); 61 | //[庚子, 戊寅, 戊寅, 癸亥] 62 | System.out.println(lunar.getBaZi()); 63 | } 64 | 65 | @Test 66 | public void testBazi2Solar6() { 67 | List l = Solar.fromBaZi("庚子", "戊寅", "戊寅", "癸亥"); 68 | // [2020-02-05 22:00:00, 1960-02-20 22:00:00] 69 | for (Solar solar : l) { 70 | System.out.println(solar.toFullString()); 71 | } 72 | } 73 | 74 | @Test 75 | public void test7(){ 76 | Solar solar = new Solar(2020,5,26,23,43,0); 77 | Lunar lunar = solar.getLunar(); 78 | //[庚子, 辛巳, 庚午, 丙子] 79 | System.out.println(lunar.getBaZi()); 80 | } 81 | 82 | @Test 83 | public void testBazi2Solar7() { 84 | List l = Solar.fromBaZi("庚子", "辛巳", "庚午", "丙子"); 85 | // [2020-05-26 23:00:00, 2020-05-27 00:00:00] 86 | for (Solar solar : l) { 87 | System.out.println(solar.toFullString()); 88 | } 89 | } 90 | 91 | @Test 92 | public void testBazi2Solar() { 93 | List l = Solar.fromBaZi("庚子", "癸未", "乙丑", "丁亥"); 94 | // [2020-07-21 22:00:00, 1960-08-05 22:00:00] 95 | for (Solar solar : l) { 96 | System.out.println(solar.toFullString()); 97 | } 98 | } 99 | 100 | @Test 101 | public void testBazi2Solar2() { 102 | List l = Solar.fromBaZi("庚子", "戊子", "己卯", "庚午"); 103 | // [1960-12-17 12:00:00, 1901-01-01 12:00:00] 104 | for (Solar solar : l) { 105 | System.out.println(solar.toFullString()); 106 | } 107 | } 108 | 109 | @Test 110 | public void testBaziShiShenZhi() { 111 | Solar solar = new Solar(2020,1,1,22,35,0); 112 | Lunar lunar = solar.getLunar(); 113 | //[己亥, 丙子, 癸卯, 癸亥] 114 | System.out.println(lunar.getBaZi()); 115 | //[七杀, 正财, 日主, 比肩] 116 | System.out.println(lunar.getBaZiShiShenGan()); 117 | //[劫财, 比肩, 食神, 劫财] 118 | System.out.println(lunar.getBaZiShiShenZhi()); 119 | //[劫财, 伤官] 120 | System.out.println(lunar.getBaZiShiShenYearZhi()); 121 | //[比肩] 122 | System.out.println(lunar.getBaZiShiShenMonthZhi()); 123 | //[食神] 124 | System.out.println(lunar.getBaZiShiShenDayZhi()); 125 | //[劫财, 伤官] 126 | System.out.println(lunar.getBaZiShiShenTimeZhi()); 127 | } 128 | 129 | } 130 | -------------------------------------------------------------------------------- /src/test/java/sample/HalfYearTest.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import com.nlf.calendar.*; 4 | import org.junit.Test; 5 | 6 | /** 7 | * 半年示例 8 | */ 9 | public class HalfYearTest { 10 | 11 | @Test 12 | public void test(){ 13 | SolarHalfYear halfYear = new SolarHalfYear(); 14 | System.out.println(halfYear); 15 | System.out.println(halfYear.toFullString()); 16 | 17 | for(SolarMonth month:halfYear.getMonths()){ 18 | System.out.println(month.toFullString()); 19 | } 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/sample/HolidayTest.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import com.nlf.calendar.Holiday; 4 | import com.nlf.calendar.Solar; 5 | import com.nlf.calendar.util.HolidayUtil; 6 | import org.junit.Test; 7 | 8 | import java.util.List; 9 | 10 | public class HolidayTest { 11 | 12 | @Test 13 | public void test(){ 14 | System.out.println(HolidayUtil.getHolidays("2017-10")); 15 | System.out.println(HolidayUtil.getHoliday("2020-01-01")); 16 | System.out.println(HolidayUtil.getHoliday("20200102")); 17 | System.out.println(HolidayUtil.getHolidays("202002")); 18 | System.out.println(HolidayUtil.getHolidays("20200201")); 19 | System.out.println(HolidayUtil.getHoliday(1999,1,5)); 20 | System.out.println(HolidayUtil.getHoliday(2019,1,1)); 21 | System.out.println(HolidayUtil.getHolidays(2020,5)); 22 | System.out.println(HolidayUtil.getHolidays(2020)); 23 | 24 | System.out.println(HolidayUtil.getHolidaysByTarget("20200501")); 25 | System.out.println(HolidayUtil.getHolidaysByTarget(2011,1,1)); 26 | } 27 | 28 | @Test 29 | public void test1(){ 30 | StringBuilder s = new StringBuilder(); 31 | for(int i = 2001; i <= 2024; i++) { 32 | List l = HolidayUtil.getHolidays(i); 33 | for (Holiday d : l) { 34 | int index = -1; 35 | for (int x = 0;x expected = new ArrayList(); 38 | expected.add("准提菩萨圣诞"); 39 | Assert.assertEquals(expected, foto.getOtherFestivals()); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/test/FuTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Fu; 4 | import com.nlf.calendar.Lunar; 5 | import com.nlf.calendar.Solar; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | /** 10 | * 三伏测试 11 | * 12 | * @author 6tail 13 | */ 14 | public class FuTest { 15 | 16 | @Test 17 | public void test1(){ 18 | Solar solar = new Solar(2011,7,14); 19 | Lunar lunar = solar.getLunar(); 20 | Fu fu = lunar.getFu(); 21 | Assert.assertEquals(solar.toYmd(),"初伏",fu.toString()); 22 | Assert.assertEquals(solar.toYmd(),"初伏第1天",fu.toFullString()); 23 | } 24 | 25 | @Test 26 | public void test2(){ 27 | Solar solar = new Solar(2011,7,23); 28 | Lunar lunar = solar.getLunar(); 29 | Fu fu = lunar.getFu(); 30 | Assert.assertEquals(solar.toYmd(),"初伏",fu.toString()); 31 | Assert.assertEquals(solar.toYmd(),"初伏第10天",fu.toFullString()); 32 | } 33 | 34 | @Test 35 | public void test3(){ 36 | Solar solar = new Solar(2011,7,24); 37 | Lunar lunar = solar.getLunar(); 38 | Fu fu = lunar.getFu(); 39 | Assert.assertEquals(solar.toYmd(),"中伏",fu.toString()); 40 | Assert.assertEquals(solar.toYmd(),"中伏第1天",fu.toFullString()); 41 | } 42 | 43 | @Test 44 | public void test4(){ 45 | Solar solar = new Solar(2011,8,12); 46 | Lunar lunar = solar.getLunar(); 47 | Fu fu = lunar.getFu(); 48 | Assert.assertEquals(solar.toYmd(),"中伏",fu.toString()); 49 | Assert.assertEquals(solar.toYmd(),"中伏第20天",fu.toFullString()); 50 | } 51 | 52 | @Test 53 | public void test5(){ 54 | Solar solar = new Solar(2011,8,13); 55 | Lunar lunar = solar.getLunar(); 56 | Fu fu = lunar.getFu(); 57 | Assert.assertEquals(solar.toYmd(),"末伏",fu.toString()); 58 | Assert.assertEquals(solar.toYmd(),"末伏第1天",fu.toFullString()); 59 | } 60 | 61 | @Test 62 | public void test6(){ 63 | Solar solar = new Solar(2011,8,22); 64 | Lunar lunar = solar.getLunar(); 65 | Fu fu = lunar.getFu(); 66 | Assert.assertEquals(solar.toYmd(),"末伏",fu.toString()); 67 | Assert.assertEquals(solar.toYmd(),"末伏第10天",fu.toFullString()); 68 | } 69 | 70 | @Test 71 | public void test7(){ 72 | Solar solar = new Solar(2011,7,13); 73 | Lunar lunar = solar.getLunar(); 74 | Fu fu = lunar.getFu(); 75 | Assert.assertNull(solar.toYmd(),fu); 76 | } 77 | 78 | @Test 79 | public void test8(){ 80 | Solar solar = new Solar(2011,8,23); 81 | Lunar lunar = solar.getLunar(); 82 | Fu fu = lunar.getFu(); 83 | Assert.assertNull(solar.toYmd(),fu); 84 | } 85 | 86 | @Test 87 | public void test9(){ 88 | Solar solar = new Solar(2012,7,18); 89 | Lunar lunar = solar.getLunar(); 90 | Fu fu = lunar.getFu(); 91 | Assert.assertEquals(solar.toYmd(),"初伏",fu.toString()); 92 | Assert.assertEquals(solar.toYmd(),"初伏第1天",fu.toFullString()); 93 | } 94 | 95 | @Test 96 | public void test10(){ 97 | Solar solar = new Solar(2012,8,5); 98 | Lunar lunar = solar.getLunar(); 99 | Fu fu = lunar.getFu(); 100 | Assert.assertEquals(solar.toYmd(),"中伏",fu.toString()); 101 | Assert.assertEquals(solar.toYmd(),"中伏第9天",fu.toFullString()); 102 | } 103 | 104 | @Test 105 | public void test11(){ 106 | Solar solar = new Solar(2012,8,8); 107 | Lunar lunar = solar.getLunar(); 108 | Fu fu = lunar.getFu(); 109 | Assert.assertEquals(solar.toYmd(),"末伏",fu.toString()); 110 | Assert.assertEquals(solar.toYmd(),"末伏第2天",fu.toFullString()); 111 | } 112 | 113 | @Test 114 | public void test12(){ 115 | Solar solar = new Solar(2020,7,17); 116 | Lunar lunar = solar.getLunar(); 117 | Fu fu = lunar.getFu(); 118 | Assert.assertEquals(solar.toYmd(),"初伏",fu.toString()); 119 | Assert.assertEquals(solar.toYmd(),"初伏第2天",fu.toFullString()); 120 | } 121 | 122 | @Test 123 | public void test13(){ 124 | Solar solar = new Solar(2020,7,26); 125 | Lunar lunar = solar.getLunar(); 126 | Fu fu = lunar.getFu(); 127 | Assert.assertEquals(solar.toYmd(),"中伏",fu.toString()); 128 | Assert.assertEquals(solar.toYmd(),"中伏第1天",fu.toFullString()); 129 | } 130 | 131 | @Test 132 | public void test14(){ 133 | Solar solar = new Solar(2020,8,24); 134 | Lunar lunar = solar.getLunar(); 135 | Fu fu = lunar.getFu(); 136 | Assert.assertEquals(solar.toYmd(),"末伏",fu.toString()); 137 | Assert.assertEquals(solar.toYmd(),"末伏第10天",fu.toFullString()); 138 | } 139 | 140 | } 141 | -------------------------------------------------------------------------------- /src/test/java/test/GanZhiTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.Solar; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /** 9 | * 干支测试 10 | */ 11 | public class GanZhiTest { 12 | 13 | @Test 14 | public void test(){ 15 | Solar solar = new Solar(2020,1,1,13,22,0); 16 | Lunar lunar = solar.getLunar(); 17 | Assert.assertEquals("己亥",lunar.getYearInGanZhi()); 18 | Assert.assertEquals("己亥",lunar.getYearInGanZhiByLiChun()); 19 | Assert.assertEquals("己亥",lunar.getYearInGanZhiExact()); 20 | 21 | Assert.assertEquals("丙子",lunar.getMonthInGanZhi()); 22 | Assert.assertEquals("丙子",lunar.getMonthInGanZhiExact()); 23 | 24 | 25 | //小寒 26 | solar = new Solar(2020,1,6,13,22,0); 27 | lunar = solar.getLunar(); 28 | Assert.assertEquals("己亥",lunar.getYearInGanZhi()); 29 | Assert.assertEquals("己亥",lunar.getYearInGanZhiByLiChun()); 30 | Assert.assertEquals("己亥",lunar.getYearInGanZhiExact()); 31 | 32 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhi()); 33 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact()); 34 | 35 | 36 | solar = new Solar(2020,1,20,13,22,0); 37 | lunar = solar.getLunar(); 38 | Assert.assertEquals("己亥",lunar.getYearInGanZhi()); 39 | Assert.assertEquals("己亥",lunar.getYearInGanZhiByLiChun()); 40 | Assert.assertEquals("己亥",lunar.getYearInGanZhiExact()); 41 | 42 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhi()); 43 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact()); 44 | 45 | 46 | //春节 47 | solar = new Solar(2020,1,25,13,22,0); 48 | lunar = solar.getLunar(); 49 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 50 | Assert.assertEquals("己亥",lunar.getYearInGanZhiByLiChun()); 51 | Assert.assertEquals("己亥",lunar.getYearInGanZhiExact()); 52 | 53 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhi()); 54 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact()); 55 | 56 | 57 | solar = new Solar(2020,1,30,13,22,0); 58 | lunar = solar.getLunar(); 59 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 60 | Assert.assertEquals("己亥",lunar.getYearInGanZhiByLiChun()); 61 | Assert.assertEquals("己亥",lunar.getYearInGanZhiExact()); 62 | 63 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhi()); 64 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact()); 65 | 66 | 67 | solar = new Solar(2020,2,1,13,22,0); 68 | lunar = solar.getLunar(); 69 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 70 | Assert.assertEquals("己亥",lunar.getYearInGanZhiByLiChun()); 71 | Assert.assertEquals("己亥",lunar.getYearInGanZhiExact()); 72 | 73 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhi()); 74 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact()); 75 | 76 | solar = new Solar(2020,2,4,18,22,0); 77 | lunar = solar.getLunar(); 78 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 79 | Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun()); 80 | Assert.assertEquals("庚子",lunar.getYearInGanZhiExact()); 81 | 82 | Assert.assertEquals("戊寅",lunar.getMonthInGanZhi()); 83 | Assert.assertEquals("戊寅",lunar.getMonthInGanZhiExact()); 84 | 85 | 86 | solar = new Solar(2020,2,5,13,22,0); 87 | lunar = solar.getLunar(); 88 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 89 | Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun()); 90 | Assert.assertEquals("庚子",lunar.getYearInGanZhiExact()); 91 | 92 | Assert.assertEquals("戊寅",lunar.getMonthInGanZhi()); 93 | Assert.assertEquals("戊寅",lunar.getMonthInGanZhiExact()); 94 | 95 | 96 | solar = new Solar(2020,5,22,13,22,0); 97 | lunar = solar.getLunar(); 98 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 99 | Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun()); 100 | Assert.assertEquals("庚子",lunar.getYearInGanZhiExact()); 101 | 102 | Assert.assertEquals("辛巳",lunar.getMonthInGanZhi()); 103 | Assert.assertEquals("辛巳",lunar.getMonthInGanZhiExact()); 104 | 105 | 106 | solar = new Solar(2020,5,23,13,22,0); 107 | lunar = solar.getLunar(); 108 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 109 | Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun()); 110 | Assert.assertEquals("庚子",lunar.getYearInGanZhiExact()); 111 | 112 | Assert.assertEquals("辛巳",lunar.getMonthInGanZhi()); 113 | Assert.assertEquals("辛巳",lunar.getMonthInGanZhiExact()); 114 | 115 | solar = new Solar(2020,5,29,13,22,0); 116 | lunar = solar.getLunar(); 117 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 118 | Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun()); 119 | Assert.assertEquals("庚子",lunar.getYearInGanZhiExact()); 120 | 121 | Assert.assertEquals("辛巳",lunar.getMonthInGanZhi()); 122 | Assert.assertEquals("辛巳",lunar.getMonthInGanZhiExact()); 123 | 124 | solar = new Solar(2020,6,1,13,22,0); 125 | lunar = solar.getLunar(); 126 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 127 | Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun()); 128 | Assert.assertEquals("庚子",lunar.getYearInGanZhiExact()); 129 | 130 | Assert.assertEquals("辛巳",lunar.getMonthInGanZhi()); 131 | Assert.assertEquals("辛巳",lunar.getMonthInGanZhiExact()); 132 | 133 | solar = new Solar(2020,6,29,13,22,0); 134 | lunar = solar.getLunar(); 135 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 136 | Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun()); 137 | Assert.assertEquals("庚子",lunar.getYearInGanZhiExact()); 138 | 139 | Assert.assertEquals("壬午",lunar.getMonthInGanZhi()); 140 | Assert.assertEquals("壬午",lunar.getMonthInGanZhiExact()); 141 | 142 | solar = new Solar(2019,5,1,13,22,0); 143 | lunar = solar.getLunar(); 144 | Assert.assertEquals("己亥",lunar.getYearInGanZhi()); 145 | Assert.assertEquals("己亥",lunar.getYearInGanZhiByLiChun()); 146 | Assert.assertEquals("己亥",lunar.getYearInGanZhiExact()); 147 | 148 | Assert.assertEquals("戊辰",lunar.getMonthInGanZhi()); 149 | Assert.assertEquals("戊辰",lunar.getMonthInGanZhiExact()); 150 | 151 | solar = new Solar(1986,5,29,13,22,0); 152 | lunar = solar.getLunar(); 153 | Assert.assertEquals("丙寅",lunar.getYearInGanZhi()); 154 | Assert.assertEquals("丙寅",lunar.getYearInGanZhiByLiChun()); 155 | Assert.assertEquals("丙寅",lunar.getYearInGanZhiExact()); 156 | 157 | Assert.assertEquals("癸巳",lunar.getMonthInGanZhi()); 158 | Assert.assertEquals("癸巳",lunar.getMonthInGanZhiExact()); 159 | 160 | solar = new Solar(1986,5,1,1,22,0); 161 | lunar = solar.getLunar(); 162 | Assert.assertEquals("丙寅",lunar.getYearInGanZhi()); 163 | Assert.assertEquals("丙寅",lunar.getYearInGanZhiByLiChun()); 164 | Assert.assertEquals("丙寅",lunar.getYearInGanZhiExact()); 165 | 166 | Assert.assertEquals("壬辰",lunar.getMonthInGanZhi()); 167 | Assert.assertEquals("壬辰",lunar.getMonthInGanZhiExact()); 168 | 169 | solar = new Solar(1986,5,6,1,22,0); 170 | lunar = solar.getLunar(); 171 | Assert.assertEquals("丙寅",lunar.getYearInGanZhi()); 172 | Assert.assertEquals("丙寅",lunar.getYearInGanZhiByLiChun()); 173 | Assert.assertEquals("丙寅",lunar.getYearInGanZhiExact()); 174 | 175 | Assert.assertEquals("癸巳",lunar.getMonthInGanZhi()); 176 | Assert.assertEquals("壬辰",lunar.getMonthInGanZhiExact()); 177 | 178 | solar = new Solar(1986,5,6,23,22,0); 179 | lunar = solar.getLunar(); 180 | Assert.assertEquals("丙寅",lunar.getYearInGanZhi()); 181 | Assert.assertEquals("丙寅",lunar.getYearInGanZhiByLiChun()); 182 | Assert.assertEquals("丙寅",lunar.getYearInGanZhiExact()); 183 | 184 | Assert.assertEquals("癸巳",lunar.getMonthInGanZhi()); 185 | Assert.assertEquals("癸巳",lunar.getMonthInGanZhiExact()); 186 | } 187 | 188 | @Test 189 | public void test1() { 190 | Solar solar = new Solar(1996, 1, 16); 191 | Lunar lunar = solar.getLunar(); 192 | Assert.assertEquals("壬子",lunar.getDayInGanZhi()); 193 | } 194 | 195 | @Test 196 | public void test2() { 197 | Solar solar = new Solar(1997, 2, 16); 198 | Lunar lunar = solar.getLunar(); 199 | Assert.assertEquals("己丑",lunar.getDayInGanZhi()); 200 | } 201 | 202 | @Test 203 | public void test3() { 204 | Solar solar = new Solar(1998, 3, 16); 205 | Lunar lunar = solar.getLunar(); 206 | Assert.assertEquals("壬戌",lunar.getDayInGanZhi()); 207 | } 208 | 209 | @Test 210 | public void test4() { 211 | Solar solar = new Solar(1999, 4, 16); 212 | Lunar lunar = solar.getLunar(); 213 | Assert.assertEquals("戊戌",lunar.getDayInGanZhi()); 214 | } 215 | 216 | @Test 217 | public void test5() { 218 | Solar solar = new Solar(2000, 7, 16); 219 | Lunar lunar = solar.getLunar(); 220 | Assert.assertEquals("乙亥",lunar.getDayInGanZhi()); 221 | } 222 | 223 | @Test 224 | public void test6() { 225 | Solar solar = new Solar(2000, 1, 6); 226 | Lunar lunar = solar.getLunar(); 227 | Assert.assertEquals("癸亥",lunar.getDayInGanZhi()); 228 | } 229 | 230 | @Test 231 | public void test7() { 232 | Solar solar = new Solar(2000, 1, 9); 233 | Lunar lunar = solar.getLunar(); 234 | Assert.assertEquals("丙寅",lunar.getDayInGanZhi()); 235 | } 236 | 237 | @Test 238 | public void test8() { 239 | Solar solar = new Solar(2000, 2, 2); 240 | Lunar lunar = solar.getLunar(); 241 | Assert.assertEquals("庚寅",lunar.getDayInGanZhi()); 242 | } 243 | 244 | @Test 245 | public void test9() { 246 | Solar solar = new Solar(2012, 8, 5); 247 | Lunar lunar = solar.getLunar(); 248 | Assert.assertEquals("戊戌",lunar.getDayInGanZhi()); 249 | } 250 | 251 | @Test 252 | public void test10() { 253 | Solar solar = new Solar(2012, 9, 20); 254 | Lunar lunar = solar.getLunar(); 255 | Assert.assertEquals("壬辰",lunar.getYearInGanZhi()); 256 | Assert.assertEquals("己酉",lunar.getMonthInGanZhi()); 257 | Assert.assertEquals("甲申",lunar.getDayInGanZhi()); 258 | } 259 | 260 | @Test 261 | public void test11() { 262 | Solar solar = new Solar(2012, 10, 20); 263 | Lunar lunar = solar.getLunar(); 264 | Assert.assertEquals("壬辰",lunar.getYearInGanZhi()); 265 | Assert.assertEquals("庚戌",lunar.getMonthInGanZhi()); 266 | Assert.assertEquals("甲寅",lunar.getDayInGanZhi()); 267 | } 268 | 269 | @Test 270 | public void test12() { 271 | Solar solar = new Solar(2012, 11, 20); 272 | Lunar lunar = solar.getLunar(); 273 | Assert.assertEquals("壬辰",lunar.getYearInGanZhi()); 274 | Assert.assertEquals("辛亥",lunar.getMonthInGanZhi()); 275 | Assert.assertEquals("乙酉",lunar.getDayInGanZhi()); 276 | } 277 | 278 | @Test 279 | public void test13() { 280 | Solar solar = new Solar(2012, 12, 20); 281 | Lunar lunar = solar.getLunar(); 282 | Assert.assertEquals("壬辰",lunar.getYearInGanZhi()); 283 | Assert.assertEquals("壬子",lunar.getMonthInGanZhi()); 284 | Assert.assertEquals("乙卯",lunar.getDayInGanZhi()); 285 | } 286 | 287 | @Test 288 | public void test14() { 289 | Solar solar = new Solar(2012, 12, 27); 290 | Lunar lunar = solar.getLunar(); 291 | Assert.assertEquals("壬辰",lunar.getYearInGanZhi()); 292 | Assert.assertEquals("壬子",lunar.getMonthInGanZhi()); 293 | Assert.assertEquals("壬戌",lunar.getDayInGanZhi()); 294 | } 295 | 296 | @Test 297 | public void test15() { 298 | Solar solar = new Solar(1988, 2, 15); 299 | Lunar lunar = solar.getLunar(); 300 | Assert.assertEquals("丁卯",lunar.getYearInGanZhi()); 301 | } 302 | 303 | @Test 304 | public void test16() { 305 | Solar solar = new Solar(1988, 2, 15, 23, 30,0); 306 | Lunar lunar = solar.getLunar(); 307 | Assert.assertEquals("丁卯",lunar.getYearInGanZhi()); 308 | Assert.assertEquals("戊辰",lunar.getYearInGanZhiByLiChun()); 309 | Assert.assertEquals("戊辰",lunar.getYearInGanZhiExact()); 310 | } 311 | 312 | @Test 313 | public void test17() { 314 | Solar solar = new Solar(2019, 2, 8, 13, 22, 0); 315 | Lunar lunar = solar.getLunar(); 316 | Assert.assertEquals("己亥", lunar.getYearInGanZhi()); 317 | Assert.assertEquals("己亥", lunar.getYearInGanZhiByLiChun()); 318 | Assert.assertEquals("己亥", lunar.getYearInGanZhiExact()); 319 | 320 | Assert.assertEquals("丙寅", lunar.getMonthInGanZhi()); 321 | Assert.assertEquals("丙寅", lunar.getMonthInGanZhiExact()); 322 | } 323 | 324 | @Test 325 | public void test18() { 326 | Solar solar = new Solar(2020,2,4,13,22,0); 327 | Lunar lunar = solar.getLunar(); 328 | Assert.assertEquals("庚子",lunar.getYearInGanZhi()); 329 | Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun()); 330 | Assert.assertEquals("己亥",lunar.getYearInGanZhiExact()); 331 | 332 | Assert.assertEquals("戊寅",lunar.getMonthInGanZhi()); 333 | Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact()); 334 | } 335 | 336 | @Test 337 | public void test19() { 338 | Solar solar = new Solar(2022,4,6,20,18,0); 339 | Lunar lunar = solar.getLunar(); 340 | Assert.assertEquals("壬寅",lunar.getYearInGanZhi()); 341 | Assert.assertEquals("壬寅",lunar.getYearInGanZhiByLiChun()); 342 | Assert.assertEquals("壬寅",lunar.getYearInGanZhiExact()); 343 | 344 | Assert.assertEquals("甲辰",lunar.getMonthInGanZhi()); 345 | Assert.assertEquals("甲辰",lunar.getMonthInGanZhiExact()); 346 | 347 | Assert.assertEquals("己丑",lunar.getDayInGanZhi()); 348 | Assert.assertEquals("己丑",lunar.getDayInGanZhiExact()); 349 | Assert.assertEquals("己丑",lunar.getDayInGanZhiExact2()); 350 | 351 | Assert.assertEquals("甲戌",lunar.getTimeInGanZhi()); 352 | } 353 | 354 | } 355 | -------------------------------------------------------------------------------- /src/test/java/test/HalfYearTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.SolarHalfYear; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * 半年测试 9 | * 10 | * @author 6tail 11 | */ 12 | public class HalfYearTest { 13 | 14 | @Test 15 | public void test(){ 16 | SolarHalfYear halfYear = new SolarHalfYear(2019,5); 17 | Assert.assertEquals("2019.1",halfYear.toString()); 18 | Assert.assertEquals("2019年上半年",halfYear.toFullString()); 19 | 20 | Assert.assertEquals("2019.2",halfYear.next(1).toString()); 21 | Assert.assertEquals("2019年下半年",halfYear.next(1).toFullString()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/test/HolidayTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Holiday; 4 | import com.nlf.calendar.Solar; 5 | import com.nlf.calendar.util.HolidayUtil; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | public class HolidayTest { 13 | 14 | @Test 15 | public void test2021() { 16 | Map holidays = new HashMap() { 17 | private static final long serialVersionUID = 1L; 18 | 19 | { 20 | put("2021-01-01", "2021-01-01 元旦节 2021-01-01"); 21 | put("2021-01-02", "2021-01-02 元旦节 2021-01-01"); 22 | put("2021-01-03", "2021-01-03 元旦节 2021-01-01"); 23 | 24 | put("2021-02-11", "2021-02-11 春节 2021-02-12"); 25 | put("2021-02-12", "2021-02-12 春节 2021-02-12"); 26 | put("2021-02-13", "2021-02-13 春节 2021-02-12"); 27 | put("2021-02-14", "2021-02-14 春节 2021-02-12"); 28 | put("2021-02-15", "2021-02-15 春节 2021-02-12"); 29 | put("2021-02-16", "2021-02-16 春节 2021-02-12"); 30 | put("2021-02-17", "2021-02-17 春节 2021-02-12"); 31 | 32 | put("2021-02-07", "2021-02-07 春节调休 2021-02-12"); 33 | put("2021-02-20", "2021-02-20 春节调休 2021-02-12"); 34 | 35 | put("2021-04-03", "2021-04-03 清明节 2021-04-04"); 36 | put("2021-04-04", "2021-04-04 清明节 2021-04-04"); 37 | put("2021-04-05", "2021-04-05 清明节 2021-04-04"); 38 | 39 | put("2021-05-01", "2021-05-01 劳动节 2021-05-01"); 40 | put("2021-05-02", "2021-05-02 劳动节 2021-05-01"); 41 | put("2021-05-03", "2021-05-03 劳动节 2021-05-01"); 42 | put("2021-05-04", "2021-05-04 劳动节 2021-05-01"); 43 | put("2021-05-05", "2021-05-05 劳动节 2021-05-01"); 44 | 45 | put("2021-04-25", "2021-04-25 劳动节调休 2021-05-01"); 46 | put("2021-05-08", "2021-05-08 劳动节调休 2021-05-01"); 47 | 48 | put("2021-06-12", "2021-06-12 端午节 2021-06-14"); 49 | put("2021-06-13", "2021-06-13 端午节 2021-06-14"); 50 | put("2021-06-14", "2021-06-14 端午节 2021-06-14"); 51 | 52 | put("2021-09-19", "2021-09-19 中秋节 2021-09-21"); 53 | put("2021-09-20", "2021-09-20 中秋节 2021-09-21"); 54 | put("2021-09-21", "2021-09-21 中秋节 2021-09-21"); 55 | 56 | put("2021-09-18", "2021-09-18 中秋节调休 2021-09-21"); 57 | 58 | put("2021-10-01", "2021-10-01 国庆节 2021-10-01"); 59 | put("2021-10-02", "2021-10-02 国庆节 2021-10-01"); 60 | put("2021-10-03", "2021-10-03 国庆节 2021-10-01"); 61 | put("2021-10-04", "2021-10-04 国庆节 2021-10-01"); 62 | put("2021-10-05", "2021-10-05 国庆节 2021-10-01"); 63 | put("2021-10-06", "2021-10-06 国庆节 2021-10-01"); 64 | put("2021-10-07", "2021-10-07 国庆节 2021-10-01"); 65 | 66 | put("2021-09-26", "2021-09-26 国庆节调休 2021-10-01"); 67 | put("2021-10-09", "2021-10-09 国庆节调休 2021-10-01"); 68 | } 69 | }; 70 | Solar solar = Solar.fromYmd(2021, 1, 1); 71 | while (solar.toYmd().compareTo("2022-01-01") < 0) { 72 | String ymd = solar.toYmd(); 73 | String holiday = holidays.get(ymd); 74 | Assert.assertEquals(ymd, holiday + "", HolidayUtil.getHoliday(ymd) + ""); 75 | 76 | solar = solar.next(1); 77 | } 78 | } 79 | 80 | @Test 81 | public void test() { 82 | Assert.assertEquals("2020-01-01 元旦节 2020-01-01", HolidayUtil.getHoliday("2020-01-01") + ""); 83 | 84 | // 将2020-01-01修改为春节 85 | HolidayUtil.fix("202001011120200101"); 86 | Assert.assertEquals("2020-01-01 春节 2020-01-01", HolidayUtil.getHoliday("2020-01-01") + ""); 87 | 88 | // 追加2099-01-01为元旦节 89 | HolidayUtil.fix("209901010120990101"); 90 | Assert.assertEquals("2099-01-01 元旦节 2099-01-01", HolidayUtil.getHoliday("2099-01-01") + ""); 91 | 92 | // 将2020-01-01修改为春节,并追加2099-01-01为元旦节 93 | HolidayUtil.fix("202001011120200101209901010120990101"); 94 | Assert.assertEquals("2020-01-01 春节 2020-01-01", HolidayUtil.getHoliday("2020-01-01") + ""); 95 | Assert.assertEquals("2099-01-01 元旦节 2099-01-01", HolidayUtil.getHoliday("2099-01-01") + ""); 96 | 97 | // 自定义节假日名称 98 | String[] names = new String[HolidayUtil.NAMES.length]; 99 | for (int i = 0, j = HolidayUtil.NAMES.length; i < j; i++) { 100 | names[i] = HolidayUtil.NAMES[i]; 101 | } 102 | names[0] = "元旦"; 103 | names[1] = "大年初一"; 104 | 105 | HolidayUtil.fix(names, null); 106 | Assert.assertEquals("2020-01-01 大年初一 2020-01-01", HolidayUtil.getHoliday("2020-01-01") + ""); 107 | Assert.assertEquals("2099-01-01 元旦 2099-01-01", HolidayUtil.getHoliday("2099-01-01") + ""); 108 | 109 | // 追加节假日名称和数据 110 | names = new String[12]; 111 | for (int i = 0, j = HolidayUtil.NAMES.length; i < j; i++) { 112 | names[i] = HolidayUtil.NAMES[i]; 113 | } 114 | names[9] = "我的生日"; 115 | names[10] = "结婚纪念日"; 116 | names[11] = "她的生日"; 117 | 118 | HolidayUtil.fix(names, "20210529912021052920211111:12021111120211201;120211201"); 119 | Assert.assertEquals("2021-05-29 我的生日 2021-05-29", HolidayUtil.getHoliday("2021-05-29") + ""); 120 | Assert.assertEquals("2021-11-11 结婚纪念日 2021-11-11", HolidayUtil.getHoliday("2021-11-11") + ""); 121 | Assert.assertEquals("2021-12-01 她的生日 2021-12-01", HolidayUtil.getHoliday("2021-12-01") + ""); 122 | } 123 | 124 | @Test 125 | public void test1() { 126 | Holiday holiday = HolidayUtil.getHoliday(2016,10,4); 127 | Assert.assertNotNull(holiday); 128 | Assert.assertNotNull(holiday.getTarget()); 129 | Assert.assertEquals("2016-10-01",holiday.getTarget()); 130 | } 131 | 132 | @Test 133 | public void testRemove() { 134 | // 设置默认的节假日名称 135 | HolidayUtil.fix(HolidayUtil.NAMES, null); 136 | Holiday holiday = HolidayUtil.getHoliday(2010,1,1); 137 | Assert.assertNotNull(holiday); 138 | Assert.assertEquals("元旦节",holiday.getName()); 139 | 140 | HolidayUtil.fix("20100101~000000000000000000000000000"); 141 | holiday = HolidayUtil.getHoliday(2010,1,1); 142 | Assert.assertNull(holiday); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/test/java/test/JieQiTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.Solar; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | /** 12 | * 节气测试 13 | * 14 | * @author 6tail 15 | */ 16 | public class JieQiTest { 17 | 18 | @Test 19 | public void test2022() { 20 | 21 | Map jieQi = new HashMap(){ 22 | private static final long serialVersionUID = 1L; 23 | { 24 | put("冬至","2021-12-21 23:59:09"); 25 | put("小寒","2022-01-05 17:13:54"); 26 | put("大寒","2022-01-20 10:38:56"); 27 | put("立春","2022-02-04 04:50:36"); 28 | put("雨水","2022-02-19 00:42:50"); 29 | put("惊蛰","2022-03-05 22:43:34"); 30 | put("春分","2022-03-20 23:33:15"); 31 | put("清明","2022-04-05 03:20:03"); 32 | put("谷雨","2022-04-20 10:24:07"); 33 | put("立夏","2022-05-05 20:25:47"); 34 | put("小满","2022-05-21 09:22:25"); 35 | put("芒种","2022-06-06 00:25:38"); 36 | put("夏至","2022-06-21 17:13:40"); 37 | put("小暑","2022-07-07 10:37:49"); 38 | put("大暑","2022-07-23 04:06:49"); 39 | put("立秋","2022-08-07 20:28:57"); 40 | put("处暑","2022-08-23 11:15:59"); 41 | put("白露","2022-09-07 23:32:07"); 42 | put("秋分","2022-09-23 09:03:31"); 43 | put("寒露","2022-10-08 15:22:16"); 44 | put("霜降","2022-10-23 18:35:31"); 45 | put("立冬","2022-11-07 18:45:18"); 46 | put("小雪","2022-11-22 16:20:18"); 47 | put("大雪","2022-12-07 11:46:04"); 48 | } 49 | }; 50 | 51 | Solar solar = Solar.fromYmd(2022, 7, 15); 52 | Map result = solar.getLunar().getJieQiTable(); 53 | for(Map.Entry entry:jieQi.entrySet()){ 54 | String name = entry.getKey(); 55 | Assert.assertEquals(name, entry.getValue(), result.get(name).toYmdHms()); 56 | } 57 | } 58 | 59 | @Test 60 | public void test1986() { 61 | 62 | Map jieQi = new HashMap(){ 63 | private static final long serialVersionUID = 1L; 64 | { 65 | put("冬至","1985-12-22 06:07:40"); 66 | put("小寒","1986-01-05 23:28:02"); 67 | put("大寒","1986-01-20 16:46:12"); 68 | put("立春","1986-02-04 11:07:42"); 69 | put("雨水","1986-02-19 06:57:31"); 70 | put("惊蛰","1986-03-06 05:12:08"); 71 | put("春分","1986-03-21 06:02:41"); 72 | put("清明","1986-04-05 10:06:07"); 73 | put("谷雨","1986-04-20 17:12:08"); 74 | put("立夏","1986-05-06 03:30:36"); 75 | put("小满","1986-05-21 16:27:55"); 76 | put("芒种","1986-06-06 07:44:23"); 77 | put("夏至","1986-06-22 00:29:57"); 78 | put("小暑","1986-07-07 18:00:45"); 79 | put("大暑","1986-07-23 11:24:23"); 80 | put("立秋","1986-08-08 03:45:36"); 81 | put("处暑","1986-08-23 18:25:47"); 82 | put("白露","1986-09-08 06:34:37"); 83 | put("秋分","1986-09-23 15:58:52"); 84 | put("寒露","1986-10-08 22:06:45"); 85 | put("霜降","1986-10-24 01:14:11"); 86 | put("立冬","1986-11-08 01:12:49"); 87 | put("小雪","1986-11-22 22:44:20"); 88 | put("大雪","1986-12-07 18:00:56"); 89 | } 90 | }; 91 | 92 | Solar solar = Solar.fromYmd(1986, 7, 15); 93 | Map result = solar.getLunar().getJieQiTable(); 94 | for(Map.Entry entry:jieQi.entrySet()){ 95 | String name = entry.getKey(); 96 | Assert.assertEquals(name, entry.getValue(), result.get(name).toYmdHms()); 97 | } 98 | } 99 | 100 | @Test 101 | public void test() { 102 | Solar solar = Solar.fromYmd(1986,1,5); 103 | Lunar lunar = solar.getLunar(); 104 | Assert.assertEquals("小寒", lunar.getJie()); 105 | Assert.assertEquals("小寒", lunar.getJieQi()); 106 | Assert.assertEquals("小寒", lunar.getCurrentJieQi().getName()); 107 | Assert.assertEquals("小寒", lunar.getCurrentJie().getName()); 108 | Assert.assertNull(lunar.getCurrentQi()); 109 | Assert.assertEquals("", lunar.getQi()); 110 | Assert.assertEquals("大雪", lunar.getPrevJie().getName()); 111 | Assert.assertEquals("冬至", lunar.getPrevQi().getName()); 112 | Assert.assertEquals("冬至", lunar.getPrevJieQi().getName()); 113 | 114 | solar = Solar.fromYmdHms(1986,1,20,17,0,0); 115 | lunar = solar.getLunar(); 116 | Assert.assertEquals("大寒", lunar.getQi()); 117 | Assert.assertEquals("大寒", lunar.getJieQi()); 118 | Assert.assertEquals("大寒", lunar.getCurrentJieQi().getName()); 119 | Assert.assertEquals("大寒", lunar.getCurrentQi().getName()); 120 | Assert.assertNull(lunar.getCurrentJie()); 121 | Assert.assertEquals("", lunar.getJie()); 122 | Assert.assertEquals("立春", lunar.getNextJie().getName()); 123 | Assert.assertEquals("雨水", lunar.getNextQi().getName()); 124 | Assert.assertEquals("立春", lunar.getNextJieQi().getName()); 125 | solar = Solar.fromYmdHms(1986,1,20,14,0,0); 126 | lunar = solar.getLunar(); 127 | Assert.assertEquals("小寒", lunar.getPrevJie().getName()); 128 | Assert.assertEquals("冬至", lunar.getPrevQi().getName()); 129 | Assert.assertEquals("小寒", lunar.getPrevJieQi().getName()); 130 | 131 | solar = Solar.fromYmd(1986,12,7); 132 | lunar = solar.getLunar(); 133 | Assert.assertEquals("大雪", lunar.getJie()); 134 | Assert.assertEquals("大雪", lunar.getJieQi()); 135 | Assert.assertEquals("大雪", lunar.getCurrentJieQi().getName()); 136 | Assert.assertEquals("大雪", lunar.getCurrentJie().getName()); 137 | Assert.assertNull(lunar.getCurrentQi()); 138 | Assert.assertEquals("", lunar.getQi()); 139 | Assert.assertEquals("大雪", lunar.getNextJie().getName()); 140 | Assert.assertEquals("冬至", lunar.getNextQi().getName()); 141 | Assert.assertEquals("大雪", lunar.getNextJieQi().getName()); 142 | 143 | solar = Solar.fromYmd(1986,1,1); 144 | lunar = solar.getLunar(); 145 | Assert.assertEquals("", lunar.getJie()); 146 | Assert.assertEquals("", lunar.getQi()); 147 | Assert.assertEquals("", lunar.getJieQi()); 148 | Assert.assertNull(lunar.getCurrentJieQi()); 149 | Assert.assertNull(lunar.getCurrentJie()); 150 | Assert.assertNull(lunar.getCurrentQi()); 151 | Assert.assertEquals("大雪", lunar.getPrevJie().getName()); 152 | Assert.assertEquals("冬至", lunar.getPrevQi().getName()); 153 | Assert.assertEquals("冬至", lunar.getPrevJieQi().getName()); 154 | Assert.assertEquals("小寒", lunar.getNextJie().getName()); 155 | Assert.assertEquals("大寒", lunar.getNextQi().getName()); 156 | Assert.assertEquals("小寒", lunar.getNextJieQi().getName()); 157 | 158 | 159 | solar = Solar.fromYmd(2012,12,25); 160 | lunar = solar.getLunar(); 161 | Assert.assertEquals("", lunar.getJie()); 162 | Assert.assertEquals("", lunar.getQi()); 163 | Assert.assertEquals("", lunar.getJieQi()); 164 | Assert.assertNull(lunar.getCurrentJie()); 165 | Assert.assertNull(lunar.getCurrentQi()); 166 | Assert.assertNull(lunar.getCurrentJieQi()); 167 | 168 | Assert.assertEquals("小寒", lunar.getNextJie().getName()); 169 | Assert.assertEquals("大寒", lunar.getNextQi().getName()); 170 | Assert.assertEquals("小寒", lunar.getNextJieQi().getName()); 171 | 172 | Assert.assertEquals("大雪", lunar.getPrevJie().getName()); 173 | Assert.assertEquals("冬至", lunar.getPrevQi().getName()); 174 | Assert.assertEquals("冬至", lunar.getPrevJieQi().getName()); 175 | } 176 | 177 | @Test 178 | public void test7() { 179 | Lunar lunar = Lunar.fromYmd(2012, 9, 1); 180 | Assert.assertEquals("2012-09-07 13:29:01", lunar.getJieQiTable().get("白露").toYmdHms()); 181 | } 182 | 183 | @Test 184 | public void test8() { 185 | Lunar lunar = Lunar.fromYmd(2050, 12, 1); 186 | Assert.assertEquals("2050-12-07 06:40:53", lunar.getJieQiTable().get("DA_XUE").toYmdHms()); 187 | } 188 | 189 | @Test 190 | public void test9() { 191 | Solar solar = Solar.fromYmd(2021, 12, 21); 192 | Assert.assertEquals("冬至", solar.getLunar().getJieQi()); 193 | Assert.assertEquals("", solar.getLunar().getJie()); 194 | Assert.assertEquals("冬至", solar.getLunar().getQi()); 195 | } 196 | 197 | @Test 198 | public void test10() { 199 | Lunar lunar = Solar.fromYmd(2023, 6, 1).getLunar(); 200 | Assert.assertEquals("2022-12-22 05:48:01", lunar.getJieQiTable().get("冬至").toYmdHms()); 201 | } 202 | 203 | @Test 204 | public void test11() { 205 | Lunar lunar = Solar.fromYmd(2025, 3, 5).getLunar(); 206 | Assert.assertEquals("2025-03-05 16:07:02", lunar.getJieQiTable().get("惊蛰").toYmdHms()); 207 | } 208 | 209 | } 210 | -------------------------------------------------------------------------------- /src/test/java/test/JulianDayTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Solar; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * 儒略日测试 9 | * 10 | * @author 6tail 11 | */ 12 | public class JulianDayTest { 13 | 14 | @Test 15 | public void testSolar2JD() { 16 | Solar solar = Solar.fromYmd(2020, 7, 15); 17 | Assert.assertEquals(2459045.5, solar.getJulianDay(), 0); 18 | } 19 | 20 | @Test 21 | public void testJD2Solar() { 22 | Solar solar = Solar.fromJulianDay(2459045.5); 23 | Assert.assertEquals("2020-07-15 00:00:00", solar.toYmdHms()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/test/LiuYaoTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.Solar; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /** 9 | * 六曜测试 10 | * 11 | * @author 6tail 12 | */ 13 | public class LiuYaoTest { 14 | 15 | @Test 16 | public void test1(){ 17 | Solar solar = new Solar(2020,4,23); 18 | Lunar lunar = solar.getLunar(); 19 | Assert.assertEquals(lunar.toString(),"佛灭",lunar.getLiuYao()); 20 | } 21 | 22 | @Test 23 | public void test2(){ 24 | Solar solar = new Solar(2021,1,15); 25 | Lunar lunar = solar.getLunar(); 26 | Assert.assertEquals(lunar.toString(),"友引",lunar.getLiuYao()); 27 | } 28 | 29 | @Test 30 | public void test3(){ 31 | Solar solar = new Solar(2017,1,5); 32 | Lunar lunar = solar.getLunar(); 33 | Assert.assertEquals(lunar.toString(),"先胜",lunar.getLiuYao()); 34 | } 35 | 36 | @Test 37 | public void test4(){ 38 | Solar solar = new Solar(2020,4,10); 39 | Lunar lunar = solar.getLunar(); 40 | Assert.assertEquals(lunar.toString(),"友引",lunar.getLiuYao()); 41 | } 42 | 43 | @Test 44 | public void test5(){ 45 | Solar solar = new Solar(2020,6,11); 46 | Lunar lunar = solar.getLunar(); 47 | Assert.assertEquals(lunar.toString(),"大安",lunar.getLiuYao()); 48 | } 49 | 50 | @Test 51 | public void test6(){ 52 | Solar solar = new Solar(2020,6,1); 53 | Lunar lunar = solar.getLunar(); 54 | Assert.assertEquals(lunar.toString(),"先胜",lunar.getLiuYao()); 55 | } 56 | 57 | @Test 58 | public void test7(){ 59 | Solar solar = new Solar(2020,12,8); 60 | Lunar lunar = solar.getLunar(); 61 | Assert.assertEquals(lunar.toString(),"先负",lunar.getLiuYao()); 62 | } 63 | 64 | @Test 65 | public void test8(){ 66 | Solar solar = new Solar(2020,12,11); 67 | Lunar lunar = solar.getLunar(); 68 | Assert.assertEquals(lunar.toString(),"赤口",lunar.getLiuYao()); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/test/LunarMonthTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.LunarMonth; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * 农历月测试 9 | * 10 | * @author 6tail 11 | */ 12 | public class LunarMonthTest { 13 | 14 | @Test 15 | public void test(){ 16 | LunarMonth month = LunarMonth.fromYm(2023, 1); 17 | Assert.assertEquals(1, month.getIndex()); 18 | Assert.assertEquals("甲寅", month.getGanZhi()); 19 | } 20 | 21 | @Test 22 | public void test1(){ 23 | LunarMonth month = LunarMonth.fromYm(2023, -2); 24 | Assert.assertEquals(3, month.getIndex()); 25 | Assert.assertEquals("丙辰", month.getGanZhi()); 26 | } 27 | 28 | @Test 29 | public void test2(){ 30 | LunarMonth month = LunarMonth.fromYm(2023, 3); 31 | Assert.assertEquals(4, month.getIndex()); 32 | Assert.assertEquals("丁巳", month.getGanZhi()); 33 | } 34 | 35 | @Test 36 | public void test3(){ 37 | LunarMonth month = LunarMonth.fromYm(2024, 1); 38 | Assert.assertEquals(1, month.getIndex()); 39 | Assert.assertEquals("丙寅", month.getGanZhi()); 40 | } 41 | 42 | @Test 43 | public void test4(){ 44 | LunarMonth month = LunarMonth.fromYm(2023, 12); 45 | Assert.assertEquals(13, month.getIndex()); 46 | Assert.assertEquals("丙寅", month.getGanZhi()); 47 | } 48 | 49 | @Test 50 | public void test5(){ 51 | LunarMonth month = LunarMonth.fromYm(2022, 1); 52 | Assert.assertEquals(1, month.getIndex()); 53 | Assert.assertEquals("壬寅", month.getGanZhi()); 54 | } 55 | 56 | @Test 57 | public void test6(){ 58 | LunarMonth month = LunarMonth.fromYm(2023, 9); 59 | Assert.assertEquals("癸亥", month.getGanZhi()); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/test/MonthTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.SolarMonth; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * 月份测试 9 | * 10 | * @author 6tail 11 | */ 12 | public class MonthTest { 13 | 14 | @Test 15 | public void test(){ 16 | SolarMonth month = new SolarMonth(2019,5); 17 | Assert.assertEquals("2019-5",month.toString()); 18 | Assert.assertEquals("2019年5月",month.toFullString()); 19 | 20 | Assert.assertEquals("2019-6",month.next(1).toString()); 21 | Assert.assertEquals("2019年6月",month.next(1).toFullString()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/test/NineStarTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.LunarMonth; 5 | import com.nlf.calendar.LunarYear; 6 | import com.nlf.calendar.Solar; 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | public class NineStarTest { 11 | @Test 12 | public void test1() { 13 | Lunar lunar = Solar.fromYmd(1985, 2, 19).getLunar(); 14 | Assert.assertEquals("六", lunar.getYearNineStar().getNumber()); 15 | Assert.assertEquals("五黄土玉衡", lunar.getDayNineStar().toString()); 16 | } 17 | 18 | @Test 19 | public void test2() { 20 | Lunar lunar = Lunar.fromYmd(2022, 1, 1); 21 | Assert.assertEquals("六白金开阳", lunar.getYearNineStar().toString()); 22 | Assert.assertEquals("四绿木天权", lunar.getDayNineStar().toString()); 23 | } 24 | 25 | @Test 26 | public void test3() { 27 | Lunar lunar = Lunar.fromYmd(2033, 1, 1); 28 | Assert.assertEquals("四绿木天权", lunar.getYearNineStar().toString()); 29 | Assert.assertEquals("一白水天枢", lunar.getDayNineStar().toString()); 30 | } 31 | 32 | @Test 33 | public void test4() { 34 | LunarYear y = LunarYear.fromYear(1985); 35 | Assert.assertEquals("六白金开阳", y.getNineStar().toString()); 36 | } 37 | 38 | @Test 39 | public void test5() { 40 | LunarYear y = LunarYear.fromYear(2022); 41 | Assert.assertEquals("五黄土玉衡", y.getNineStar().toString()); 42 | } 43 | 44 | @Test 45 | public void test6() { 46 | LunarYear y = LunarYear.fromYear(2033); 47 | Assert.assertEquals("三碧木天玑", y.getNineStar().toString()); 48 | } 49 | 50 | @Test 51 | public void test7() { 52 | LunarMonth m = LunarMonth.fromYm(1985, 2); 53 | Assert.assertEquals("四绿木天权", m.getNineStar().toString()); 54 | } 55 | 56 | @Test 57 | public void test8() { 58 | LunarMonth m = LunarMonth.fromYm(2022, 1); 59 | Assert.assertEquals("二黑土天璇", m.getNineStar().toString()); 60 | } 61 | 62 | @Test 63 | public void test9() { 64 | LunarMonth m = LunarMonth.fromYm(2033, 1); 65 | Assert.assertEquals("五黄土玉衡", m.getNineStar().toString()); 66 | } 67 | 68 | @Test 69 | public void test10() { 70 | Lunar d = Lunar.fromYmdHms(2033, 1, 1, 12, 0,0); 71 | Assert.assertEquals("七赤金摇光", d.getTimeNineStar().toString()); 72 | } 73 | 74 | @Test 75 | public void test11() { 76 | Lunar d = Lunar.fromYmdHms(2011, 5, 3, 23, 0,0); 77 | Assert.assertEquals("七赤金摇光", d.getTimeNineStar().toString()); 78 | } 79 | 80 | @Test 81 | public void test12() { 82 | LunarMonth m = LunarMonth.fromYm(2024, 11); 83 | Assert.assertEquals("四绿木天权", m.getNineStar().toString()); 84 | m = LunarMonth.fromYm(2024, 12); 85 | Assert.assertEquals("三碧木天玑", m.getNineStar().toString()); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/test/java/test/SeasonTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.SolarSeason; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * 季度测试 9 | * 10 | * @author 6tail 11 | */ 12 | public class SeasonTest { 13 | 14 | @Test 15 | public void test(){ 16 | SolarSeason season = new SolarSeason(2019,5); 17 | Assert.assertEquals("2019.2",season.toString()); 18 | Assert.assertEquals("2019年2季度",season.toFullString()); 19 | 20 | Assert.assertEquals("2019.3",season.next(1).toString()); 21 | Assert.assertEquals("2019年3季度",season.next(1).toFullString()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/test/ShuJiuTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.ShuJiu; 5 | import com.nlf.calendar.Solar; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | /** 10 | * 数九测试 11 | * 12 | * @author 6tail 13 | */ 14 | public class ShuJiuTest { 15 | 16 | @Test 17 | public void test1(){ 18 | Solar solar = new Solar(2020,12,21); 19 | Lunar lunar = solar.getLunar(); 20 | ShuJiu shuJiu = lunar.getShuJiu(); 21 | Assert.assertEquals(solar.toYmd(),"一九",shuJiu.toString()); 22 | Assert.assertEquals(solar.toYmd(),"一九第1天",shuJiu.toFullString()); 23 | } 24 | 25 | @Test 26 | public void test2(){ 27 | Solar solar = new Solar(2020,12,22); 28 | Lunar lunar = solar.getLunar(); 29 | ShuJiu shuJiu = lunar.getShuJiu(); 30 | Assert.assertEquals(solar.toYmd(),"一九",shuJiu.toString()); 31 | Assert.assertEquals(solar.toYmd(),"一九第2天",shuJiu.toFullString()); 32 | } 33 | 34 | @Test 35 | public void test3(){ 36 | Solar solar = new Solar(2020,1,7); 37 | Lunar lunar = solar.getLunar(); 38 | ShuJiu shuJiu = lunar.getShuJiu(); 39 | Assert.assertEquals(solar.toYmd(),"二九",shuJiu.toString()); 40 | Assert.assertEquals(solar.toYmd(),"二九第8天",shuJiu.toFullString()); 41 | } 42 | 43 | @Test 44 | public void test4(){ 45 | Solar solar = new Solar(2021,1,6); 46 | Lunar lunar = solar.getLunar(); 47 | ShuJiu shuJiu = lunar.getShuJiu(); 48 | Assert.assertEquals(solar.toYmd(),"二九",shuJiu.toString()); 49 | Assert.assertEquals(solar.toYmd(),"二九第8天",shuJiu.toFullString()); 50 | } 51 | 52 | @Test 53 | public void test5(){ 54 | Solar solar = new Solar(2021,1,8); 55 | Lunar lunar = solar.getLunar(); 56 | ShuJiu shuJiu = lunar.getShuJiu(); 57 | Assert.assertEquals(solar.toYmd(),"三九",shuJiu.toString()); 58 | Assert.assertEquals(solar.toYmd(),"三九第1天",shuJiu.toFullString()); 59 | } 60 | 61 | @Test 62 | public void test6(){ 63 | Solar solar = new Solar(2021,3,5); 64 | Lunar lunar = solar.getLunar(); 65 | ShuJiu shuJiu = lunar.getShuJiu(); 66 | Assert.assertEquals(solar.toYmd(),"九九",shuJiu.toString()); 67 | Assert.assertEquals(solar.toYmd(),"九九第3天",shuJiu.toFullString()); 68 | } 69 | 70 | @Test 71 | public void test7(){ 72 | Solar solar = new Solar(2021,7,5); 73 | Lunar lunar = solar.getLunar(); 74 | ShuJiu shuJiu = lunar.getShuJiu(); 75 | Assert.assertNull(solar.toYmd(),shuJiu); 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/test/java/test/SolarTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.Solar; 5 | import com.nlf.calendar.util.SolarUtil; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | /** 10 | * 阳历测试 11 | * 12 | * @author 6tail 13 | */ 14 | public class SolarTest { 15 | 16 | @Test 17 | public void test(){ 18 | Solar date = new Solar(2019,5,1); 19 | Assert.assertEquals("2019-05-01",date.toString()); 20 | Assert.assertEquals("2019-05-01 00:00:00 星期三 (劳动节) 金牛座",date.toFullString()); 21 | Assert.assertEquals("二〇一九年三月廿七",date.getLunar().toString()); 22 | Assert.assertEquals("二〇一九年三月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 纳音[平地木 大林木 平地木 桑柘木] 星期三 西方白虎 星宿[参水猿](吉) 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[艮](东北) 财神方位[坎](正北) 冲[(壬辰)龙] 煞[北]",date.getLunar().toFullString()); 23 | } 24 | 25 | @Test 26 | public void test1(){ 27 | Solar solar = new Solar(2020,5,24,13,0,0); 28 | Assert.assertEquals("二〇二〇年闰四月初二",solar.getLunar().toString()); 29 | } 30 | 31 | @Test 32 | public void test6(){ 33 | Solar solar = new Solar(11,1,1); 34 | Assert.assertEquals("一〇年腊月初八",solar.getLunar().toString()); 35 | } 36 | 37 | @Test 38 | public void test7(){ 39 | Solar solar = new Solar(11,3,1); 40 | Assert.assertEquals("一一年二月初八",solar.getLunar().toString()); 41 | } 42 | 43 | @Test 44 | public void test9(){ 45 | Solar solar = new Solar(26,4,13); 46 | Assert.assertEquals("二六年三月初八",solar.getLunar().toString()); 47 | } 48 | 49 | @Test 50 | public void test10(){ 51 | Assert.assertTrue(SolarUtil.isLeapYear(1500)); 52 | } 53 | 54 | @Test 55 | public void testNext(){ 56 | Solar date = new Solar(2020,1,23); 57 | Assert.assertEquals("2020-01-24",date.next(1).toString()); 58 | // 仅工作日,跨越春节假期 59 | Assert.assertEquals("2020-02-03",date.next(1,true).toString()); 60 | 61 | date = new Solar(2020,2,3); 62 | Assert.assertEquals("2020-01-31",date.next(-3).toString()); 63 | // 仅工作日,跨越春节假期 64 | Assert.assertEquals("2020-01-21",date.next(-3,true).toString()); 65 | 66 | date = new Solar(2020,2,9); 67 | Assert.assertEquals("2020-02-15",date.next(6).toString()); 68 | // 仅工作日,跨越周末 69 | Assert.assertEquals("2020-02-17",date.next(6,true).toString()); 70 | 71 | date = new Solar(2020,1,17); 72 | Assert.assertEquals("2020-01-18",date.next(1).toString()); 73 | // 仅工作日,周日调休按上班算 74 | Assert.assertEquals("2020-01-19",date.next(1,true).toString()); 75 | } 76 | 77 | @Test 78 | public void test11(){ 79 | Solar solar = new Solar(2022, 3, 28); 80 | Assert.assertEquals("全国中小学生安全教育日",solar.getFestivals().get(0)); 81 | } 82 | 83 | @Test 84 | public void test12(){ 85 | Solar solar = new Solar(2022, 1, 1); 86 | Assert.assertEquals("2022-01-02", solar.next(1).toYmd()); 87 | } 88 | 89 | @Test 90 | public void test13(){ 91 | Solar solar = new Solar(2022, 1, 31); 92 | Assert.assertEquals("2022-02-01", solar.next(1).toYmd()); 93 | } 94 | 95 | @Test 96 | public void test14(){ 97 | Solar solar = new Solar(2022, 1, 1); 98 | Assert.assertEquals("2023-01-01", solar.next(365).toYmd()); 99 | } 100 | 101 | @Test 102 | public void test15(){ 103 | Solar solar = new Solar(2023, 1, 1); 104 | Assert.assertEquals("2022-01-01", solar.next(-365).toYmd()); 105 | } 106 | 107 | @Test 108 | public void test16(){ 109 | Solar solar = new Solar(1582, 10, 4); 110 | Assert.assertEquals("1582-10-15", solar.next(1).toYmd()); 111 | } 112 | 113 | @Test 114 | public void test17(){ 115 | Solar solar = new Solar(1582, 10, 4); 116 | Assert.assertEquals("1582-11-01", solar.next(18).toYmd()); 117 | } 118 | 119 | @Test 120 | public void test18(){ 121 | Solar solar = new Solar(1582, 11, 1); 122 | Assert.assertEquals("1582-10-04", solar.next(-18).toYmd()); 123 | } 124 | 125 | @Test 126 | public void test19(){ 127 | Solar solar = new Solar(1582, 11, 1); 128 | Assert.assertEquals("1582-10-15", solar.next(-17).toYmd()); 129 | } 130 | 131 | @Test 132 | public void test20(){ 133 | int days = SolarUtil.getDaysBetween(1582, 10, 4, 1582, 10, 15); 134 | Assert.assertEquals(1, days); 135 | } 136 | 137 | @Test 138 | public void test21(){ 139 | int days = SolarUtil.getDaysBetween(1582, 10, 4, 1582, 11, 1); 140 | Assert.assertEquals(18, days); 141 | } 142 | 143 | @Test 144 | public void test22(){ 145 | int days = SolarUtil.getDaysBetween(1582, 1, 1, 1583, 1, 1); 146 | Assert.assertEquals(355, days); 147 | } 148 | 149 | @Test 150 | public void test23(){ 151 | Solar solar = Solar.fromYmd(1991, 5, 12); 152 | Lunar lunar = solar.getLunar(); 153 | Assert.assertEquals("壬午", lunar.getDayInGanZhi()); 154 | } 155 | 156 | @Test 157 | public void test24(){ 158 | Solar solar = new Solar(1582, 10, 15); 159 | Assert.assertEquals("1582-09-30", solar.next(-5).toYmd()); 160 | } 161 | 162 | @Test 163 | public void test25(){ 164 | Solar solar = new Solar(1582, 10, 15); 165 | Assert.assertEquals("1582-10-04", solar.next(-1).toYmd()); 166 | } 167 | 168 | @Test 169 | public void test26(){ 170 | Solar solar = new Solar(1582, 10, 15); 171 | Assert.assertEquals("1582-09-29", solar.next(-6).toYmd()); 172 | } 173 | 174 | @Test 175 | public void test27(){ 176 | Solar solar = Solar.fromYmd(2023, 8, 31); 177 | Assert.assertEquals("2023-09-30", solar.nextMonth(1).toYmd()); 178 | } 179 | 180 | @Test 181 | public void test28(){ 182 | Solar solar = Solar.fromYmd(2023, 8, 31); 183 | Assert.assertEquals("2023-10-31", solar.nextMonth(2).toYmd()); 184 | } 185 | 186 | @Test 187 | public void test29(){ 188 | Solar solar = Solar.fromYmd(2023, 8, 31); 189 | Assert.assertEquals("2025-08-31", solar.nextYear(2).toYmd()); 190 | } 191 | 192 | @Test 193 | public void test30(){ 194 | Solar solar = Solar.fromYmd(2023, 8, 31); 195 | Assert.assertEquals("2024-02-29", solar.nextMonth(6).toYmd()); 196 | } 197 | 198 | } 199 | -------------------------------------------------------------------------------- /src/test/java/test/SolarWeekTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.SolarWeek; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * 阳历周测试 9 | * 10 | * @author 6tail 11 | */ 12 | public class SolarWeekTest { 13 | 14 | @Test 15 | public void test() { 16 | SolarWeek week = SolarWeek.fromYmd(2022, 5, 1, 0); 17 | Assert.assertEquals(1, week.getIndex()); 18 | } 19 | 20 | @Test 21 | public void test1() { 22 | SolarWeek week = SolarWeek.fromYmd(2022, 5, 7, 0); 23 | Assert.assertEquals(1, week.getIndex()); 24 | } 25 | 26 | @Test 27 | public void test2() { 28 | SolarWeek week = SolarWeek.fromYmd(2022, 5, 8, 0); 29 | Assert.assertEquals(2, week.getIndex()); 30 | } 31 | 32 | @Test 33 | public void test3() { 34 | SolarWeek week = SolarWeek.fromYmd(2022, 5, 1, 1); 35 | Assert.assertEquals(1, week.getIndex()); 36 | } 37 | 38 | @Test 39 | public void test4() { 40 | SolarWeek week = SolarWeek.fromYmd(2022, 5, 2, 1); 41 | Assert.assertEquals(2, week.getIndex()); 42 | } 43 | 44 | @Test 45 | public void test5() { 46 | SolarWeek week = SolarWeek.fromYmd(2022, 5, 8, 1); 47 | Assert.assertEquals(2, week.getIndex()); 48 | } 49 | 50 | @Test 51 | public void test6() { 52 | SolarWeek week = SolarWeek.fromYmd(2021, 11, 1, 0); 53 | Assert.assertEquals(1, week.getIndex()); 54 | } 55 | 56 | @Test 57 | public void test7() { 58 | SolarWeek week = SolarWeek.fromYmd(2021, 11, 1, 1); 59 | Assert.assertEquals(1, week.getIndex()); 60 | } 61 | 62 | @Test 63 | public void test8() { 64 | SolarWeek week = SolarWeek.fromYmd(2021, 5, 2, 2); 65 | Assert.assertEquals(1, week.getIndex()); 66 | } 67 | 68 | @Test 69 | public void test9() { 70 | SolarWeek week = SolarWeek.fromYmd(2021, 5, 4, 2); 71 | Assert.assertEquals(2, week.getIndex()); 72 | } 73 | 74 | @Test 75 | public void test10() { 76 | SolarWeek week = SolarWeek.fromYmd(2022, 3, 6, 0); 77 | Assert.assertEquals(11, week.getIndexInYear()); 78 | } 79 | 80 | @Test 81 | public void test11() { 82 | SolarWeek week = SolarWeek.fromYmd(2022, 3, 6, 1); 83 | Assert.assertEquals(10, week.getIndexInYear()); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/test/TaoTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.Tao; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /** 9 | * 道历测试 10 | * 11 | * @author 6tail 12 | */ 13 | public class TaoTest { 14 | 15 | @Test 16 | public void test() { 17 | Tao tao = Tao.fromLunar(Lunar.fromYmdHms(2021, 10, 17, 18, 0, 0)); 18 | Assert.assertEquals("四七一八年十月十七", tao.toString()); 19 | Assert.assertEquals("道歷四七一八年,天運辛丑年,己亥月,癸酉日。十月十七日,酉時。", tao.toFullString()); 20 | } 21 | 22 | @Test 23 | public void test1() { 24 | Tao tao = Tao.fromYmd(4718, 10, 18); 25 | Assert.assertEquals("[地母娘娘圣诞, 四时会]", tao.getFestivals().toString()); 26 | 27 | tao = Lunar.fromYmd(2021, 10, 18).getTao(); 28 | Assert.assertEquals("[地母娘娘圣诞, 四时会]", tao.getFestivals().toString()); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/test/TimeTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.util.LunarUtil; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import java.util.HashMap; 9 | import java.util.LinkedHashMap; 10 | import java.util.Map; 11 | 12 | /** 13 | * 时辰测试 14 | * 15 | * @author 6tail 16 | */ 17 | public class TimeTest { 18 | 19 | private static final Map ZHI = new LinkedHashMap(){ 20 | private static final long serialVersionUID = -1L; 21 | { 22 | put("23:00","子"); 23 | put("00:59","子"); 24 | put("23:30","子"); 25 | 26 | put("01:00","丑"); 27 | put("02:59","丑"); 28 | put("01:30","丑"); 29 | 30 | put("03:00","寅"); 31 | put("04:59","寅"); 32 | put("03:30","寅"); 33 | 34 | put("05:00","卯"); 35 | put("06:59","卯"); 36 | put("05:30","卯"); 37 | 38 | put("07:00","辰"); 39 | put("08:59","辰"); 40 | put("07:30","辰"); 41 | 42 | put("09:00","巳"); 43 | put("10:59","巳"); 44 | put("09:30","巳"); 45 | 46 | put("11:00","午"); 47 | put("12:59","午"); 48 | put("11:30","午"); 49 | 50 | put("13:00","未"); 51 | put("14:59","未"); 52 | put("13:30","未"); 53 | 54 | put("15:00","申"); 55 | put("16:59","申"); 56 | put("15:30","申"); 57 | 58 | put("17:00","酉"); 59 | put("18:59","酉"); 60 | put("17:30","酉"); 61 | 62 | put("19:00","戌"); 63 | put("20:59","戌"); 64 | put("19:30","戌"); 65 | 66 | put("21:00","亥"); 67 | put("22:59","亥"); 68 | put("21:30","亥"); 69 | 70 | put(null,"子"); 71 | 72 | put("","子"); 73 | put("23:01:01","子"); 74 | put("其他","子"); 75 | 76 | put("21:01:01","亥"); 77 | } 78 | }; 79 | 80 | private static final Map GAN = new LinkedHashMap(){ 81 | private static final long serialVersionUID = -1L; 82 | { 83 | //晚子时 84 | put("2020-4,5,23:00","戊"); 85 | //早子时 86 | put("2020-4,5,00:59","丙"); 87 | //晚子时 88 | put("2020-4,5,23:30","戊"); 89 | 90 | put("2020-4,5,01:00","丁"); 91 | put("2020-4,5,02:59","丁"); 92 | put("2020-4,5,01:30","丁"); 93 | 94 | put("2020-4,5,03:00","戊"); 95 | put("2020-4,5,04:59","戊"); 96 | put("2020-4,5,03:30","戊"); 97 | 98 | put("2020-4,5,05:00","己"); 99 | put("2020-4,5,06:59","己"); 100 | put("2020-4,5,05:30","己"); 101 | 102 | put("2020-4,5,07:00","庚"); 103 | put("2020-4,5,08:59","庚"); 104 | put("2020-4,5,07:30","庚"); 105 | 106 | put("2020-4,5,09:00","辛"); 107 | put("2020-4,5,10:59","辛"); 108 | put("2020-4,5,09:30","辛"); 109 | 110 | put("2020-4,5,11:00","壬"); 111 | put("2020-4,5,12:59","壬"); 112 | put("2020-4,5,11:30","壬"); 113 | 114 | put("2020-4,5,13:00","癸"); 115 | put("2020-4,5,14:59","癸"); 116 | put("2020-4,5,13:30","癸"); 117 | 118 | put("2020-4,5,15:00","甲"); 119 | put("2020-4,5,16:59","甲"); 120 | put("2020-4,5,15:30","甲"); 121 | 122 | put("2020-4,5,17:00","乙"); 123 | put("2020-4,5,18:59","乙"); 124 | put("2020-4,5,17:30","乙"); 125 | 126 | put("2020-4,5,19:00","丙"); 127 | put("2020-4,5,20:59","丙"); 128 | put("2020-4,5,19:30","丙"); 129 | 130 | put("2020-4,5,21:00","丁"); 131 | put("2020-4,5,22:59","丁"); 132 | put("2020-4,5,21:30","丁"); 133 | 134 | put("2020-4,5,null","丙"); 135 | 136 | put("2020-4,5,","丙"); 137 | put("2020-4,5,23:01:01","戊"); 138 | put("2020-4,5,其他","丙"); 139 | put("2020-4,5,0:90","丙"); 140 | 141 | put("2020-4,5,21:01:01","丁"); 142 | 143 | put("2020-4,2,23:00","壬"); 144 | put("2020-4,2,11:20","丙"); 145 | } 146 | }; 147 | 148 | private static final Map GAN_ZHI = new HashMap(){ 149 | private static final long serialVersionUID = -1L; 150 | { 151 | //晚子时 152 | put("2020-4,5,23:00","戊子"); 153 | //早子时 154 | put("2020-4,5,00:59","丙子"); 155 | //晚子时 156 | put("2020-4,5,23:30","戊子"); 157 | 158 | put("2020-4,5,01:00","丁丑"); 159 | put("2020-4,5,02:59","丁丑"); 160 | put("2020-4,5,01:30","丁丑"); 161 | 162 | put("2020-4,5,03:00","戊寅"); 163 | put("2020-4,5,04:59","戊寅"); 164 | put("2020-4,5,03:30","戊寅"); 165 | 166 | put("2020-4,5,05:00","己卯"); 167 | put("2020-4,5,06:59","己卯"); 168 | put("2020-4,5,05:30","己卯"); 169 | 170 | put("2020-4,5,07:00","庚辰"); 171 | put("2020-4,5,08:59","庚辰"); 172 | put("2020-4,5,07:30","庚辰"); 173 | 174 | put("2020-4,5,09:00","辛巳"); 175 | put("2020-4,5,10:59","辛巳"); 176 | put("2020-4,5,09:30","辛巳"); 177 | 178 | put("2020-4,5,11:00","壬午"); 179 | put("2020-4,5,12:59","壬午"); 180 | put("2020-4,5,11:30","壬午"); 181 | 182 | put("2020-4,5,13:00","癸未"); 183 | put("2020-4,5,14:59","癸未"); 184 | put("2020-4,5,13:30","癸未"); 185 | 186 | put("2020-4,5,15:00","甲申"); 187 | put("2020-4,5,16:59","甲申"); 188 | put("2020-4,5,15:30","甲申"); 189 | 190 | put("2020-4,5,17:00","乙酉"); 191 | put("2020-4,5,18:59","乙酉"); 192 | put("2020-4,5,17:30","乙酉"); 193 | 194 | put("2020-4,5,19:00","丙戌"); 195 | put("2020-4,5,20:59","丙戌"); 196 | put("2020-4,5,19:30","丙戌"); 197 | 198 | put("2020-4,5,21:00","丁亥"); 199 | put("2020-4,5,22:59","丁亥"); 200 | put("2020-4,5,21:30","丁亥"); 201 | 202 | put("2020-4,5,null","丙子"); 203 | 204 | put("2020-4,5,","丙子"); 205 | put("2020-4,5,23:01:01","戊子"); 206 | put("2020-4,5,其他","丙子"); 207 | put("2020-4,5,0:90","丙子"); 208 | 209 | put("2020-4,5,20:21:01","丙戌"); 210 | put("2020-4,5,21:01:01","丁亥"); 211 | put("2020-4,5,01:21:01","丁丑"); 212 | 213 | put("2020-4,2,23:00","壬子"); 214 | put("2020-4,2,11:20","丙午"); 215 | put("20204,28,23:20","甲子"); 216 | put("20204,29,00:20","甲子"); 217 | } 218 | }; 219 | 220 | @Test 221 | public void testLunarTimeGanZhi(){ 222 | for(Map.Entry entry:GAN_ZHI.entrySet()){ 223 | int hour = 0,minute = 0; 224 | String time = entry.getKey(); 225 | int year = Integer.parseInt(time.substring(0,4)); 226 | int month = Integer.parseInt(time.substring(4,time.indexOf(","))); 227 | int day = Integer.parseInt(time.substring(time.indexOf(",")+1,time.lastIndexOf(","))); 228 | String hm = time.substring(time.lastIndexOf(",")+1); 229 | if(hm.length()>=5){ 230 | hour = Integer.parseInt(hm.substring(0,2),10); 231 | minute = Integer.parseInt(hm.substring(3,5),10); 232 | } 233 | Lunar lunar = Lunar.fromYmdHms(year,month,day,hour,minute,0); 234 | String ganZhi = entry.getValue(); 235 | Assert.assertEquals(lunar.getYear()+"年"+lunar.getMonthInChinese()+"月"+lunar.getDayInChinese()+" "+hm, ganZhi, lunar.getTimeInGanZhi()); 236 | } 237 | } 238 | @Test 239 | public void testLunarTimeGan(){ 240 | for(Map.Entry entry:GAN.entrySet()){ 241 | int hour = 0,minute = 0; 242 | String time = entry.getKey(); 243 | int year = Integer.parseInt(time.substring(0,4)); 244 | int month = Integer.parseInt(time.substring(4,time.indexOf(","))); 245 | int day = Integer.parseInt(time.substring(time.indexOf(",")+1,time.lastIndexOf(","))); 246 | String hm = time.substring(time.lastIndexOf(",")+1); 247 | if(hm.length()>=5){ 248 | hour = Integer.parseInt(hm.substring(0,2),10); 249 | minute = Integer.parseInt(hm.substring(3,5),10); 250 | } 251 | Lunar lunar = Lunar.fromYmdHms(year,month,day,hour,minute,0); 252 | String gan = entry.getValue(); 253 | Assert.assertEquals(lunar.getYear()+"年"+lunar.getMonthInChinese()+"月"+lunar.getDayInChinese()+" "+hm, gan, lunar.getTimeGan()); 254 | } 255 | } 256 | 257 | 258 | @Test 259 | public void test(){ 260 | for(Map.Entry entry:ZHI.entrySet()){ 261 | String hm = entry.getKey(); 262 | String zhi = entry.getValue(); 263 | Assert.assertEquals(hm, zhi, LunarUtil.convertTime(hm)); 264 | } 265 | } 266 | } 267 | -------------------------------------------------------------------------------- /src/test/java/test/WeekTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Solar; 4 | import com.nlf.calendar.SolarWeek; 5 | import com.nlf.calendar.util.SolarUtil; 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | /** 10 | * 周测试 11 | * 12 | * @author 6tail 13 | */ 14 | public class WeekTest { 15 | 16 | @Test 17 | public void testFromMonday(){ 18 | //一周的开始从星期一开始计 19 | int start = 1; 20 | SolarWeek week = new SolarWeek(2019,5,1,start); 21 | Assert.assertEquals("2019.5.1",week.toString()); 22 | Assert.assertEquals("2019年5月第1周",week.toFullString()); 23 | //当月共几周 24 | Assert.assertEquals(5,SolarUtil.getWeeksOfMonth(week.getYear(),week.getMonth(),start)); 25 | //当周第一天 26 | Assert.assertEquals("2019-04-29",week.getFirstDay().toString()); 27 | //当周第一天(本月) 28 | Assert.assertEquals("2019-05-01",week.getFirstDayInMonth().toString()); 29 | } 30 | 31 | @Test 32 | public void testFromSunday(){ 33 | //一周的开始从星期日开始计 34 | int start = 0; 35 | SolarWeek week = new SolarWeek(2019,5,1,start); 36 | Assert.assertEquals("2019.5.1",week.toString()); 37 | Assert.assertEquals("2019年5月第1周",week.toFullString()); 38 | //当月共几周 39 | Assert.assertEquals(5,SolarUtil.getWeeksOfMonth(week.getYear(),week.getMonth(),start)); 40 | //当周第一天 41 | Assert.assertEquals("2019-04-28",week.getFirstDay().toString()); 42 | //当周第一天(本月) 43 | Assert.assertEquals("2019-05-01",week.getFirstDayInMonth().toString()); 44 | } 45 | 46 | @Test 47 | public void test1(){ 48 | Solar solar = Solar.fromYmd(1582, 10, 1); 49 | Assert.assertEquals(1, solar.getWeek()); 50 | } 51 | 52 | @Test 53 | public void test2(){ 54 | Solar solar = Solar.fromYmd(1582, 10, 15); 55 | Assert.assertEquals(5, solar.getWeek()); 56 | } 57 | 58 | @Test 59 | public void test3(){ 60 | Solar solar = Solar.fromYmd(1129, 11, 17); 61 | Assert.assertEquals(0, solar.getWeek()); 62 | } 63 | 64 | @Test 65 | public void test4(){ 66 | Solar solar = Solar.fromYmd(1129, 11, 1); 67 | Assert.assertEquals(5, solar.getWeek()); 68 | } 69 | 70 | @Test 71 | public void test5(){ 72 | Solar solar = Solar.fromYmd(8, 11, 1); 73 | Assert.assertEquals(4, solar.getWeek()); 74 | } 75 | 76 | @Test 77 | public void test6(){ 78 | Solar solar = Solar.fromYmd(1582, 9, 30); 79 | Assert.assertEquals(0, solar.getWeek()); 80 | } 81 | 82 | @Test 83 | public void test7(){ 84 | Solar solar = Solar.fromYmd(1582, 1, 1); 85 | Assert.assertEquals(1, solar.getWeek()); 86 | } 87 | 88 | @Test 89 | public void test8(){ 90 | Solar solar = Solar.fromYmd(1500, 2, 29); 91 | Assert.assertEquals(6, solar.getWeek()); 92 | } 93 | 94 | @Test 95 | public void test9(){ 96 | Solar solar = Solar.fromYmd(9865, 7, 26); 97 | Assert.assertEquals(3, solar.getWeek()); 98 | } 99 | 100 | @Test 101 | public void test10(){ 102 | Assert.assertEquals(6, Solar.fromYmd(1961, 9, 30).getWeek()); 103 | Assert.assertEquals(6, Solar.fromYmdHms(1961, 9, 30, 23, 59, 59).getWeek()); 104 | Assert.assertEquals(6, Solar.fromYmdHms(1961, 9, 30, 20, 0, 0).getWeek()); 105 | } 106 | 107 | @Test 108 | public void test11(){ 109 | Assert.assertEquals(3, Solar.fromYmdHms(2021, 9, 15, 20, 0, 0).getWeek()); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/test/java/test/WuHouTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Lunar; 4 | import com.nlf.calendar.Solar; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /** 9 | * 物候测试 10 | * 11 | * @author 6tail 12 | */ 13 | public class WuHouTest { 14 | 15 | @Test 16 | public void test1(){ 17 | Solar solar = new Solar(2020,4,23); 18 | Lunar lunar = solar.getLunar(); 19 | Assert.assertEquals(solar.toString(),"萍始生",lunar.getWuHou()); 20 | } 21 | 22 | @Test 23 | public void test2(){ 24 | Solar solar = new Solar(2021,1,15); 25 | Lunar lunar = solar.getLunar(); 26 | Assert.assertEquals(solar.toString(),"雉始雊",lunar.getWuHou()); 27 | } 28 | 29 | @Test 30 | public void test3(){ 31 | Solar solar = new Solar(2017,1,5); 32 | Lunar lunar = solar.getLunar(); 33 | Assert.assertEquals(solar.toString(),"雁北乡",lunar.getWuHou()); 34 | } 35 | 36 | @Test 37 | public void test4(){ 38 | Solar solar = new Solar(2020,4,10); 39 | Lunar lunar = solar.getLunar(); 40 | Assert.assertEquals(solar.toString(),"田鼠化为鴽",lunar.getWuHou()); 41 | } 42 | 43 | @Test 44 | public void test5(){ 45 | Solar solar = new Solar(2020,6,11); 46 | Lunar lunar = solar.getLunar(); 47 | Assert.assertEquals(solar.toString(),"鵙始鸣",lunar.getWuHou()); 48 | } 49 | 50 | @Test 51 | public void test6(){ 52 | Solar solar = new Solar(2020,6,1); 53 | Lunar lunar = solar.getLunar(); 54 | Assert.assertEquals(solar.toString(),"麦秋至",lunar.getWuHou()); 55 | } 56 | 57 | @Test 58 | public void test7(){ 59 | Solar solar = new Solar(2020,12,8); 60 | Lunar lunar = solar.getLunar(); 61 | Assert.assertEquals(solar.toString(),"鹖鴠不鸣",lunar.getWuHou()); 62 | } 63 | 64 | @Test 65 | public void test8(){ 66 | Solar solar = new Solar(2020,12,11); 67 | Lunar lunar = solar.getLunar(); 68 | Assert.assertEquals(solar.toString(),"鹖鴠不鸣",lunar.getWuHou()); 69 | } 70 | 71 | @Test 72 | public void test9(){ 73 | Solar solar = new Solar(1982,12,22); 74 | Lunar lunar = solar.getLunar(); 75 | Assert.assertEquals(solar.toString(),"蚯蚓结",lunar.getWuHou()); 76 | } 77 | 78 | @Test 79 | public void test10(){ 80 | Solar solar = new Solar(2021,12,21); 81 | Lunar lunar = solar.getLunar(); 82 | Assert.assertEquals(solar.toString(),"冬至 初候",lunar.getHou()); 83 | } 84 | 85 | @Test 86 | public void test11(){ 87 | Solar solar = new Solar(2021,12,26); 88 | Lunar lunar = solar.getLunar(); 89 | Assert.assertEquals(solar.toString(),"冬至 二候",lunar.getHou()); 90 | } 91 | 92 | @Test 93 | public void test12(){ 94 | Solar solar = new Solar(2021,12,31); 95 | Lunar lunar = solar.getLunar(); 96 | Assert.assertEquals(solar.toString(),"冬至 三候",lunar.getHou()); 97 | } 98 | 99 | @Test 100 | public void test13(){ 101 | Solar solar = new Solar(2022,1,5); 102 | Lunar lunar = solar.getLunar(); 103 | Assert.assertEquals(solar.toString(),"小寒 初候",lunar.getHou()); 104 | } 105 | 106 | @Test 107 | public void test15(){ 108 | Solar solar = new Solar(2022,8,22); 109 | Lunar lunar = solar.getLunar(); 110 | Assert.assertEquals(solar.toString(),"寒蝉鸣",lunar.getWuHou()); 111 | } 112 | 113 | @Test 114 | public void test16(){ 115 | Solar solar = new Solar(2022,8,23); 116 | Lunar lunar = solar.getLunar(); 117 | Assert.assertEquals(solar.toString(),"鹰乃祭鸟",lunar.getWuHou()); 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/test/java/test/XingZuoTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.Solar; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * 星座测试 9 | * 10 | * @author 6tail 11 | */ 12 | public class XingZuoTest { 13 | 14 | @Test 15 | public void test1(){ 16 | Solar solar = new Solar(2020,3,21); 17 | Assert.assertEquals(solar.toYmd(),"白羊",solar.getXingZuo()); 18 | solar = new Solar(2020,4,19); 19 | Assert.assertEquals(solar.toYmd(),"白羊",solar.getXingZuo()); 20 | } 21 | 22 | @Test 23 | public void test2(){ 24 | Solar solar = new Solar(2020,4,20); 25 | Assert.assertEquals(solar.toYmd(),"金牛",solar.getXingZuo()); 26 | solar = new Solar(2020,5,20); 27 | Assert.assertEquals(solar.toYmd(),"金牛",solar.getXingZuo()); 28 | } 29 | 30 | @Test 31 | public void test3(){ 32 | Solar solar = new Solar(2020,5,21); 33 | Assert.assertEquals(solar.toYmd(),"双子",solar.getXingZuo()); 34 | solar = new Solar(2020,6,21); 35 | Assert.assertEquals(solar.toYmd(),"双子",solar.getXingZuo()); 36 | } 37 | 38 | @Test 39 | public void test4(){ 40 | Solar solar = new Solar(2020,6,22); 41 | Assert.assertEquals(solar.toYmd(),"巨蟹",solar.getXingZuo()); 42 | solar = new Solar(2020,7,22); 43 | Assert.assertEquals(solar.toYmd(),"巨蟹",solar.getXingZuo()); 44 | } 45 | 46 | @Test 47 | public void test5(){ 48 | Solar solar = new Solar(2020,7,23); 49 | Assert.assertEquals(solar.toYmd(),"狮子",solar.getXingZuo()); 50 | solar = new Solar(2020,8,22); 51 | Assert.assertEquals(solar.toYmd(),"狮子",solar.getXingZuo()); 52 | } 53 | 54 | @Test 55 | public void test6(){ 56 | Solar solar = new Solar(2020,8,23); 57 | Assert.assertEquals(solar.toYmd(),"处女",solar.getXingZuo()); 58 | solar = new Solar(2020,9,22); 59 | Assert.assertEquals(solar.toYmd(),"处女",solar.getXingZuo()); 60 | } 61 | 62 | @Test 63 | public void test7(){ 64 | Solar solar = new Solar(2020,9,23); 65 | Assert.assertEquals(solar.toYmd(),"天秤",solar.getXingZuo()); 66 | solar = new Solar(2020,10,23); 67 | Assert.assertEquals(solar.toYmd(),"天秤",solar.getXingZuo()); 68 | } 69 | 70 | @Test 71 | public void test8(){ 72 | Solar solar = new Solar(2020,10,24); 73 | Assert.assertEquals(solar.toYmd(),"天蝎",solar.getXingZuo()); 74 | solar = new Solar(2020,11,22); 75 | Assert.assertEquals(solar.toYmd(),"天蝎",solar.getXingZuo()); 76 | } 77 | 78 | @Test 79 | public void test9(){ 80 | Solar solar = new Solar(2020,11,23); 81 | Assert.assertEquals(solar.toYmd(),"射手",solar.getXingZuo()); 82 | solar = new Solar(2020,12,21); 83 | Assert.assertEquals(solar.toYmd(),"射手",solar.getXingZuo()); 84 | } 85 | 86 | @Test 87 | public void test10(){ 88 | Solar solar = new Solar(2020,12,22); 89 | Assert.assertEquals(solar.toYmd(),"摩羯",solar.getXingZuo()); 90 | solar = new Solar(2021,1,19); 91 | Assert.assertEquals(solar.toYmd(),"摩羯",solar.getXingZuo()); 92 | } 93 | 94 | @Test 95 | public void test11(){ 96 | Solar solar = new Solar(2021,1,20); 97 | Assert.assertEquals(solar.toYmd(),"水瓶",solar.getXingZuo()); 98 | solar = new Solar(2021,2,18); 99 | Assert.assertEquals(solar.toYmd(),"水瓶",solar.getXingZuo()); 100 | } 101 | 102 | @Test 103 | public void test12(){ 104 | Solar solar = new Solar(2021,2,19); 105 | Assert.assertEquals(solar.toYmd(),"双鱼",solar.getXingZuo()); 106 | solar = new Solar(2021,3,20); 107 | Assert.assertEquals(solar.toYmd(),"双鱼",solar.getXingZuo()); 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /src/test/java/test/XunTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.EightChar; 4 | import com.nlf.calendar.Lunar; 5 | import com.nlf.calendar.Solar; 6 | import com.nlf.calendar.util.LunarUtil; 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | /** 14 | * 旬测试 15 | * 16 | * @author 6tail 17 | */ 18 | public class XunTest { 19 | 20 | @Test 21 | public void testXun(){ 22 | Map xun = new HashMap(); 23 | xun.put("甲子","甲子"); 24 | xun.put("乙丑","甲子"); 25 | xun.put("丙寅","甲子"); 26 | xun.put("丁卯","甲子"); 27 | xun.put("戊辰","甲子"); 28 | xun.put("己巳","甲子"); 29 | xun.put("庚午","甲子"); 30 | xun.put("辛未","甲子"); 31 | xun.put("壬申","甲子"); 32 | xun.put("癸酉","甲子"); 33 | xun.put("甲戌","甲戌"); 34 | xun.put("乙亥","甲戌"); 35 | xun.put("丙子","甲戌"); 36 | xun.put("丁丑","甲戌"); 37 | xun.put("戊寅","甲戌"); 38 | xun.put("己卯","甲戌"); 39 | xun.put("庚辰","甲戌"); 40 | xun.put("辛巳","甲戌"); 41 | xun.put("壬午","甲戌"); 42 | xun.put("癸未","甲戌"); 43 | xun.put("甲申","甲申"); 44 | xun.put("乙酉","甲申"); 45 | xun.put("丙戌","甲申"); 46 | xun.put("丁亥","甲申"); 47 | xun.put("戊子","甲申"); 48 | xun.put("己丑","甲申"); 49 | xun.put("庚寅","甲申"); 50 | xun.put("辛卯","甲申"); 51 | xun.put("壬辰","甲申"); 52 | xun.put("癸巳","甲申"); 53 | xun.put("甲午","甲午"); 54 | xun.put("乙未","甲午"); 55 | xun.put("丙申","甲午"); 56 | xun.put("丁酉","甲午"); 57 | xun.put("戊戌","甲午"); 58 | xun.put("己亥","甲午"); 59 | xun.put("庚子","甲午"); 60 | xun.put("辛丑","甲午"); 61 | xun.put("壬寅","甲午"); 62 | xun.put("癸卯","甲午"); 63 | xun.put("甲辰","甲辰"); 64 | xun.put("乙巳","甲辰"); 65 | xun.put("丙午","甲辰"); 66 | xun.put("丁未","甲辰"); 67 | xun.put("戊申","甲辰"); 68 | xun.put("己酉","甲辰"); 69 | xun.put("庚戌","甲辰"); 70 | xun.put("辛亥","甲辰"); 71 | xun.put("壬子","甲辰"); 72 | xun.put("癸丑","甲辰"); 73 | xun.put("甲寅","甲寅"); 74 | xun.put("乙卯","甲寅"); 75 | xun.put("丙辰","甲寅"); 76 | xun.put("丁巳","甲寅"); 77 | xun.put("戊午","甲寅"); 78 | xun.put("己未","甲寅"); 79 | xun.put("庚申","甲寅"); 80 | xun.put("辛酉","甲寅"); 81 | xun.put("壬戌","甲寅"); 82 | xun.put("癸亥","甲寅"); 83 | 84 | for(Map.Entry entry:xun.entrySet()){ 85 | Assert.assertEquals(entry.getValue(), LunarUtil.getXun(entry.getKey())); 86 | } 87 | } 88 | 89 | @Test 90 | public void testXunKong(){ 91 | Map kong = new HashMap(); 92 | kong.put("甲子","戌亥"); 93 | kong.put("乙丑","戌亥"); 94 | kong.put("丙寅","戌亥"); 95 | kong.put("丁卯","戌亥"); 96 | kong.put("戊辰","戌亥"); 97 | kong.put("己巳","戌亥"); 98 | kong.put("庚午","戌亥"); 99 | kong.put("辛未","戌亥"); 100 | kong.put("壬申","戌亥"); 101 | kong.put("癸酉","戌亥"); 102 | kong.put("甲戌","申酉"); 103 | kong.put("乙亥","申酉"); 104 | kong.put("丙子","申酉"); 105 | kong.put("丁丑","申酉"); 106 | kong.put("戊寅","申酉"); 107 | kong.put("己卯","申酉"); 108 | kong.put("庚辰","申酉"); 109 | kong.put("辛巳","申酉"); 110 | kong.put("壬午","申酉"); 111 | kong.put("癸未","申酉"); 112 | kong.put("甲申","午未"); 113 | kong.put("乙酉","午未"); 114 | kong.put("丙戌","午未"); 115 | kong.put("丁亥","午未"); 116 | kong.put("戊子","午未"); 117 | kong.put("己丑","午未"); 118 | kong.put("庚寅","午未"); 119 | kong.put("辛卯","午未"); 120 | kong.put("壬辰","午未"); 121 | kong.put("癸巳","午未"); 122 | kong.put("甲午","辰巳"); 123 | kong.put("乙未","辰巳"); 124 | kong.put("丙申","辰巳"); 125 | kong.put("丁酉","辰巳"); 126 | kong.put("戊戌","辰巳"); 127 | kong.put("己亥","辰巳"); 128 | kong.put("庚子","辰巳"); 129 | kong.put("辛丑","辰巳"); 130 | kong.put("壬寅","辰巳"); 131 | kong.put("癸卯","辰巳"); 132 | kong.put("甲辰","寅卯"); 133 | kong.put("乙巳","寅卯"); 134 | kong.put("丙午","寅卯"); 135 | kong.put("丁未","寅卯"); 136 | kong.put("戊申","寅卯"); 137 | kong.put("己酉","寅卯"); 138 | kong.put("庚戌","寅卯"); 139 | kong.put("辛亥","寅卯"); 140 | kong.put("壬子","寅卯"); 141 | kong.put("癸丑","寅卯"); 142 | kong.put("甲寅","子丑"); 143 | kong.put("乙卯","子丑"); 144 | kong.put("丙辰","子丑"); 145 | kong.put("丁巳","子丑"); 146 | kong.put("戊午","子丑"); 147 | kong.put("己未","子丑"); 148 | kong.put("庚申","子丑"); 149 | kong.put("辛酉","子丑"); 150 | kong.put("壬戌","子丑"); 151 | kong.put("癸亥","子丑"); 152 | 153 | for(Map.Entry entry:kong.entrySet()){ 154 | Assert.assertEquals(entry.getValue(), LunarUtil.getXunKong(entry.getKey())); 155 | } 156 | } 157 | 158 | @Test 159 | public void testXun1(){ 160 | Solar solar = new Solar(2020,11,19,0,0,0); 161 | Lunar lunar = solar.getLunar(); 162 | Assert.assertEquals("甲午",lunar.getYearXun()); 163 | } 164 | 165 | @Test 166 | public void testXunKong1(){ 167 | Solar solar = new Solar(2020,11,19,0,0,0); 168 | Lunar lunar = solar.getLunar(); 169 | Assert.assertEquals("辰巳",lunar.getYearXunKong()); 170 | Assert.assertEquals("午未",lunar.getMonthXunKong()); 171 | Assert.assertEquals("戌亥",lunar.getDayXunKong()); 172 | } 173 | 174 | @Test 175 | public void testBaZiDayXunKong(){ 176 | Solar solar = new Solar(1990,12,23,8,37,0); 177 | Lunar lunar = solar.getLunar(); 178 | EightChar eightChar = lunar.getEightChar(); 179 | Assert.assertEquals("子丑",eightChar.getDayXunKong()); 180 | } 181 | 182 | } 183 | -------------------------------------------------------------------------------- /src/test/java/test/YearTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.LunarYear; 4 | import com.nlf.calendar.SolarYear; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /** 9 | * 年份测试 10 | * 11 | * @author 6tail 12 | */ 13 | public class YearTest { 14 | 15 | @Test 16 | public void test(){ 17 | SolarYear year = new SolarYear(2019); 18 | Assert.assertEquals("2019",year.toString()); 19 | Assert.assertEquals("2019年",year.toFullString()); 20 | 21 | Assert.assertEquals("2020",year.next(1).toString()); 22 | Assert.assertEquals("2020年",year.next(1).toFullString()); 23 | } 24 | 25 | @Test 26 | public void test1(){ 27 | LunarYear year = new LunarYear(2017); 28 | Assert.assertEquals("二龙治水",year.getZhiShui()); 29 | 30 | year = new LunarYear(2018); 31 | Assert.assertEquals("二龙治水",year.getZhiShui()); 32 | 33 | year = new LunarYear(2019); 34 | Assert.assertEquals("八龙治水",year.getZhiShui()); 35 | 36 | year = new LunarYear(5); 37 | Assert.assertEquals("三龙治水",year.getZhiShui()); 38 | } 39 | 40 | @Test 41 | public void test2(){ 42 | LunarYear year = new LunarYear(2017); 43 | Assert.assertEquals("二人分饼",year.getFenBing()); 44 | 45 | year = new LunarYear(2018); 46 | Assert.assertEquals("八人分饼",year.getFenBing()); 47 | 48 | year = new LunarYear(5); 49 | Assert.assertEquals("一人分饼",year.getFenBing()); 50 | } 51 | 52 | @Test 53 | public void test3(){ 54 | LunarYear year = new LunarYear(2021); 55 | Assert.assertEquals("十一牛耕田",year.getGengTian()); 56 | } 57 | 58 | @Test 59 | public void test4(){ 60 | LunarYear year = new LunarYear(2018); 61 | Assert.assertEquals("三日得金",year.getDeJin()); 62 | } 63 | 64 | @Test 65 | public void test5(){ 66 | LunarYear year = new LunarYear(1864); 67 | Assert.assertEquals("上元",year.getYuan()); 68 | } 69 | 70 | @Test 71 | public void test6(){ 72 | LunarYear year = new LunarYear(1923); 73 | Assert.assertEquals("上元",year.getYuan()); 74 | } 75 | 76 | @Test 77 | public void test7(){ 78 | LunarYear year = new LunarYear(1924); 79 | Assert.assertEquals("中元",year.getYuan()); 80 | } 81 | 82 | @Test 83 | public void test8(){ 84 | LunarYear year = new LunarYear(1983); 85 | Assert.assertEquals("中元",year.getYuan()); 86 | } 87 | 88 | @Test 89 | public void test9(){ 90 | LunarYear year = new LunarYear(1984); 91 | Assert.assertEquals("下元",year.getYuan()); 92 | } 93 | 94 | @Test 95 | public void test10(){ 96 | LunarYear year = new LunarYear(2043); 97 | Assert.assertEquals("下元",year.getYuan()); 98 | } 99 | 100 | @Test 101 | public void test11(){ 102 | LunarYear year = new LunarYear(1864); 103 | Assert.assertEquals("一运",year.getYun()); 104 | } 105 | 106 | @Test 107 | public void test12(){ 108 | LunarYear year = new LunarYear(1883); 109 | Assert.assertEquals("一运",year.getYun()); 110 | } 111 | 112 | @Test 113 | public void test13(){ 114 | LunarYear year = new LunarYear(1884); 115 | Assert.assertEquals("二运",year.getYun()); 116 | } 117 | 118 | @Test 119 | public void test14(){ 120 | LunarYear year = new LunarYear(1903); 121 | Assert.assertEquals("二运",year.getYun()); 122 | } 123 | 124 | @Test 125 | public void test15(){ 126 | LunarYear year = new LunarYear(1904); 127 | Assert.assertEquals("三运",year.getYun()); 128 | } 129 | 130 | @Test 131 | public void test16(){ 132 | LunarYear year = new LunarYear(1923); 133 | Assert.assertEquals("三运",year.getYun()); 134 | } 135 | 136 | @Test 137 | public void test17(){ 138 | LunarYear year = new LunarYear(2004); 139 | Assert.assertEquals("八运",year.getYun()); 140 | } 141 | 142 | @Test 143 | public void test18(){ 144 | LunarYear year = new LunarYear(2023); 145 | Assert.assertEquals(384, year.getDayCount()); 146 | } 147 | 148 | @Test 149 | public void test19(){ 150 | LunarYear year = new LunarYear(1517); 151 | Assert.assertEquals(384, year.getDayCount()); 152 | } 153 | 154 | @Test 155 | public void test20(){ 156 | LunarYear year = new LunarYear(1518); 157 | Assert.assertEquals(355, year.getDayCount()); 158 | } 159 | 160 | @Test 161 | public void test21(){ 162 | LunarYear year = new LunarYear(2022); 163 | Assert.assertEquals(355, year.getDayCount()); 164 | } 165 | 166 | @Test 167 | public void test22(){ 168 | LunarYear year = new LunarYear(2021); 169 | Assert.assertEquals(354, year.getDayCount()); 170 | } 171 | 172 | } 173 | -------------------------------------------------------------------------------- /src/test/java/test/YunTest.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import com.nlf.calendar.EightChar; 4 | import com.nlf.calendar.Lunar; 5 | import com.nlf.calendar.Solar; 6 | import com.nlf.calendar.eightchar.Yun; 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | /** 11 | * 起运测试 12 | */ 13 | public class YunTest { 14 | 15 | @Test 16 | public void test1() { 17 | Solar solar = new Solar(1981, 1, 29, 23, 37, 0); 18 | Lunar lunar = solar.getLunar(); 19 | EightChar eightChar = lunar.getEightChar(); 20 | Yun yun = eightChar.getYun(0); 21 | Assert.assertEquals("起运年数", 8, yun.getStartYear()); 22 | Assert.assertEquals("起运月数", 0, yun.getStartMonth()); 23 | Assert.assertEquals("起运天数", 20, yun.getStartDay()); 24 | Assert.assertEquals("起运阳历", "1989-02-18", yun.getStartSolar().toYmd()); 25 | } 26 | 27 | @Test 28 | public void test2() { 29 | Lunar lunar = new Lunar(2019, 12, 12, 11, 22, 0); 30 | System.out.println(lunar.getMonth()); 31 | System.out.println(lunar.toFullString()); 32 | System.out.println(lunar.getSolar().toFullString()); 33 | EightChar eightChar = lunar.getEightChar(); 34 | Yun yun = eightChar.getYun(1); 35 | Assert.assertEquals("起运年数", 0, yun.getStartYear()); 36 | Assert.assertEquals("起运月数", 1, yun.getStartMonth()); 37 | Assert.assertEquals("起运天数", 0, yun.getStartDay()); 38 | Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd()); 39 | } 40 | 41 | @Test 42 | public void test3() { 43 | Solar solar = new Solar(2020, 1, 6, 11, 22, 0); 44 | Lunar lunar = solar.getLunar(); 45 | System.out.println(lunar.getMonth()); 46 | System.out.println(lunar.toFullString()); 47 | System.out.println(lunar.getSolar().toFullString()); 48 | EightChar eightChar = lunar.getEightChar(); 49 | Yun yun = eightChar.getYun(1); 50 | Assert.assertEquals("起运年数", 0, yun.getStartYear()); 51 | Assert.assertEquals("起运月数", 1, yun.getStartMonth()); 52 | Assert.assertEquals("起运天数", 0, yun.getStartDay()); 53 | Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd()); 54 | } 55 | 56 | @Test 57 | public void test4() { 58 | Solar solar = new Solar(2022, 3, 9, 20, 51, 0); 59 | Lunar lunar = solar.getLunar(); 60 | EightChar eightChar = lunar.getEightChar(); 61 | Yun yun = eightChar.getYun(1); 62 | Assert.assertEquals("起运阳历", "2030-12-19", yun.getStartSolar().toYmd()); 63 | } 64 | 65 | @Test 66 | public void test5() { 67 | Solar solar = new Solar(2022, 3, 9, 20, 51, 0); 68 | Lunar lunar = solar.getLunar(); 69 | EightChar eightChar = lunar.getEightChar(); 70 | Yun yun = eightChar.getYun(1, 2); 71 | Assert.assertEquals("起运年数", 8, yun.getStartYear()); 72 | Assert.assertEquals("起运月数", 9, yun.getStartMonth()); 73 | Assert.assertEquals("起运天数", 2, yun.getStartDay()); 74 | Assert.assertEquals("起运阳历", "2030-12-12", yun.getStartSolar().toYmd()); 75 | } 76 | 77 | @Test 78 | public void test6() { 79 | Solar solar = new Solar(2018, 6, 11, 9, 30, 0); 80 | Lunar lunar = solar.getLunar(); 81 | EightChar eightChar = lunar.getEightChar(); 82 | Yun yun = eightChar.getYun(0, 2); 83 | Assert.assertEquals("起运阳历", "2020-03-21", yun.getStartSolar().toYmd()); 84 | } 85 | 86 | } 87 | --------------------------------------------------------------------------------