java1.8时间处理
object TimeUtil {
var DEFAULT_FORMAT = DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
var HOUR_FORMAT = DateTimeFormatter.ofPattern("yyyyMMddHH")
var DAY_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd")
var MONTH_FORMAT = DateTimeFormatter.ofPattern("yyyyMM")
var YEAR_FORMAT = DateTimeFormatter.ofPattern("yyyy")
/**
* 以List[String]形式返回两个日期之间的所有日期
* 如 begin:20190903 end :20190906 返回[20190903,20190904,20190905,20190906]
*
* @param localStart
* @param localEnd
* @return
*/
def findDatesBetween(localStart: LocalDateTime, localEnd: LocalDateTime): util.LinkedList[String] = {
var localDateList = new util.LinkedList[String]()
try {
if (localStart.toLocalDate.equals(localEnd.toLocalDate)) {
localDateList.add(DAY_FORMAT.format(localStart.toLocalDate))
return localDateList
}
var length = ChronoUnit.DAYS.between(localStart.toLocalDate, localEnd.toLocalDate)
while (length >= 0) {
localDateList.add(DAY_FORMAT.format(localEnd.toLocalDate.minusDays(length)))
length = length - 1
}
} catch {
case ex => {
println(ex.getStackTrace.mkString(","))
}
}
return localDateList
}
def findHoursBetween(localStart: LocalDateTime, localEnd: LocalDateTime): util.LinkedList[String] = {
var localDateList = new util.LinkedList[String]();
try {
if (localStart.toString.equals(localEnd.toString)) {
localDateList.add(HOUR_FORMAT.format(localStart))
return localDateList
}
var local = localEnd
var length = ChronoUnit.HOURS.between(localStart, localEnd)
while (length >= 0) {
localDateList.add(HOUR_FORMAT.format(local))
local = local.minusHours(1)
length = length - 1
}
} catch {
case ex => {
println(ex.getStackTrace.mkString(","))
}
}
return localDateList
}
/**
* 计算粒度时间间天数
*
* @param time
* @param granul
* @return
*/
def findDays(start: LocalDateTime, end: LocalDateTime): Long = {
// jode Days.daysBetween
return ChronoUnit.DAYS.between(start.toLocalDate, end.toLocalDate) + 1
}
/**
* 计算指定粒度的时间间隔的天数
*
* @param time
* @param granul
* @return
*/
def calDays(time: String, granul: String): Long = {
var end = findDayEnd(time, granul)
var start = findDayStart(time, granul)
if (end.isAfter(LocalDateTime.now)) {
end = LocalDateTime.now
}
if (start.toLocalDate.equals(end.toLocalDate)) {
return 1L
}
var res = findDays(start, end)
if (res < 0) {
return 1L
}
if (end.toLocalDate.equals(LocalDate.now())) {
res = res - 1
}
res
}
def findDayStart(time: String, granul: String): LocalDateTime = {
if (Constants.WEEK.equalsIgnoreCase(granul)) {
return getWeekStart(time)
} else if (Constants.MONTH.equalsIgnoreCase(granul)) {
return getMonthStart(time)
} else if (Constants.SEASON.equalsIgnoreCase(granul)) {
return getSeasonStart(time)
} else if (Constants.YEAR.equalsIgnoreCase(granul)) {
return getYearStart(time)
} else if (Constants.HOUR.equals(granul)) {
if (time.trim.length >= 11) {
return LocalDateTime.parse(time.replaceAll("-", "").substring(0, 10) + "0000",
new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss").toFormatter())
}
return LocalDateTime.parse(time.replaceAll("-", "").substring(0, 8) + "000000",
new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss").toFormatter())
} else {
return LocalDateTime.parse(time.replaceAll("-|\\s", "") + "000000",
new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss").toFormatter())
}
}
def findDayEnd(time: String, granul: String): LocalDateTime = {
if (Constants.WEEK.equalsIgnoreCase(granul)) {
return getWeekEnd(time)
} else if (Constants.MONTH.equalsIgnoreCase(granul)) {
return getMonthEnd(time)
} else if (Constants.SEASON.equalsIgnoreCase(granul)) {
return getSeasonEnd(time)
} else if (Constants.YEAR.equalsIgnoreCase(granul)) {
return getYearEnd(time)
} else if (Constants.HOUR.equals(granul)) {
if (time.trim.length >= 11) {
return LocalDateTime.parse(time.replaceAll("-|\\s", "").substring(0, 10) + "5959",
new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss").toFormatter())
}
return LocalDateTime.parse(time.replaceAll("-", "").substring(0, 8) + "235959",
new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss").toFormatter())
} else {
return LocalDateTime.parse(time.replaceAll("-", "").substring(0, 8) + "235959",
new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss").toFormatter())
}
}
/**
*
* @param time 2020 2020年第一天时间,即 2020-01-01
* @return
*/
def getYearStart(time: String): LocalDateTime = {
val yearFirstDate = LocalDateTime.now
.withYear(Integer.valueOf(time)).withMonth(1).withDayOfMonth(1)
return getMin(yearFirstDate)
}
/**
*
* @param time 2020 2020年最后一天时间,即 2020-12-31
* @return
*/
def getYearEnd(time: String): LocalDateTime = {
var yearMonth = LocalDateTime.now
.withYear(Integer.valueOf(time))
.withMonth(12)
var yearLastDateTime = getMax(yearMonthr.`with`(TemporalAdjusters.lastDayOfMonth()))
return yearLastDateTime
}
/**
*
* @param time 2020-02 2020年第二季度最后一天时间,即 2020-06-30 23:59:59
* @return
*/
def getSeasonEnd(time: String): LocalDateTime = {
var r = LocalDateTime.now
.withYear(Integer.valueOf(time.substring(0, time.lastIndexOf("-"))))
.withMonth((Integer.valueOf(time.substring(time.lastIndexOf("-") + 1, time.length))) * 3)
var seasonLastDateTime = getMax(r.r.`with`(TemporalAdjusters.lastDayOfMonth()))
return seasonLastDateTime
}
/**
*
* @param time 2020-02 2020年第二季度第一天时间,即 2020-04-01
* @return
*/
def getSeasonStart(time: String): LocalDateTime = {
val seasonFirstDate = LocalDateTime.now
.withYear(Integer.valueOf(time.substring(0, time.lastIndexOf("-"))))
.withMonth((Integer.valueOf(time.substring(time.lastIndexOf("-") + 1)) - 1) * 3 + 1).withDayOfMonth(1)
return getMin(seasonFirstDate)
}
/**
*
* @param time 2020-02 2020年2月第一天时间,即 2020-02-01
* @return
*/
def getMonthStart(time: String): LocalDateTime = {
val now = LocalDateTime.now
var r = now.withYear(Integer.valueOf(time.substring(0, time.lastIndexOf("-"))))
.withMonth(Integer.valueOf(time.substring(time.lastIndexOf("-") + 1, time.length)))
return getMin(r.withDayOfMonth(1))
}
/**
*
* @param time 2020-02 2020年2月最后一天时间,即 2020-02-29
* @return
*/
def getMonthEnd(time: String): LocalDateTime = {
val now = LocalDateTime.now
var r = now.withYear(Integer.valueOf(time.substring(0, time.lastIndexOf("-"))))
.withMonth(Integer.valueOf(time.substring(time.lastIndexOf("-") + 1, time.length)))
var monthLastDateTime = getMax(rr.`with`(TemporalAdjusters.lastDayOfMonth()))
return monthLastDateTime
}
/**
*
* @param time 2020-03 2020年第三周开始时间,
* @return
*/
def getWeekStart(time: String): LocalDateTime = {
return getMin(LocalDate.parse(time,
new DateTimeFormatterBuilder().appendPattern("YYYY-w").parseDefaulting(WeekFields.ISO.dayOfWeek(), 1).toFormatter()).atStartOfDay())
}
/**
*
* @param time 2020-03 2020年第三周最后一天日期,如果当前时间在第三周内,end时间则为当前时间
* @return
*/
def getWeekEnd(time: String): LocalDateTime = {
var lastDateTime = getMax(LocalDate.parse(time,
new DateTimeFormatterBuilder().appendPattern("YYYY-w").parseDefaulting(WeekFields.ISO.dayOfWeek(), 1).toFormatter()).atStartOfDay() .`with`(TemporalAdjusters.
next(DayOfWeek.SUNDAY)))
return lastDateTime }
def parse(tt: String): LocalDateTime = {
if (StringUtils.isBlank(tt)) {
throw new NullPointerException() }
var t = tt.replaceAll("-|\\s", "")
var pattern = "yyyyMMddHHmmss"
if (t.length == 10) { pattern =
"yyyyMMddHH"
}
return LocalDateTime.parse(t,
new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter()) }
def getMin(tt: LocalDateTime): LocalDateTime = {
return tt.withHour(0).withMinute(0).withSecond(0) }
def getMax(tt: LocalDateTime): LocalDateTime = {
return tt.withHour(23).withMinute(59).withSecond(59) }
java1.8时间处理的更多相关文章
- atitit.获取北京时间CST 功能api总结 O7
atitit.获取北京时间CST 功能api总结 O7 1. 获取cst时间(北京时间)两布:1.抓取url timtstamp >>format 到cst 1 2. 设置本机时间 se ...
- Java1.5泛型指南中文版(Java1.5 Generic Tutorial)
Java1.5泛型指南中文版(Java1.5 Generic Tutorial) 英文版pdf下载链接:http://java.sun.com/j2se/1.5/pdf/generics-tutori ...
- java1.8--Null Object模式
整理这篇博客是因为现在在整理java8中的optional,所以觉得很有必要整理下Null Object模式.java.lang.NullPointerException,只要敢自称Java程序员,那 ...
- java1.8--1.8入门介绍
在我之前的工作中,一直使用的是java6.所以即使现在java已经到了1.8,对于1.7增加的新特性我也基本没使用的.在整理一系列1.8的新特性的过程中,我也会添加上1.7增加的特性. 下面的整理可能 ...
- 【Linux】【Java】CentOS7安装最新版Java1.8.191运行开发环境
1.前言 本来在写[Linux][Apatch Tomcat]安装与运行.都快写完了. 结果...我忘记安装 Java 环境 然后...新开了博客编辑页面. 最后...我的那个没了...没了...真的 ...
- java1.8新特性(三 关于 ::的用法)
java1.8 推出了一种::的语法 用法 身边 基本没人用1.8的新API 目前 我也是只处于学习 运用 阶段 有点 知其然不知其所以然 通过后面的学习,及时查漏补缺 一个类中 有 静态方法 ,非静 ...
- java1.8操作日期
java1.8获取年份: int year = Calendar.getInstance().get(Calendar.YEAR); StringBuilder code = new StringBu ...
- Java 8 日期时间API使用介绍
如何正确处理时间 现实生活的世界里,时间是不断向前的,如果向前追溯时间的起点,可能是宇宙出生时,又或是是宇宙出现之前, 但肯定是我们目前无法找到的,我们不知道现在距离时间原点的精确距离.所以我们要表示 ...
- Java学习关于时间操作的应用类--Date类、Calendar类及其子类
Date类 Date类封装了当期时间和日期.与Java1.0定义的原始版的Date类相比,Date类发生了本质的变化.在Java1.1发布时,原始版Date类定义的许多功能被移进Calendar类和D ...
随机推荐
- tomcat 在linux下启动时找不到JDK
方案一. 修改bashrc (转载: https://www.cnblogs.com/hongzg1982/articles/2101792.html) $ vim ~/.bashrc #加入JA ...
- PLAI那些事_07 FAE with Deferred Substitution
FAE-parse : 一成不变 FAE-Value : interp的最终转让值 ;;numV: value ;;closureV: param-FAE(或value,或function) pair ...
- 龟兔赛跑算法 floyed判环算法
今天写线段树写到要用到这个算法的题目,简单的学习一下. https://blog.csdn.net/javaisnotgood/article/details/89243876 https://blo ...
- Coursera课程笔记----计算导论与C语言基础----Week 1
计算机的基本原理(Week 1) 第一次数学危机 公元前500年,毕达哥拉斯学派,他们相信数是万物的本源:一切数均可表示成整数或者整数之比 然而毕达哥拉斯证明了勾股定理,某些直角三角形的三边比不能用整 ...
- MinorGC前检查
- 就没有我遇不到的报错!java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/filter/Filter
本来准备用HBase的Bulkload将HDFS的HFile文件导入到HBase的myuser2表中,用的是yarn jar的命令 yarn jar /export/servers/hbase-1.2 ...
- 有感FOC算法学习与实现总结
文章目录 基于STM32的有感FOC算法学习与实现总结 1 前言 2 FOC算法架构 3 坐标变换 3.1 Clark变换 3.2 Park变换 3.3 Park反变换 4 SVPWM 5 反馈部分 ...
- js前端获取当前日期,日期格式为yyyy-mm-dd HH:MM
var date = new Date(); var year = date.getFullYear(); var month = date.getMonth()+1; var day = date. ...
- html中require.config 缓存问题
在html中,require的官方基本用法如下: <!DOCTYPE html> <html> <head> <title>My Sample Proj ...
- 2、接口测试(Composer)
前言 Fiddler最大的优势在于抓包,我们大部分使用的功能也在抓包的功能上,fiddler做接口测试也是非常方便的. 对应没有接口测试文档的时候,可以直接抓完包后,copy请求参数,修改下就可以了. ...