package com.jansh.comm.util;

 import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter; /**
* 日期时间工具类
*
* @author nie
*
*/
public class DateUtil {
/**
* yyyyMMddHHmmss
*/
public static final DateTimeFormatter formatter_DateTimestamp = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
/**
* yyyy-MM-dd HH:mm:ss
*/
public static final DateTimeFormatter formatter_DateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /**
* 获取当前日期
*
* @return
*/
public static LocalDate getLocalDate() {
return LocalDate.now();
} /**
* 获取当前时间
*
* @return
*/
public static LocalTime getLocalTime() {
return LocalTime.now();
} /**
* 获取当前日期时间
*
* @return
*/
public static LocalDateTime getLocalDateTime() {
return LocalDateTime.now();
} /**
* 获取当前的微秒数
*
* @return
*/
public static long getClockMillis() {
Clock clock = Clock.systemDefaultZone();
return clock.millis();
} /**
* 返回当前时间yyyyMMddHHmmss
*
* @return
*/
public static String getDateTimestamp() {
return getLocalDateTime().format(formatter_DateTimestamp);
} /**
* 返回当前时间yyyy-MM-dd
*
* @return
*/
public static String getDate() {
return getLocalDate().format(DateTimeFormatter.ISO_LOCAL_DATE);
} /**
* 返回当前系统时间 yyyy-MM-dd HH:mm:ss
*
* @return 返回当前系统时间
*/
public static String getDateTime() {
return getLocalDateTime().format(formatter_DateTime);
} /**
* 获取当月第一天 yyyy-MM-dd
*
* @return
*/
public static String getFirstDayOfMonth() {
return getLocalDate().withDayOfMonth(1).format(DateTimeFormatter.ISO_LOCAL_DATE);
} /**
* 获取本月最后一天 yyyy-MM-dd
*
* @return
*/
public static String getLastDayOfMonth() {
LocalDate localDate = getLocalDate();
return localDate.withDayOfMonth(localDate.lengthOfMonth()).format(DateTimeFormatter.ISO_LOCAL_DATE);
} /**
* 将yyyyMMddHHmmss转为 yyyy-MM-dd HH:mm:ss
*
* @param time
* @return
*/
public static String formatDateTimestamp(String dateTimestamp) {
return LocalDateTime.parse(dateTimestamp, formatter_DateTimestamp).format(formatter_DateTime);
} /**
* 将yyyy-MM-dd HH:mm:ss转为 yyyyMMddHHmmss
*/
public static String formatDateTime(String dateTime) {
return parseLocalDateTime(dateTime).format(formatter_DateTimestamp);
} /**
* 将yyyy-MM-dd HH:mm:ss转为 LocalDateTime
*/
public static LocalDateTime parseLocalDateTime(String dateTime) {
return LocalDateTime.parse(dateTime, formatter_DateTime);
} /**
* 将yyyyMMddHHmmss转为 LocalDateTime
*/
public static LocalDateTime parseLocalDateTimestamp(String dateTimestamp) {
return LocalDateTime.parse(dateTimestamp, formatter_DateTimestamp);
} /**
* yyyy-MM-dd字符串转LocalDate
*
* @param dateString
* @return
*/
public static LocalDate parseLocalDate(String dateString) {
return LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE);
} /**
* yyyy-MM-dd 增加日期
*
* @param date
* @param day
* @return
*/
public static String plusDays(String date, int days) {
LocalDate localDate = parseLocalDate(date);
return localDate.plusDays(days).format(DateTimeFormatter.ISO_LOCAL_DATE);
} /**
* 计算两个日期之间相差的天数
*
* @param startDate
* 较小的时间 yyyy-MM-dd
* @param endDate
* 较大的时间 yyyy-MM-dd
* @return 相差天数
*/
public static int dateCompareTo(String startDate, String endDate) {
LocalDate startLocalDate = LocalDate.parse(startDate, DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate endLocalDate = LocalDate.parse(endDate, DateTimeFormatter.ISO_LOCAL_DATE);
Period period = Period.between(startLocalDate, endLocalDate);
return period.getDays();
} public static void main(String[] args) {
System.out.println(getDateTimestamp());
System.out.println(getDateTime());
System.out.println(getLocalDate().withDayOfMonth(1));
System.out.println(getLocalDate().withDayOfMonth(getLocalDate().lengthOfMonth()));
System.out.println(plusDays("2016-08-31", 1));
System.out.println(dateCompareTo("2016-08-21", "2016-08-30"));
System.out.println(getClockMillis());
System.out.println(LocalDateTime.parse("2016-08-31 11:39:14", formatter_DateTime)
.format(DateTimeFormatter.ISO_LOCAL_DATE)); } }

例子

 包概述
