网上搜索了半天都没有找到Java8的LocalDateTime的工具类,只好自己写了一个,常用功能基本都有。还在用Date的Java同道该换换了

个人项目地址:https://github.com/KingBoyWorld/common.git,Common模块中有很多实用的工具包,都是优化过的。

工具类

package com.kingboy.common.utils.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date; /*
* @author kingboy
* @Date 2017/7/22 下午2:12
* @Description LocalDateTimeUtils is used to Java8中的时间类
*/
public class LocalDateTimeUtils { //获取当前时间的LocalDateTime对象
//LocalDateTime.now(); //根据年月日构建LocalDateTime
//LocalDateTime.of(); //比较日期先后
//LocalDateTime.now().isBefore(),
//LocalDateTime.now().isAfter(), //Date转换为LocalDateTime
public static LocalDateTime convertDateToLDT(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
} //LocalDateTime转换为Date
public static Date convertLDTToDate(LocalDateTime time) {
return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
} //获取指定日期的毫秒
public static Long getMilliByTime(LocalDateTime time) {
return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
} //获取指定日期的秒
public static Long getSecondsByTime(LocalDateTime time) {
return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
} //获取指定时间的指定格式
public static String formatTime(LocalDateTime time,String pattern) {
return time.format(DateTimeFormatter.ofPattern(pattern));
} //获取当前时间的指定格式
public static String formatNow(String pattern) {
return formatTime(LocalDateTime.now(), pattern);
} //日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
return time.plus(number, field);
} //日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){
return time.minus(number,field);
} /**
* 获取两个日期的差 field参数为ChronoUnit.*
* @param startTime
* @param endTime
* @param field 单位(年月日时分秒)
* @return
*/
public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
if (field == ChronoUnit.YEARS) return period.getYears();
if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
return field.between(startTime, endTime);
} //获取一天的开始时间,2017,7,22 00:00
public static LocalDateTime getDayStart(LocalDateTime time) {
return time.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
} //获取一天的结束时间,2017,7,22 23:59:59.999999999
public static LocalDateTime getDayEnd(LocalDateTime time) {
return time.withHour(23)
.withMinute(59)
.withSecond(59)
.withNano(999999999);
} }

测试类

package com.kingboy.common.localdatetimeutils;

