1 Star 0 Fork 150

li_yi / xk-time

forked from xkzhangsan / xk-time 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

xk-time

xk-time is a datetime converter calculator formatter calendar and cron expression tool set, based on java8 date and time API, thread safe, easy to use.

时间转换,计算,格式化,解析,日历和cron表达式等的工具,使用java8,线程安全,简单易用,多达20几种常用日期格式化模板。

0.为什么要开发这个工具?

(1)java8以前的Date API设计不太好,使用不方便,往往会有线程安全问题。

xk-time工具包,使用java8 api,其中Instant、LocalDate、LocalDateTime、LocalTime、ZonedDateTime等都是线程安全的类,而且增加了更丰富的方法,在此基础上开发相关工具类,线程安全,让使用更方便。

(2)常见的DateUtil,往往将时间转换,计算,格式化,解析等功能都放在同一个类中,导致类功能复杂,方法太多,查找不方便。

xk-time工具包,将上面功能按照时间转换,时间计算,时间格式化解析分成3个工具类:DateTimeConverterUtil,DateTimeCalculatorUtil,DateTimeFormatterUtil,每个类只做一个种功能,方便使用。

(3)为了将与时间紧密相关的节假日、农历、二十四节气、十二星座和日历等功能集中起来开发成工具,方便使用。

1.Maven 坐标

<dependency>  
  <groupId>com.github.xkzhangsan</groupId>    
  <artifactId>xk-time</artifactId>       
  <version>1.1.0</version>    
</dependency>    

2.日期转换工具类 DateTimeConverterUtil

包含Date、LocalDate、LocalDateTime、LocalTime、Instant、ZonedDateTime、YearMonth和Timestamp的互相转换
注意,ZonedDateTime相关的转换,尤其是其他时间转ZonedDateTime,要注意时间和对应时区一致。

详细使用可以查看相关测试代码。

3.日期计算工具类 DateTimeCalculatorUtil

包括:
(1)获取时间属性方法,get* 比如getYear(Date date) 获取年部分,getMonthCnLong(Date date)获取月份中文,getDayOfWeekCn(Date date),获取星期中文。
(2)获取时间加操作方法,plus* 比如plusYears(Date date, long amountToAdd) 当前时间年增加amountToAdd值。
(3)获取时间减操作方法,minus* 比如minusYears(Date date, long amountToSubtract) 当前时间年减少amountToSubtract值。
(4)获取时间修改属性方法,with* 比如withYear(Date date, long newValue) 修改当前时间年值为newValue。
(5)获取比较2个时间方法,between* 比如betweenYears(Date startInclusive, Date endExclusive) 比较2个时间,获取年部分。
(6)其他常用方法,比如isLeapYear(Date date) 判断是否闰年,isWeekend(Date date) 判断是否周末,isExpiry(String yearMonthStr) 是否过期等
(7)时区转换计算方法,transform*,比如transform(ZonedDateTime zonedDateTime, String zoneId)
(8)比较2个时间大小和相等方法,compare*,比如compare(Date date1, Date date2)
(9)获取准确的起始时间方法,start*,end*,比如startTimeOfMonth() 当月起始时间 当月第一天日期+00:00:00,endTimeOfMonth() 当月最后一天日期+23:59:59。
(10)相同月日比较判断方法,isSameMonthDay*,betweenNextSameMonthDay*,nextSameMonthDay*, 比如用于生日,节日等周期性的日期比较判断。
(11)星座计算方法,getConstellation*,比如getConstellationNameCn(String monthDayStr),根据日期计算星座。
(12)计算指定年月或起始时间区间的时间列表,getList, 比如getDateList(int year, int month),计算指定年月的时间列表。
(13)减少时间精度方法,reduceAccuracyTo
, 比如reduceAccuracyToDay(Date date),减少时间精度到天,其他补0,返回如,2020-04-23 00:00:00。
(14)获取时间戳方法,getEpoch*, 比如getEpochMilli()获取时间戳,getEpochMilliFormat()获取时间戳格式化字符串(yyyy-MM-dd HH:mm:ss)

详细使用可以查看相关测试代码。

4.日期格式化和解析工具类 DateTimeFormatterUtil

包含常用日期格式如:
yyyy-MM-dd
HH:mm:ss
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd'T'HH:mm:ssZ等等

(1)格式化方法, format*, 比如formatToDateStr(Date date) 格式化,返回日期部分,如:yyyy-MM-dd;
format(Date date, DateTimeFormatter formatter) formatter 可以选择已定义好的formatter比如YYYY_MM_DD_HH_MM_SS_FMT(yyyy-MM-dd HH:mm:ss)格式化日期。

(2)解析方法, parse*, 比如parseDateStrToDate(String text) 解析日期yyyy-MM-dd,返回Date;
parseToDate(String text, DateTimeFormatter formatter) 根据 formatter解析为 Date。