java.time 包是在JDK8新引入的,提供了用于日期、时间、实例和周期的主要API。
java.time包定义的类表示了日期-时间概念的规则,包括instants, durations, dates, times, time-zones and periods。这些都是基于ISO日历系统,它又是遵循 Gregorian规则的。 所有类都是不可变的、线程安全的。 一些类的介绍
LocalDateTime:存储了日期和时间,如:2013-10-15T14:43:14.539。
LocalDate:存储了日期,如:2013-10-15。
LocalTime:存储了时间,如:14:43:14.539。
上面的类可以由下面的类组合来:
Year:表示年份。
Month:表示月份。
YearMonth:表示年月。
MonthDay:表示月日。
DayOfWeek:存储星期的一天。 类之间转换的示例:
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime :" + localDateTime); LocalDate localDate = LocalDate.now();
System.out.println("localDate :" + localDate); LocalTime localtime = LocalTime.now();
System.out.println("localtime :" + localtime); // 获取当前年份
Year year = Year.now();
System.out.println("year :" + year);
// 从Year获取LocalDate
LocalDate localDate1 = year.atDay(59);
System.out.println("localDate1 :" + localDate1);
// 把LocalTime关联到一个LocalDate得到一个LocalDateTime
LocalDateTime localDateTime1 = localtime.atDate(localDate1);
System.out.println("localDateTime1 :" + localDateTime1); // 用指定的年获取一个Year
Year year1 = Year.of(2012);
System.out.println("year1 :" + year1); // 从Year获取YearMonth
YearMonth yearMonth = year1.atMonth(2);
System.out.println("yearMonth :" + yearMonth);
// YearMonth指定日得到 LocalDate
LocalDate localDate2 = yearMonth.atDay(29);
System.out.println("localDate2 :" + localDate2);
// 判断是否是闰年
System.out.println("isLeapYear :" + localDate2.isLeapYear()); //自动处理闰年的2月日期
//创建一个 MonthDay
MonthDay monthDay = MonthDay.of(2, 29);
LocalDate leapYear = monthDay.atYear(2012);
System.out.println("leapYear :" + leapYear); //同一个 MonthDay 关联到另一个年份上
LocalDate nonLeapYear = monthDay.atYear(2011);
System.out.println("nonLeapYear :" + nonLeapYear); 上面代码的输出结果为:
localDateTime :2013-10-15T15:11:57.489
localDate :2013-10-15
localtime :15:11:57.489
year :2013
localDate1 :2013-02-28
localDateTime1 :2013-02-28T15:11:57.489
year1 :2012
yearMonth :2012-02
localDate2 :2012-02-29
isLeapYear :true
leapYear :2012-02-29
nonLeapYear :2011-02-28 格式化与时间计算
DateTimeFormatter:在日期对象与字符串之间进行转换。
ChronoUnit:计算出两个时间点之间的时间距离,可按多种时间单位计算。
TemporalAdjuster:各种日期计算功能。
续前面的代码:
DayOfWeek dayOfWeek = DayOfWeek.of(1);
System.out.println("dayOfWeek :" + dayOfWeek); //计算两个日期之间的天数,还可以按其他时间单位计算两个时间点之间的间隔。
long between = ChronoUnit.DAYS.between(localDate, leapYear);
System.out.println("between :" + between); // 线程安全的格式化类,不用每次都new个SimpleDateFormat
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("uuuu MM dd"); // 把日期时间转换为字符串标识
System.out.println("date formatter :" + dateTimeFormatter.format(nonLeapYear)); // 解析字符串形式的日期时间
TemporalAccessor temporalAccessor = dateTimeFormatter.parse("2013 01 15");
System.out.println("temporalAccessor :" + LocalDate.from(temporalAccessor)); Instant instant = Instant.now(); // 时间戳
System.out.println("instant :" + instant); //计算某月的第一天的日期
LocalDate with = nonLeapYear.with(TemporalAdjuster.firstDayOfMonth());
System.out.println("with :" + with); // 计算某月的第一个星期一的日期
TemporalAdjuster temporalAdjuster = TemporalAdjuster.firstInMonth(DayOfWeek.MONDAY);
LocalDate with1 = localDate.with(temporalAdjuster);
System.out.println("with1 :" + with1); // 计算localDate的下一个星期一的日期
LocalDate with2 = localDate.with(TemporalAdjuster.next(DayOfWeek.MONDAY));
System.out.println("with2 :" + with2);
输出:
dayOfWeek :MONDAY
between :-594
date formatter :2011 02 28
temporalAccessor :2013-01-15
instant :2013-10-15T07:55:30.964Z
with :2011-02-01
with1 :2013-10-07
with2 :2013-10-21 java.util.Date到新库类的转换
转换可通过下面的方法进行。
Date.toInstant()
Date.from(Instant)
Calendar.toInstant()
方法概览
该包的API提供了大量相关的方法,这些方法一般有一致的方法前缀:
of:静态工厂方法。
parse:静态工厂方法,关注于解析。
get:获取某些东西的值。
is:检查某些东西的是否是true。
with:不可变的setter等价物。
plus:加一些量到某个对象。
minus:从某个对象减去一些量。
to:转换到另一个类型。
at:把这个对象与另一个对象组合起来,例如:date.atTime(time)。

