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的更多相关文章

  1. java8新特性七-Date Time API

    Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理. 在旧版的 Java 中,日期时间 API 存在诸多问题,其中有: 非线程安全 − java.ut ...

  2. Java8新特性之三:Stream API

    Java8的两个重大改变,一个是Lambda表达式,另一个就是本节要讲的Stream API表达式.Stream 是Java8中处理集合的关键抽象概念,它可以对集合进行非常复杂的查找.过滤.筛选等操作 ...

  3. Java8新特性时间日期库DateTime API及示例

    Java8新特性的功能已经更新了不少篇幅了,今天重点讲解时间日期库中DateTime相关处理.同样的,如果你现在依旧在项目中使用传统Date.Calendar和SimpleDateFormat等API ...

  4. Java8 新特性 Data Time API

    Java8新的日期类型 在Java8以前,Date日期API对我们非常的不友好,它无法表示日期,只能以毫秒的精试来表示时间,并且可以修改,他的线程还不是安全的.所以Java8中引入了全新的日期和时间A ...

  5. 2020你还不会Java8新特性?

    Java8(1)新特性介绍及Lambda表达式 前言: 跟大娃一块看,把原来的电脑拿出来放中间看视频用 --- 以后会有的课程 难度 深入Java 8 难度1 并发与netty 难度3 JVM 难度4 ...

  6. 【Java8新特性】关于Java8中的日期时间API,你需要掌握这些!!

    写在前面 Java8之前的日期和时间API,存在一些问题,比如:线程安全的问题,跨年的问题等等.这些问题都在Hava8中的日期和时间API中得到了解决,而且Java8中的日期和时间API更加强大.立志 ...

  7. java8新特性——时间日期API

    传统的时间 API 存在线程安全的问题,在多线程开发中必须要上锁,所以 java8 现在为我们提供了一套全新的时间日期 API ,今天进来学习一下java8 的时间日期 API. 一.使用 Local ...

  8. Java8新特性(三)——Optional类、接口方法与新时间日期API

    一.Optional容器类 这是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象. 查看结构图可以看到有如下常用方法: of(T)—— ...

  9. Java8新特性——新一套时间API的使用

    JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了.而Calendar并不比Date好多少.它们面临的问题是: 可变性: ...

  10. Java8 新特性(三) - 日期时间对象以及一些其他特性

    日期时间对象 关于日期时间的操作可以分为两种: 转换:与字符串的互相转换,与时间戳的互相转换 计算:计算两个时间点之间的间隔.时间点与时间段的计算(计算下周N.下个月D日.去年M月D日等等) Java ...

随机推荐

  1. Nginx实现多重IF判断的办法

    在YII框架中如果访问的图片不存在,会记录大量的错误,于是我想了个办法,凡是访问不存在的图片,直接返回404,不经过YII框架 location / {  set $if_img N;  if ($r ...

  2. maven 一好用的仓库镜像

    <mirror> <id>ibiblio.org</id> <name>ibiblio Mirror of http://repo1.maven.org ...

  3. c# 排序算法总结

    /// <summary> /// 冒泡排序法1 /// </summary> /// <param name="list"></para ...

  4. 类似 select 选择框效果及美化

    网上有各种各样的关于 select 选择框的美化,找了很多,并没有好的样式效果.所以就找了一个利用 ul li 做的类似 select 选择框的效果,不废话了,先上图,效果如下: 对于上图的箭头效果, ...

  5. 学习CSS3BUTTON(二)

    今天,继续学习其源代码: button { margin-left: 0; margin-right: 0; *padding: 5px 5px 3px 5px; } /*margin-left:设定 ...

  6. js拖拽换位置,使用数组方法

    之前一直需要一个拖拽效果,网上找了些感觉不是不好用,就是写的有些地方让人不太满意,下面贡献一个自己写的.亲测可用,拖动后可互换位置!(带有注释) 方法/步骤 CSS代码部分 <style> ...

  7. [转]30分钟学会反向Ajax

    原文链接:http://www.cnblogs.com/learnhow/p/5708364.html 场景1:当有新邮件的时候,网页自动弹出提示信息而无需用户手动的刷新收件箱. 场景2:当用户的手机 ...

  8. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  9. nginx指定配制文件

    nginx启动: 未指定配制文件: ./nginx 指定配制文件: /usr/local/nginx/sbin/nginx -c /home/deploy/nginx-wz/conf/nginx.co ...

  10. Oracle Savepoint

    1.目的: Use the SAVEPOINT statement to identify a point in a transaction to which you can later roll b ...