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 ...
随机推荐
- iOS符号表
https://docs.bugtags.com/zh/symbols/ios/find.html 发红包的限制 1.发送频率规则 ◆ 每分钟发送红包数量不得超过1800个: ◆ 同一个商户号,每分钟 ...
- nginx地址重写
1. 域名重定向 server_name wx.he.com weixin.ha.com; if ($http_host !~* "wx\.he\.com"){ rewrite ^ ...
- MySQL-profiling的使用
分析SQL执行带来的开销是优化SQL的重要手段.在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析.该参数可以在全局和session级别来设置.对于全局级别则作用于整个MySQL ...
- Emacs 列编辑
copy from http://chandlewei.blogbus.com/logs/15583440.html 不敢独享,与大家分享.也可以在Emacs中用C-x C-h列出全部命令,查找C-x ...
- hdu1251(字典树)
统计难题(hdu1251) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Tota ...
- The golden ratio: 1.618
http://www.chinaz.com/design/2015/1109/467968_2.shtml The golden ratio: 1.618 a/b=b/(a+b) The Fibona ...
- URAL 1002 Phone Numbers(KMP+最短路orDP)
In the present world you frequently meet a lot of call numbers and they are going to be longer and l ...
- hdu4609 3-idiots
FFT 代码 #include<iostream> #include<cstring> #include<cstdio> #include<cmath> ...
- CCF真题之ISBN号码
201312-2 问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上 ...
- paper 57 :颜色直方图的代码
clear clc close all Image = imread('29.jpg');[M,N,O] = size(Image);[h,s,v] = rgb2hsv(Image); H = h; ...