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 ...
随机推荐
- 【漏洞预警】SaltStack远程命令执行漏洞 /tmp/salt-minions
前言: 2020年5月3日,阿里云应急响应中心监测到近日国外某安全团队披露了SaltStack存在认证绕过致命令执行漏洞以及目录遍历漏洞.在多个微信群和QQ群已经有群友反映中招,请马上修复. 以下 ...
- js 跳出循环
js 循环主要有 for while 主要有三种方式 :break continue return break是跳出当前整个循环语句,循环终止会继续执行该循环之后的代码 而continue是跳过当前循 ...
- 这是一篇每个人都能读懂的最小生成树文章(Kruskal)
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法和数据结构专题的第19篇文章,我们一起来看看最小生成树. 我们先不讲算法的原理,也不讲一些七七八八的概念,因为对于初学者来说,看到 ...
- P1635 跳跃
传送门 观察到\(4x+3=2(2x+1)+1\)以及\(8x+7=2(2(2x+1)+1)+1\) 所以可以把\(xx->2x+12x+1\)当成一个基本变化 则\(xx->4x+3\) ...
- php报错:strip_tags() expects parameter 1 to be string, array given
囧....... 这个表示参数需要字符串,而你传入了数组,所以出错了~ 检查下函数或者方法是否正确,还有参数
- Redis 学习笔记(一) 字符串 SDS
SDS 简单动态字符串. SDS的结构: struct sdshdr{ int len;//记录BUF数组中已使用字节的数量 ,等于SDS所八寸字符串的长度 int free;//记录BUF数组中未使 ...
- 杂记---主要关于PHP导出excel表格学习
今天上午处理了一下WIN7系统的电脑前置话筒和耳机口无法使用的问题,主要现象是耳机插入后没声音,麦插入话筒说话对方也听不到,后置端口一切正常.刚开始判断肯定是设置的问题,于是用另一台电脑百度搜索“wi ...
- NOI Online #2 赛后题解
color 题意 \(\;\) 给定\(p_1,p_2\),要求\(p_1\)的倍数格子填红色,\(p_2\)的倍数格子填蓝色,既是\(p_1\)又是\(p_2\)倍数的格子颜色任选.求是否存在一种填 ...
- An invalid domain [.test.com] was specified for this cookie 原因分析
java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie 以上博客 ...
- 自动配置的Springboot内junit测试单元不能运行
解决测试单元不能运行 问题:测试单元的 @Test 前面没有运行图标 解决 IDEA内:File - Setting - Plugins:搜到JUnitGenerator2.0,安装,重启IDEA 光 ...