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. 王颖奇 201771010129《面向对象程序设计(java)》第一周学习总结

    <面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com wangyingqi 程序 ...

  2. 如何发挥Visual Studio 2019强大的编辑功能轻松编辑Keil项目

    本文地址:https://www.cnblogs.com/jqdy/p/12565161.html 习惯了VS的强大编辑功能,对Keil 5越来越深恶痛绝.查阅网络文章后按图索骥初步实现了VS编辑Ke ...

  3. 物流配送中心管理系统(SSM+MYSQL)

    工程项目视频观看地址:www.toutiao.com/i6804066711… 本文首先对系统所涉及到的基础理论知识进行阐述,并在此基础上进行了系统分析.系统分析是平台开发的一个不可缺少的环节,为了能 ...

  4. MySQL基础总结(三)

    ORDER BY排序 ORDER BY默认是ASC(升序),降序是DESC LIMIT限制查询结果显示条数 LIMIT显示条数 LIMIT偏移量,显示条数 到目前为止有关查询数据的操作(DQL) 更新 ...

  5. 【FreeRTOS实战汇总】小白博主的RTOS学习实战快速进阶之路(持续更新)

    博主是个小白,打算把这段时间系统学习RTOS的文章统一整理到这里,另外本文会给出一些参考性资料和指导性建议: 本文宗旨 FreeRTOS 是由Richard Barry在2003年由设计的,由于其设计 ...

  6. failed parsing overlays.

    clearn + rebuild + 重新运行: 删掉模拟器进程 + 重新运行:

  7. JAVA异常以及字节流

    异常 JAVA异常可以分为编译时候出现的异常和执行时候出现的异常 JVM默认处理异常的方法是抛出异常 异常处理 //第一种 try{ 可能会出错的代码 }catch{ 发生异常后处置方法 }final ...

  8. 🏃‍♀️点亮你的Vue技术栈,万字Nuxt.js实践笔记来了~

    前言 作为一位 Vuer(vue开发者),如果还不会这个框架,那么你的 Vue 技术栈还没被点亮. Nuxt.js 是什么 Nuxt.js 官方介绍: Nuxt.js 是一个基于 Vue.js 的通用 ...

  9. 我的linux学习日记day6

    ping -c ping几次的意思-i 每次的间隔-W 最长响应时间为几秒钟 #!/bin/bash -i $ &>/dev/null #无论正确或者错误结果都输出到/dev/ 用户输入 ...

  10. XCode Interface Builder开发——2

    XCode Interface Builder开发--2 简单的练手项目--仿苹果自备的计算器 简介 制作一个简易功能的计算器并非难事,但是其中要考虑的不同情况却仍有许多,稍不留神就会踩坑. 例如: ...