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 ...
随机推荐
- spring消费RESTfull服务
使用RestTemplate做这件事非常简单 package hello; import org.slf4j.Logger; import org.slf4j.LoggerFactory; impor ...
- TOM大叔的几道Javascript题目与解答
几道JS题目 之前没有深入研究js语言,最近几年前端越来越工程化,需要扎实的js基础,看到博客园上有很多大牛分享JS学习文章,幸运看到tom大叔的blog,抽时间潜心学习了其文章,遇到到其出的几道题目 ...
- Python之配置文件模块 ConfigParser
写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...
- Oracle Flashback Technologies (总)
Oracle Flashback Technologies Oracle 9i中增加了闪回查询技术,闪回查询为数据库提供了一种简单.强大.完全无干扰从人为错误中恢复的机制.通过闪回查询,用户可以查看过 ...
- MS SQL SERVER 锁研究记录
首先创建一直数据表 ChenJi,有如下字段: ID, DanWeiID, Name, ChenJi CREATE TABLE [dbo].[ChenJi]( [ID] [int] NOT NUL ...
- Java数据库操作大全
1.提取单条记录 //import java.sql.*; Connection con=null; Statement stmt=null; ResultSet %%6=null; try { Cl ...
- Xcode 遇到 App Transport Security has blocked a cleartext HTTP 错误
今天用Xcode 创建新项目用到 URL 发送请求时,报下面的错: “App Transport Security has blocked a cleartext HTTP (http://) re ...
- Swift实战-豆瓣电台(八)播放进度与时间
视频观看地址:http://www.tudou.com/programs/view/4mEtz8S72k0/?resourceId=399000367_06_02_99 这节主要内容是NSTimer, ...
- 树形DP +01背包(HDU 1011)
题意:有n个房间,有n-1条道路连接着n个房间,每个房间都有若干个野怪和一定的能量值,有m个士兵从1房间入口进去,到达每个房间必须要留下若干士兵杀死所有的野怪,然后其他人继续走,(一个士兵可以杀死20 ...
- [转]ms sql 2000 下批量 附加/分离 数据库(sql语句)
这次公司要把MS SQL Server 2000 服务器上的数据库复制到新的服务器上面去,于是几百个数据库文件就交给我附加到新服务器上了 以前一直没接触过这方面的东西,于是果断谷歌了也百度了 找 ...