jdk8 之 java.time包AND DateUtils的更多相关文章

  1. Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等

    从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...

  2. Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析

    目录 0.前言 1.TemporalAccessor源码 2.Temporal源码 3.TemporalAdjuster源码 4.ChronoLocalDate源码 5.LocalDate源码 6.总 ...

  3. java基础-包

    浏览以下内容前,请点击并阅读 声明 为了使类型更容易查找和使用,避免命名冲突,以及可视范围的控制,程序员一般将相关的一些类型组合到一个包中.组合的类型包括类,接口,枚举和注释,枚举是一种特殊的类,而注 ...

  4. java中包命名常见规则

    做java的都知道java的包.类.接口.枚举.方法.常量.变量等等模型都有一套约定的命名规则! 学习每一种语言都应该学习对应语法和命名规则,以保持一个良好的编码风格.一来显示自己的专业.二来方便阅读 ...

  5. java jar包收集

    activation~与javaMail有关的jar包,使用javaMail时应与mail.jar (mail.jar和activation.jar)一起加入到lib中去,具体负责mail的数据源和类 ...

  6. java util包概述

    util是utiliy的缩写,意为多用途的,工具性质的包这个包中主要存放了:集合类(如ArrayList,HashMap等),随机数产生类,属性文件读取类,定时器类等类.这些类极大方便了Java编程, ...

  7. java.io包详细解说

    转自:http://hzxdark.iteye.com/blog/40133 hzxdark的博客 我不知道各位是师弟师妹们学java时是怎样的,就我的刚学java时的感觉,java.io包是最让我感 ...

  8. java.io包中的字节流—— FilterInputStream和FilterOutputStream

    接着上篇文章,本篇继续说java.io包中的字节流.按照前篇文章所说,java.io包中的字节流中的类关系有用到GoF<设计模式>中的装饰者模式,而这正体现在FilterInputStre ...

  9. java jar包解析:打包文件,引入文件

    java jar包解析:打包文件,引入文件 cmd下: jar命令:package包打包 javac命令:普通类文件打包 Hello.java: package org.lxh.demo; publi ...

随机推荐

  1. If only it could be all the same like we first me

    为什么 你当时对我好 Why? You nice to me at that time. 又为什么 现在变得冷淡了 Why? Now you give a cold shoulder to me. 我 ...

  2. 使用ReTrofit做缓存(结合上拉加载和下拉刷新)

    1. noCache 不使用缓存,全部走网络 2. noStore 不使用缓存,也不存储缓存 3. onlyIfCached 只使用缓存 4. maxAge 设置最大失效时间,失效则不使用 需要服务器 ...

  3. DedeCMS织梦文章内容图片绝对路径修改方法

    这几天在网站改版,想把网站做大,想做频道页二级域名,于是在做网站的过程中发现一个问题,dedecms开设二级域名后,在二级域名的文章页无法显示图片,查看源代码后发现问题,由于dedecms文章页中的图 ...

  4. P图

    照片名称:调出照片柔和的蓝黄色-简单方法,1.打开原图素材,按Ctrl + J把背景图层复制一层,点通道面板,选择蓝色通道,图像 > 应用图像,图层为背景,混合为正片叠底,不透明度50%,反相打 ...

  5. hdu_5110_Alexandra and COS(DP+分块思想)

    题目连接:hdu_5110_Alexandra and COS 题意: 给你一个图,X代表宝藏,然后有一个船,它的声纳的频率为D,定船到宝藏的距离为Dis=max(abs(x1-x2),abs(y1- ...

  6. Linux链接VPN进行转发

    1.安装client sudo apt-get install pptp-linux 2.连接vpn server sudo pptpsetup --create pptpd --server x.x ...

  7. iptables原理详解以及功能说明

    原文:http://www.svipc.com/thread-450-1-1.html 前言   iptables其实就是Linux下的一个开源的信息过滤程序,包括地址转换和信息重定向等功能的,他由四 ...

  8. Fragment在Activity中的应用 (转载)

    原文链接 http://www.cnblogs.com/nanxin/archive/2013/01/24/2875341.html 在本小节中介绍在Activity中创建Fragment. 官网有很 ...

  9. PAT (Advanced Level) 1074. Reversing Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  10. 百度网盘API的操作--PCS 百度个人云存储 上传 ,下载文件

    来自http://blog.csdn.net/u014492257/article/details/39856403 另外需要所有API使用方法的请访问本人上传的资源(需要3个下载分的)链接: htt ...