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 ...
随机推荐
- .NET Core 反编译dll源码查看
一.可以通过JetBrains dotPeek进行反编译 二.可以通过.NET Reflector和VS自带的反编译工具查看
- 《Docker从入门到跑路》之简介
什么是Docker Docker,中文翻译是"码头工人".根据官方的定义,Docker是以Docker容器为资源分割和调度的基本单元,封装了整个软件运行的环境,为开发者和系统管理员 ...
- 前端——Vue-cli 通过UI页面创建项目
在使用该教程创建项目时请先安装vue ui,具体安装方法请百度 1.打开CMD,输入vue ui 2.点击创建按钮,选择项目目录 3.填写项目名 4.配置项目 选择项目所需要的模块
- 带"反悔"的贪心-超市
题面:https://www.acwing.com/problem/content/description/147/ 超市里有N件商品,每个商品都有利润pi和过期时间di,每天只能卖一件商品,过期商品 ...
- 单调队列+二分 G - Queue 小阳买水果
B. Queue 这个题目会做的很偶然,突然想到的,因为我们要求离这只海象的最远的比他年轻的海象,这个年轻的海象可以用单调栈维护. 就是从前往后遍历一遍,单调栈里面存年龄从小往大的海象,这个为什么这么 ...
- jQuery的事件绑定与触发 - 学习笔记
jQuery的事件绑定与触发 事件绑定 自动触发事件 常用的鼠标事件 事件冒泡和默认行为 事件冒泡 默认行为 获得当前鼠标的位置和按键 jQuery的事件绑定与触发 事件绑定 基本绑定 $(eleme ...
- MATLAB与三大变换
运行 Simulink 有三种方式: z 在 MATLAB 的命令窗口直接键入“Simulink”并回车: z 单击 MATLAB 工具条上的 Simulink 图标: z 在 MATLAB 菜单上选 ...
- 基于R语言的航空公司客户价值分析
分析航空公司现状 1.行业内竞争 民航的竞争除了三大航空公司之间的竞争之外,还将加入新崛起的各类小型航空公司.民营航空公司,甚至国外航空巨头.航空产品生产过剩,产品同质化特征愈加明显,于是航空公司从价 ...
- linux 修改时间同步到BIOS
设置时间和日期例如:将系统日期设定成2020年4月14日的命令 命令 : "date -s 04/14/2020" 将系统时间设定成下午5点55分55秒的命令 命令 : " ...
- tp5.1的事务操作
普通的事务操作很简单,最简单的方式是使用 transaction 方法操作数据库事务, 当闭包中的代码发生异常会自动回滚, 例如: Db::transaction(function () { Db:: ...