Java8新特性之一、时间日期API
package com.effective.common.base.date; import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date; /**
* 日期工具类
* @author yanweiqi
* @since 2016-5-6
*
*/
public class LocalDateUtils { private static ZoneId zone = ZoneId.systemDefault(); /**
* 字符串转Date
* @param date
* @return
* @throws Exception
*/
public static Date convertToDate(String date) throws Exception{
LocalDate localDate = null;
if(null == date){
throw new NullPointerException("date isn't null");
} else {
localDate = LocalDate.parse(date);
return convertToDate(localDate);
}
} /**
* 字符串转LocalDateTime
* @param date
* @return localDateTime
*/
public static LocalDateTime convertToLocalDateTime(String date){
LocalDateTime localDateTime = null;
if(null == date){
throw new NullPointerException("date isn't null");
} else {
localDateTime = LocalDateTime.parse(date);
return localDateTime;
}
} /**
* LocalDate转Date
* @param localDate
* @return Date
*/
public static Date convertToDate(LocalDate localDate){
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
return Date.from(instant);
} /**
* LocalDate转Date
* @param localDateTime
* @return Date
*/
public static Date convertToDate(LocalDateTime localDateTime){
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
} /**
* Date转LocalDate
* @param date
* @return localDate
*/
public static LocalDate convertToLocalDate(Date date){
Instant instant = date.toInstant();
return convertToLocalDateTime(instant).toLocalDate();
} /**
* Date转LocalTime
* @param date
* @return localDate
*/
public static LocalTime convertToLocalTime(Date date){
Instant instant = date.toInstant();
return convertToLocalDateTime(instant).toLocalTime();
} /**
* Date转LocalDatetime
* @param date
* @return localDate
*/
public static LocalDateTime convertToLocalDateTime(Date date){
Instant instant = date.toInstant();
return convertToLocalDateTime(instant);
} /**
* Instant转LocalDateTime
* @param instant
* @return
*/
public static LocalDateTime convertToLocalDateTime(Instant instant){
return LocalDateTime.ofInstant(instant, zone);
} /**
* LocalDateTime转Instant
* @param localDateTime
* @return
*/
public static Instant convertToInstant(LocalDateTime localDateTime){
return localDateTime.atZone(zone).toInstant();
} /**
* LocalDate转Instant
* @param localDate
* @return
*/
public static Instant convertToInstant(LocalDate localDate){
return localDate.atStartOfDay(zone).toInstant();
} /**
* LocalDate转LocalDateTime
* @param localDate
* @return LocalDateTime
*/
public static LocalDateTime convertToLocalDateTime(LocalDate localDate){
return localDate.atStartOfDay();
} /**
* 日周期格式化
* @param localDateTime
* @param formatStyle
* @return
*/
public static String formatter(LocalDateTime localDateTime, String formatStyle){
return DateTimeFormatter.ofPattern(formatStyle).format(localDateTime);
} /**
* 设置年
* @param sourceDate
* @param year
* @return LocalDateTime
*/
public static LocalDateTime setYear(LocalDateTime sourceDate, Integer year){
return sourceDate.withYear(year);
} /**
* 设置月
* @param sourceDate
* @param month
* @return LocalDateTime
*/
public static LocalDateTime setMonth(LocalDateTime sourceDate, Integer month){
return sourceDate.withMonth(month);
} /**
* 设置天
* @param sourceDate
* @param month
* @return LocalDateTime
*/
public static LocalDateTime setDayOfMonth(LocalDateTime sourceDate, Integer dayOfMonth){
return sourceDate.withDayOfMonth(dayOfMonth);
} /**
* 设置小时
* @param sourceDate
* @param hour
* @return
*/
public static LocalDateTime setHour(LocalDateTime sourceDate,Integer hour){
return sourceDate.withHour(hour); } /**
* 设置分钟
* @param sourceDate
* @param minute
* @return
*/
public static LocalDateTime setMinute(LocalDateTime sourceDate,Integer minute){
return sourceDate.withMinute(minute);
} /**
* 设置秒
* @param sourceDate
* @param second
* @return
*/
public static LocalDateTime setSecond(LocalDateTime sourceDate,Integer second){
return sourceDate.withSecond(second);
} /**
* 修改年月日
* @param sourceDate
* @param year
* @param month
* @param dayOfMonth
* @return
*/
public static LocalDateTime setYMD(LocalDateTime sourceDate, Integer year, Integer month, Integer dayOfMonth) {
return sourceDate.withYear(year).withMonth(month).withDayOfMonth(dayOfMonth);
} /**
* 修改时分秒
* @param sourceDate
* @param hour
* @param minute
* @param second
* @return
*/
public static LocalDateTime setHMS(LocalDateTime sourceDate,Integer hour, Integer minute, Integer second) {
return sourceDate.withHour(hour).withMinute(minute).withSecond(second);
} /**
* 计算相差的天数
* @param beginDate
* @param endDate
* @return
*/
public static int getInteverDays(LocalDate beginDate,LocalDate endDate){
Period period = Period.between(beginDate, endDate);
return period.getDays();
} /**
* 日期加减
* @param num 数量
* @param unit 单位
* @param LocalDate 原日期
* @return LocalDate 增加后的日期
*/
@SuppressWarnings("static-access")
public static LocalDate addLocalDate(long num,ChronoUnit unit,final LocalDate localDate){
LocalDate resultDate;
if(num > 0){
resultDate = localDate.now().plus(num, unit);
} else {
resultDate = localDate.now().minus(Math.abs(num), unit);
}
return resultDate;
} /**
* 日期时分秒加
* @param num 数量
* @param unit 单位
* @param localDateTime 原日期
* @return LocalDateTime 增加后的日期
*/
@SuppressWarnings("static-access")
public static LocalDateTime addLocalDateTime(long num,ChronoUnit unit,LocalDateTime localDateTime){
LocalDateTime resultDateTime;
if(num > 0){
resultDateTime = localDateTime.now().plus(num, unit);
} else {
resultDateTime = localDateTime.now().minus(Math.abs(num),unit);
}
return resultDateTime;
} /**
* 时分秒加减
* @param num 数量
* @param unit 单位
* @param localTime 原日期
* @return LocalDateTime 增加后的日期
*/
@SuppressWarnings("static-access")
public static LocalTime addLocalTime(long num,ChronoUnit unit,LocalTime localTime){
LocalTime resultTime;
if(num > 0){
resultTime = localTime.now().plus(num, unit);
} else {
resultTime = localTime.now().minus(Math.abs(num), unit);
}
return resultTime;
} public static void main(String[] args){ LocalDateTime time = LocalDateTime.now();
String rr = formatter(time, "yyyy-MM-dd HH:mm:ss");
System.out.println(rr);
LocalDateTime time2 = addLocalDateTime(-2, ChronoUnit.HOURS, time);
String yy = formatter(time2, "yyyy-MM-dd HH:mm:ss");
System.out.println(yy);
}
Java8新特性之一、时间日期API的更多相关文章
- java8新特性七-Date Time API
Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理. 在旧版的 Java 中,日期时间 API 存在诸多问题,其中有: 非线程安全 − java.ut ...
- Java8新特性之三:Stream API
Java8的两个重大改变,一个是Lambda表达式,另一个就是本节要讲的Stream API表达式.Stream 是Java8中处理集合的关键抽象概念,它可以对集合进行非常复杂的查找.过滤.筛选等操作 ...
- Java8新特性时间日期库DateTime API及示例
Java8新特性的功能已经更新了不少篇幅了,今天重点讲解时间日期库中DateTime相关处理.同样的,如果你现在依旧在项目中使用传统Date.Calendar和SimpleDateFormat等API ...
- Java8 新特性 Data Time API
Java8新的日期类型 在Java8以前,Date日期API对我们非常的不友好,它无法表示日期,只能以毫秒的精试来表示时间,并且可以修改,他的线程还不是安全的.所以Java8中引入了全新的日期和时间A ...
- 2020你还不会Java8新特性?
Java8(1)新特性介绍及Lambda表达式 前言: 跟大娃一块看,把原来的电脑拿出来放中间看视频用 --- 以后会有的课程 难度 深入Java 8 难度1 并发与netty 难度3 JVM 难度4 ...
- 【Java8新特性】关于Java8中的日期时间API,你需要掌握这些!!
写在前面 Java8之前的日期和时间API,存在一些问题,比如:线程安全的问题,跨年的问题等等.这些问题都在Hava8中的日期和时间API中得到了解决,而且Java8中的日期和时间API更加强大.立志 ...
- java8新特性——时间日期API
传统的时间 API 存在线程安全的问题,在多线程开发中必须要上锁,所以 java8 现在为我们提供了一套全新的时间日期 API ,今天进来学习一下java8 的时间日期 API. 一.使用 Local ...
- Java8新特性(三)——Optional类、接口方法与新时间日期API
一.Optional容器类 这是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象. 查看结构图可以看到有如下常用方法: of(T)—— ...
- Java8新特性——新一套时间API的使用
JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了.而Calendar并不比Date好多少.它们面临的问题是: 可变性: ...
- Java8 新特性(三) - 日期时间对象以及一些其他特性
日期时间对象 关于日期时间的操作可以分为两种: 转换:与字符串的互相转换,与时间戳的互相转换 计算:计算两个时间点之间的间隔.时间点与时间段的计算(计算下周N.下个月D日.去年M月D日等等) Java ...
随机推荐
- HTML-移动端如何使用css让百分比布局的弹窗水平和垂直方向上居中
pc端让一个弹窗水平和垂直方向居中,在知道弹窗宽高的情况下很好计算,只需要用如下css即可: #date{ width: 300px; height: 300px; position: absolut ...
- Git 使用的配置 常用命令
老文一篇 搬过来 1. git的部分配置 # 全局提交用户名与邮箱 git config --global user.name "simon" git config --globa ...
- linux namespace note
--------------------------------- from http://oldwiki.linux-vserver.org/Namespaces //开源不只是代码,还有思想 Na ...
- 使用Mac的AppleScritp调用控制台的方式
使用Mac的AppleScritp调用 控制台的方法 tell application "Terminal" activate do script "cd Documen ...
- HTML调用servlet(一)
1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...
- poj 1176 Party Lamps
http://poj.org/problem?id=1176 Party Lamps Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- 算法训练 区间k大数查询
http://lx.lanqiao.org/problem.page?gpid=T11 算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个 ...
- [转]怎样解决Myeclipse内存溢出?
在用myeclipes10 开发 遇到了 内存溢出问题,百度了很久,这篇比较完善. 总结起来三个方面去检查 1)myeclipes的配置:myeclipes 10 的安装路径下 的myeclipse. ...
- paper 71 :图像清晰化
图像清晰度是衡量图像品质优劣的标准之一,清晰的图像能给人以赏心悦目的视觉享受.长期以来,图像扫描设备和图像处理软件的开发生产厂商都很重视图像清晰度处理功能的开发,图像处理人员也在日常的实践中不断摸索出 ...
- CDC spyglass
SoC中会有着几百的clock domains,millions的async data crossing. Glitch等cdc问题是netlist level simulation的主要目的. CD ...