jdk8处理时间
对当前时间格式化:
public static long getCurrentTimeMillis(String pattern) {
return Long.valueOf(toString(LocalDateTime.now(), pattern));
}
对指定日期进行格式化:
public static LocalDate toLocalDate(String date, String pattern) {
return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern)); }
public static String toString(LocalDate date, String pattern) {
return date.format(DateTimeFormatter.ofPattern(pattern));
}
// 计算偏移日期
public static LocalDate getOffsetLocalDate(LocalDate targetDate, ChronoUnit unit, long number) {
return targetDate.plus(number, unit);
} // 获取周一
public static LocalDate getFirstDayOfWeek(LocalDate localDate) {
return localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
} // 获取周日
public static LocalDate getLastDayOfWeek(LocalDate localDate) {
return localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
} // 获取月初
public static LocalDate getFirstDayOfMonth(LocalDate localDate) {
return localDate.with(TemporalAdjusters.firstDayOfMonth());
} // 获取月末
public static LocalDate getLastDayOfMonth(LocalDate localDate) {
return localDate.with(TemporalAdjusters.lastDayOfMonth());
} // 获取季初
public static LocalDate getFirstDayOfQuarter(LocalDate localDate) {
int month = localDate.getMonthValue();
if (month >= 1 && month <= 3) {
return LocalDate.of(localDate.getYear(), 1, 1);
} else if (month >= 4 && month <= 6) {
return LocalDate.of(localDate.getYear(), 4, 1);
} else if (month >= 7 && month <= 9) {
return LocalDate.of(localDate.getYear(), 7, 1);
} else {
return LocalDate.of(localDate.getYear(), 10, 1);
} } // 获取季末
public static LocalDate getLastDayOfQuarter(LocalDate localDate) {
int month = localDate.getMonthValue();
if (month >= 1 && month <= 3) {
return LocalDate.of(localDate.getYear(), 3, 31);
} else if (month >= 4 && month <= 6) {
return LocalDate.of(localDate.getYear(), 6, 30);
} else if (month >= 7 && month <= 9) {
return LocalDate.of(localDate.getYear(), 9, 30);
} else {
return LocalDate.of(localDate.getYear(), 12, 31);
}
} // 获取半年初
public static LocalDate getFirstDayOfHalfYear(LocalDate localDate) {
int month = localDate.getMonthValue();
if (month >= 1 && month <= 6) {
return LocalDate.of(localDate.getYear(), 1, 1);
} else {
return LocalDate.of(localDate.getYear(), 7, 1);
} } // 获取半年末
public static LocalDate getLastDayOfHalfYear(LocalDate localDate) {
int month = localDate.getMonthValue();
if (month >= 1 && month <= 6) {
return LocalDate.of(localDate.getYear(), 6, 30);
} else {
return LocalDate.of(localDate.getYear(), 12, 31);
}
} // 获取年初
public static LocalDate getFirstDayOfYear(LocalDate localDate) {
return localDate.with(TemporalAdjusters.firstDayOfYear());
} // 获取年末
public static LocalDate getLastDayOfYear(LocalDate localDate) {
return localDate.with(TemporalAdjusters.lastDayOfYear());
} // 计算两个日期相差天数
public static long getIntervalDays(LocalDate localDate1, LocalDate localDate2) {
return Math.abs(localDate1.toEpochDay() - localDate2.toEpochDay());
}
jdk8处理时间的更多相关文章
- java JDK8 学习笔记——第13章 时间与日期
第十三章 时间与日期 13.1 认识时间与日期 13.1.1 时间的度量 1.格林威治标准时间GMT 格林威治标准时间的正午是太阳抵达天空最高点之时.现在已经不作为标准时间使用. 2.世界时UT世界时 ...
- JDK8新垃圾回收机制--G1垃圾回收机制
G1全称是Garbage First Garbage Collector,使用G1的目的是简化性能优化的复杂性.例如,G1的主要输入参数是初始化和最大Java堆大小.最大GC中断时间. G1 GC由Y ...
- 基于jdk8的格式化时间方法
背景 jdk8之前,java使用Date表示时间,在做时间的格式化时,通常使用SimpleDateFormat,但是SimpleDateFormat是非线程安全的,在写代码时通常要将之定义为局部变量或 ...
- 详解 JDK8 新增的日期时间类
JDK8 新增的日期时间类 在本人之前的博文<处理时间的类 -- System类.Date类 .SimpleDateFormat类 与 Calendar类>中,讲到过表示时间的类,有三类: ...
- JDK8 日期格式化
SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...
- Day029 JDK8中新日期和时间API (二)
# JDK8中新日期和时间API (二) Instant介绍 Instant:时间线上的一个瞬时点. 这可能被用来记录应用程序中的事件时间 戳. 在处理时间和日期的时候,我们通常会想到年,月,日,时, ...
- JDK7和JDK8一些重要新特性
jdk7新特性(部分) switch支持字符串 List AutoCloseable接口实现自动关闭,在try()中 新增获取环境信息的工具方法,getJavaHomeDir,getUserHomeD ...
- Rust初步(四):在rust中处理时间
这个看起来是一个很小的问题,我们如果是在.NET里面的话,很简单地可以直接使用System.DateTime.Now获取到当前时间,还可以进行各种不同的计算或者输出.但是这样一个问题,在rust里面, ...
- android开发环境搭建(ubuntu15.04+jdk8+eclipse+android sdk)
开始学习android开发,首先对其环境对搭建比较重要.平台可以选择window/linux/mac等,这里,我选择ubuntu系统,方法比较原始,当然也可以直接用google提供的android s ...
随机推荐
- linux卸载
centos平台 yum remove xxxrpm包,rpm -e xxxtar包直接删除该文件或者make uninstall xxx
- upc组队赛12 Janitor Troubles【求最大四边形面积】
Janitor Troubles Problem Description While working a night shift at the university as a janitor, you ...
- 5.如何使用jmeter参数话
参数化:简单的来理解一下,我们录制了一个脚本,这个脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密码同时登录,或者更好的模拟多个用户来登录系统.这个时候就需要对用户名和密码进行参 ...
- Windows10下运行Android Studio3.3时关于AMD处理器不支持Intel硬件加速的解决办法
我的电脑是Thinkpad E485系列,CPU是AMD Ryzen 5 2500U,电脑预装系统是Windows10 X64家庭版,如下图所示: 下载安装了Android Studio3.3,创建了 ...
- 错误ORA-01110,在已删除数据文件情况下如何删除表空间
如果先行删除了数据文件,再删除表空间,drop tablespace 会出现如下错误: ORA-01116: error in opening database file 89 ORA-01110: ...
- zabbix cpu监控介绍
一.CPU utilization 使用Zabbix查看CPU利用率,会有下面几个值: CPU idle time:空闲的cpu时间比[简称id]CPU user time:用户态使用的cpu时间比[ ...
- mac 完全卸载vscode
原文分隔线====================== while writing go this morning, I found that the wrong code are not under ...
- HTML CSS的中英文对照
python 大蟒蛇 downloads 下载 install 安装 customize 自定义 path 环境变量:路径 optional 可选的 feature 特性特点 documentatio ...
- accordion(分类)组件
一.依赖于 Panel(面板)组件 二.class加载方式 <div id="box" class="easyui-accordion" style=&q ...
- 为IIS服务器配置SSL,并设置为默认使用https协议访问网站
要使网站支持https协议,需要SSL证书,我的服务器和域名都是在阿里云购买的,所以这里我演示阿里云获取SSL证书的方法 我先说下我的服务器环境:windows server 2012 + IIS8. ...