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时间处理的更多相关文章

  1. atitit.获取北京时间CST 功能api总结 O7

    atitit.获取北京时间CST 功能api总结 O7 1. 获取cst时间(北京时间)两布:1.抓取url timtstamp >>format 到cst 1 2. 设置本机时间  se ...

  2. Java1.5泛型指南中文版(Java1.5 Generic Tutorial)

    Java1.5泛型指南中文版(Java1.5 Generic Tutorial) 英文版pdf下载链接:http://java.sun.com/j2se/1.5/pdf/generics-tutori ...

  3. java1.8--Null Object模式

    整理这篇博客是因为现在在整理java8中的optional,所以觉得很有必要整理下Null Object模式.java.lang.NullPointerException,只要敢自称Java程序员,那 ...

  4. java1.8--1.8入门介绍

    在我之前的工作中,一直使用的是java6.所以即使现在java已经到了1.8,对于1.7增加的新特性我也基本没使用的.在整理一系列1.8的新特性的过程中,我也会添加上1.7增加的特性. 下面的整理可能 ...

  5. 【Linux】【Java】CentOS7安装最新版Java1.8.191运行开发环境

    1.前言 本来在写[Linux][Apatch Tomcat]安装与运行.都快写完了. 结果...我忘记安装 Java 环境 然后...新开了博客编辑页面. 最后...我的那个没了...没了...真的 ...

  6. java1.8新特性(三 关于 ::的用法)

    java1.8 推出了一种::的语法 用法 身边 基本没人用1.8的新API 目前 我也是只处于学习 运用 阶段 有点 知其然不知其所以然 通过后面的学习,及时查漏补缺 一个类中 有 静态方法 ,非静 ...

  7. java1.8操作日期

    java1.8获取年份: int year = Calendar.getInstance().get(Calendar.YEAR); StringBuilder code = new StringBu ...

  8. Java 8 日期时间API使用介绍

    如何正确处理时间 现实生活的世界里,时间是不断向前的,如果向前追溯时间的起点,可能是宇宙出生时,又或是是宇宙出现之前, 但肯定是我们目前无法找到的,我们不知道现在距离时间原点的精确距离.所以我们要表示 ...

  9. Java学习关于时间操作的应用类--Date类、Calendar类及其子类

    Date类 Date类封装了当期时间和日期.与Java1.0定义的原始版的Date类相比,Date类发生了本质的变化.在Java1.1发布时,原始版Date类定义的许多功能被移进Calendar类和D ...

随机推荐

  1. flink系列-10、flink保证数据的一致性

    本文摘自书籍<Flink基础教程> 一.一致性的三种级别 当在分布式系统中引入状态时,自然也引入了一致性问题.一致性实际上是“正确性级别”的另一种说法,即在成功处理故障并恢复之后得到的结果 ...

  2. D. Misha, Grisha and Underground 树链剖分

    D. Misha, Grisha and Underground 这个题目算一个树链剖分的裸题,但是这个时间复杂度注意优化. 这个题目可以选择树剖+线段树,时间复杂度有点高,比较这个本身就有n*log ...

  3. 操作系统实验——PV操作实现生产者消费者模型

    操作系统PV操作之--生产者消费者模型 个人博客主页 参考资料: Java实现PV操作 | 生产者与消费者 浙大公开课 在操作系统的多进程.多线程操作中经常会有因为同步.互斥等等问题引发出的一系列问题 ...

  4. 使用ramdisk启动ubuntu文件系统

    环境 Qemu 4.1 vexpress-ca9 概述 为了减小linux内核的大小,可以把一些外设驱动编译成内核模块,但是在启动ubuntu的时候,需要读取flash,但是此时flash的驱动模块存 ...

  5. 【Kafka】消息队列相关知识

    目录 概述 常用消息队列 常用消息队列对比 应用场景 消息队列的两种模式 概述 消息(Message) 是指在应用系统之间传递的数据.消息可以非常简单,比如只包含文本字符串,也可以更复杂,可能包含嵌入 ...

  6. Linux下ffmpeg交叉编译

    1 获取源代码 git clone -b "branch" https://git.ffmpeg.org/ffmpeg.git "branch" 可以是以下的m ...

  7. 01-Taro打造hello-world应用

    01-Taro打造hello-world应用 一.简介 Taro是由京东凹凸实验室出品,书写一套代码通过 Taro 的编译工具,将源代码分别编译出可以在不同端(微信 / 京东 / 百度 / 支付宝 / ...

  8. 【源码】RingBuffer(二)——消费者

    消费者如何读取数据? 前一篇是生产者的处理,这一篇讲消费者的处理 我们都知道,消费者无非就是不停地从队列中读取数据,处理数据.但是与BlockedQueue不同的是,RingBuffer的消费者不会对 ...

  9. [hdu5372 Segment Game]树状数组

    题意:有两种操作:(1)插入线段,第i次插入的线段左边界为Li,长度为i (2)删除线段,删除第x次插入的线段.每次插入线段之前询问有多少条线段被它覆盖. 思路:由于插入的线段长度是递增的,所以第i次 ...

  10. spring-boot+spring-cloud+maven-module 一个 maven多模块的微服务架构模版

    spring-boot-cloud-module spring-boot+spring-cloud+maven-module 一个 maven多模块的微服务架构模版. 新手上路的绝佳模版,只有必要的配 ...