import com.kingboy.common.utils.date.LocalDateTimeUtils;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit; import static com.kingboy.common.utils.date.LocalDateTimeUtils.getDayEnd;
import static com.kingboy.common.utils.date.LocalDateTimeUtils.getDayStart; /**
* @author kingboy
* @Date 2017/7/22 下午7:16
* @Description LocaDateTimeUtilsTest is used to 测试LocalDateTime工具
*/
public class LocaDateTimeUtilsTest { @Test
public void format_test() {
System.out.println(LocalDateTimeUtils.formatNow("yyyy年MM月dd日 HH:mm:ss"));
} @Test
public void betweenTwoTime_test() {
LocalDateTime start = LocalDateTime.of(1993, 10, 13, 11, 11);
LocalDateTime end = LocalDateTime.of(1994, 11, 13, 13, 13);
System.out.println("年:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.YEARS));
System.out.println("月:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MONTHS));
System.out.println("日:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.DAYS));
System.out.println("半日:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.HALF_DAYS));
System.out.println("小时:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.HOURS));
System.out.println("分钟:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MINUTES));
System.out.println("秒:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.SECONDS));
System.out.println("毫秒:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MILLIS));
//=============================================================================================
/*
年:1
月:13
日:396
半日:792
小时:9506
分钟:570362
秒:34221720
毫秒:34221720000
*/
} @Test
public void plus_test() {
//增加二十分钟
System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.plus(LocalDateTime.now(),
20,
ChronoUnit.MINUTES), "yyyy年MM月dd日 HH:mm"));
//增加两年
System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.plus(LocalDateTime.now(),
2,
ChronoUnit.YEARS), "yyyy年MM月dd日 HH:mm"));
//=============================================================================================
/*
2017年07月22日 22:53
2019年07月22日 22:33
*/
} @Test
public void dayStart_test() {
System.out.println(getDayStart(LocalDateTime.now()));
System.out.println(getDayEnd(LocalDateTime.now()));
//=============================================================================================
/*
2017-07-22T00:00
2017-07-22T23:59:59.999999999
*/
} }

原文地址:https://blog.csdn.net/kingboyworld/article/details/75808108

Java8中的LocalDateTime工具类的更多相关文章

  1. JAVA中封装JSONUtils工具类及使用

    在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. ...

  2. commons-lang3-3.2.jar中的常用工具类的使用

    这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...

  3. Android 中替代 sharedpreferences 工具类的实现

    Android 中替代 sharedpreferences 工具类的实现 背景 想必大家一定用过 sharedpreferences 吧!就我个人而言,特别讨厌每次 put 完数据还要 commit. ...

  4. 实用篇:说说我在JavaScript项目中使用的工具类

    在JavaScript的开发中,我们都会写一些工具类来帮我们简化一些业务操作的逻辑,一下就貼几个我在项目开发过程中常用的工具类.表达能力有限,各位看官还是看源码吧. 一.日期处理工具类. /** * ...

  5. java编程中的断言工具类(org.springframework.util.Assert)

    转自:https://blog.csdn.net/gokeiryou263/article/details/19612471 断言工具类:Assert类, java.lang.Object ---&g ...

  6. Java 中的并发工具类

    Java 中的并发工具类 CountDownLatch public class JoinCountDownLatchTest { public static void main(String[] a ...

  7. DateHandler日期处理工具(JSP中使用后台工具类)

    1.DateHandler.java package Utils.dateHandler; import java.text.ParseException; import java.text.Simp ...

  8. 基于Java8的日期时间工具类DateTimeFormatter

    原文:https://blog.csdn.net/qq_36596145/article/details/85331002 import java.time.Instant; import java. ...

  9. Java8集合框架——集合工具类Arrays内部方法浅析

    java.util.Arrays 备注:本文只对 Java8 中的 java.util.Arrays 中提供的基本功能进行大致介绍,并没有对其具体的实现原理进行深入的探讨和分析.详情可自己深入观摩源码 ...

随机推荐

  1. LUOGU P2280 [HNOI2003]激光炸弹

    传送门 解题思路 二维前缀和. 代码 #include<iostream> #include<cstdio> #include<cstring> using nam ...

  2. Office2010 破解(Microsoft Toolkit 2.4.3.exe)

    这两天破解刚安装好的office2010,总是报错 刚才重新下载了Microsoft Toolkit 2.4.3.exe工具后,破解成功,操作如下: 选择office按钮后,如下操作,

  3. [Day1] 初识Nginx

    一. Nginx的诞生 Nginx是一个高效的web及反向代理服务器,它的第一版发布于2012年,晚于如今占据最大市场份额的Apache.那么Nginx的诞生肩负了哪些使命呢?或者说它于Apache的 ...

  4. 014-unittest扩展

    unittest扩展 1. unittest框架里面---verbosity设置这里的verbosity是一个选项,表示测试结果的信息复杂度,有三个值: 0 (静默模式): 你只能获得总的测试用例数和 ...

  5. finger 工具:用来查询用户信息,侧重用户家目录、登录SHELL等

    finger 工具侧重于用户信息的查询:查询的内容包括用户名(也被称为登录名Login),家目录,用户真实的名字(Name)... ... 办公地址.办公电话:也包括登录终端.写状态.空闭时间等: 我 ...

  6. javascript基础:dom

    Dom: * 概念:Document Object  Model    文档对象模型 * 将标记语言文档的各个组成部分,封装成对象,可以使用这些对象,对标记语言文档进行CRUD的动态操作 *    D ...

  7. Broken Keyboard UVA 11988 数组实现链表

    这个构造十分巧妙,,,又学到一招,有点类似数组实现的邻接表 #include <iostream> #include <string.h> #include <cstdi ...

  8. LA3695 Distant Galaxy

    Distant Galaxy https://vjudge.net/problem/UVALive-3695 You are observing a distant galaxy using a te ...

  9. Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表

    给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 class Solution { publ ...

  10. ubuntn右上角小键盘消失及fictx切换输入法快捷键

    Ubuntu任务栏右上角的小键盘消失,打开系统设置-文本输入-左下角将当前输入法显示在任务栏. 切换输入法快捷键,打开系统设置 > 文件输入 >切换到下一个源(上一个源)的快捷键设置一个不 ...