(3)自动解析方法,根据字符串特点自动识别解析,smartParse*,比如smartParseToDate(String text) 自动解析Date。

(4)ISO格式(包含T)自动解析方法,根据字符串特点自动识别解析,parseIso*,比如parseIsoToDate(String text) 自动解析Date。

(5)解析时间戳方法, parseEpochMilli*, 比如parseEpochMilliToDate(String text),解析时间戳为Date,如 1590224790000。

(6)解析Date默认格式,parseDateDefaultStr*,比如parseDateDefaultStrToDate(String text)
解析 EEE MMM dd HH:mm:ss zzz yyyy 比如: Sat May 23 17:06:30 CST 2020 为Date。

(7)自定义时区格式化方法,比如 format(Date date, DateTimeFormatter formatter, String zoneId),根据zoneId格式化Date。

注意:格式化和解析与系统时区不同的时间时,使用自定义时区格式化方法,或可以使用withZone方法重新设置时区,比如:
YYYY_MM_DD_HH_MM_SS_SSS_FMT.withZone(ZoneId.of("Europe/Paris") 。

详细使用可以查看相关测试代码。

5.日历工具类 CalendarUtil

包括:
(1)生成指定时间的日历(包含年、月和日层级关系的日历)方法,generateCalendar* 比如generateCalendar(int year, int month) 生成指定年月的日历。
(2)生成指定时间的日历(包含年、月和日层级关系的日历),包含农历和所有节假日信息方法,generateCalendarWithHoliday*, 比generateCalendarWithHoliday(int year, int month, Map<String, String> localHolidayMap,Map<String, String> chineseHolidayMap, Map<String, Integer> dateTypeMap)生成指定年月的日历,包含农历和所有节假日信息,可以自定义节假日和工作日等。

详细使用可以查看相关测试代码。

6.农历日期类 LunarDate

包含:
(1)农历日期年月日计算。
(2)农历岁次,生肖属相计算。
(3)二十四节气计算等
注意: 仅支持公历1901-1950年的农历转换。
农历和二十四节气计算的准确依赖于lunarInfo和solarTermInfo基础数据的准确性和精确度。
根据测试结果,发现本程序和常用万年历基本一致,常用万年历软件中也有很少量计算误差,欢迎提出问题,会不断进行改进和修正。

详细使用可以查看相关测试代码。

7.节假日计算类 Holiday

包含:
(1)公历节假日计算, getLocalHoliday* 比如getLocalHoliday(Date date) 计算date的公历节日,getLocalHoliday(Date date, Map<String, String> localHolidayMap) 可以传入自定义公历节日数据。
(2)农历节假日计算, getChineseHoliday* 比如getChineseHoliday(Date date) 计算date的农历节日,getChineseHoliday(Date date, Map<String, String> chineseHolidayMap) 可以传入自定义农历节日数据。
(3)二十四节气计算, getSolarTerm* 比如getSolarTerm(Date date) 计算date的二十四节气。

注意: 农历和二十四节气使用农历日期类 LunarDate,仅支持公历1901-1950年的计算。

详细使用可以查看相关测试代码。

8.Cron表达式工具类 CronExpressionUtil

cron表达式从左到右(用空格隔开):秒(0-59) 分(0-59) 小时(0-23) 日期(1-31) 月份(1-12的整数或者 JAN-DEC) 星期(1-7的整数或者 SUN-SAT (1=SUN)) 年份(可选,1970-2099)
所有字段均可使用特殊字符:, - * / 分别是枚举,范围,任意,间隔
日期另外可使用:? L W 分别是任意,最后,有效工作日(周一到周五)
星期另外可使用:? L # 分别是任意,最后,每个月第几个星期几
常用cron表达式:
(1)0 0 2 1 * ? * 表示在每月的1日的凌晨2点触发
(2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
(3)0 15 10 ? * 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(4)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
(5)0 15 10 L * ? 每月最后一日的上午10:15触发
(6)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发

包含
(1)验证和格式化Cron表达式方法,isValidExpression和formatExpression。
(2)生成下一个或多个执行时间方法,getNextTime和getNextTimeList。
(3)生成下一个或多个执行时间的日期格式化(yyyy-MM-dd HH:mm:ss)方法,getNextTimeStr和getNextTimeStrList。
(4)对比Cron表达式下一个执行时间是否与指定date相等方法,isSatisfiedBy。

注意: 底层使用 quartz的CronExpression处理。

详细使用可以查看相关测试代码。

参与项目

1.提bug和建议

2.贡献代码

(1)fork项目。
(2)在dev分支修改。
(3)提交pull request。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

时间转换,计算,格式化,解析,日历和cron表达式等工具,使用Java8,线程安全,简单易用。 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/liliang_linux/xk-time.git
git@gitee.com:liliang_linux/xk-time.git
liliang_linux
xk-time
xk-time
master

搜索帮助