# ktime **Repository Path**: tianchaohongyu/ktime ## Basic Information - **Project Name**: ktime - **Description**: 本库是从实际项目中提取的一个强大的轻量级时间库。 本库特点: 1.简单优雅,时间api和基础加减语法一致。 2.轻量级,依赖极少,只要求最基础的jdk8 + kotlin1.2 - **Primary Language**: Kotlin - **License**: Apache-2.0 - **Default Branch**: 1.0 - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2018-10-24 - **Last Updated**: 2021-04-21 ## Categories & Tags **Categories**: utils **Tags**: None ## README # ktime #### 项目介绍 本库是从实际项目中提取的一个强大优雅的轻量级时间库。 本库特点: > 1. 简单优雅,时间api和基础加减语法一致。 > 2. 轻量,0依赖,只要求最基础的jdk8 + kotlin1.2,其中kotlin也可以不依赖而只使用java开发,但单纯使用java开发则无法使用本库带来的优雅语法的特性了。 > 3. 项目默认使用maven打包,由于0依赖特性,甚至可以直接将时间库源文件复制到自己的项目中使用。 > 4. 支持 java.util.Date 系列日期和时间、JDK8的新版LocalDateTime系列日期和时间,并且支持自己拓展其他日期、时间类型。 #### 使用说明 > 1. Dates对象:获取昨日、今日、明日零点的日期、时间戳。 > 2. DateTimes对象:对java.util.Date系列日期、时间进行格式化以及字符串解析。 > 3. 计算器拓展支持:java.util.Date、java.sql.Date、Timestamp、Time、LocalDateTime、LocalDate、LocalTime,其中,java.util.Date、java.sql.Date、Timestamp由于其可继承特性,它们的对应计算器也继承自它们,因此这几个类的时间计算支持连续加减。 > 4. 拓展属性:cDay、cMonth、cDay、cHour、cMinute、cSecond、cMilli、cNano 等方法,用于获取支持计算年、月、日、时、分、秒、毫秒、纳秒的计算器对象。 > 5. 拓展方法:withDate、withTime 等方法,用于指定一个日期、时间对象的日期或时间部分。 > 6. 拓展伪构造函数:Timestamp、Date、Time已快速出构造一个当前日期、时间的java.util.Date下的子类对象。 #### 示范 ```kotlin import org.junit.Test import yu.com.time.* import java.time.temporal.ChronoUnit import java.util.* /** * 使用例子 * created zpf on 2018/10/24 0024 */ class Example { @Test fun example() { println("1.当前时间:java旧版 vs 时间库") println(Date()) println(Timestamp()) println("2.时间增减(获取去年):java旧版 vs 时间库") println(java.sql.Timestamp.valueOf(Date().toLocalDateTime().minusYears(1))) println(Timestamp().cYear - 1) println("3.时间间隔天数(现在和去年之间):java旧版 vs 时间库") val now = Timestamp() //现在 val lastYear = Timestamp().cYear - 1 //去年 println(lastYear.toLocalDateTime().until(now.toLocalDateTime(), ChronoUnit.DAYS)) println(now.cDay - lastYear) println("4.指定日期部分:今年2月1日12点") println(now.withDate(null, 2, 1).withTime(12)) } } ``` ##### 运行效果 ![输入图片说明](https://images.gitee.com/uploads/images/2018/1024/104459_abd8629a_1522755.png "QQ截图20181024104109.png") #### 参与贡献 1. Fork 本项目 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 码云特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)