java8时间工具类Localdate、LocaldateTime
优点:
1.方便。
Date 只能是日期加时间的格式,而 LocalDate 、LocalTime、LocalDateTime
分别代表日期,时间,日期+时间,非常灵活。再就是后者在日期计算及格式化方面非常简单易用,而Date要繁琐很多。
2.线程安全。
传统时间类不支持多线程安全。
缺点<目前发现的坑>:
1.在比较日期相隔天数时,不要使用Period.between()方法,这个只是当月相隔天数。其实就是:a月b日 - c月d日 = (b-d)日
LocalDateTime:
他的toString()方法,不同其他类,中间有个T。可使用@JsonFormat注解,格式化需要的格式后转成json。前端展示就会显示对应的格式,当然,debug调试的时候,还是会显示T的。
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Override
public String toString() {
return date.toString() + 'T' + time.toString();
}
注意:
比较时,会需要传接口Temporal作为参数,可用它的实现类,比如localDateTime即可。
如果比较相隔的时、分、毫秒时,需要将格式转成精确到小时以后<相隔的时、分、秒都可以只格式化到小时yyyy-MM-dd HH即可不报错>
附工具类代码:
public class DateUtil { public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmmss");
public static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyyMM");
public static final DateTimeFormatter SHORT_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyMMdd");
public static final DateTimeFormatter SHORT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss");
public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); /**
* 返回当前的日期
* @return
*/
public static LocalDate getCurrentLocalDate() {
return LocalDate.now();
} /**
* 返回当前时间
* @return
*/
public static LocalTime getCurrentLocalTime() {
return LocalTime.now();
} /**
* 返回当前日期时间
* @return
*/
public static LocalDateTime getCurrentLocalDateTime() {
return LocalDateTime.now();
} /**
* yyyyMMdd
*
* @return
*/
public static String getCurrentDateStr() {
return LocalDate.now().format(DATE_FORMATTER);
} /**
* yyMMdd
*
* @return
*/
public static String getCurrentShortDateStr() {
return LocalDate.now().format(SHORT_DATE_FORMATTER);
} public static String getCurrentMonthStr() {
return LocalDate.now().format(MONTH_FORMATTER);
} /**
* yyyyMMddHHmmss
* @return
*/
public static String getCurrentDateTimeStr() {
return LocalDateTime.now().format(DATETIME_FORMATTER);
} /**
* yyMMddHHmmss
* @return
*/
public static String getCurrentShortDateTimeStr() {
return LocalDateTime.now().format(SHORT_DATETIME_FORMATTER);
} /**
* HHmmss
* @return
*/
public static String getCurrentTimeStr() {
return LocalTime.now().format(TIME_FORMATTER);
} public static String getCurrentDateStr(String pattern) {
return LocalDate.now().format(DateTimeFormatter.ofPattern(pattern));
} public static String getCurrentDateTimeStr(String pattern) {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern));
} public static String getCurrentTimeStr(String pattern) {
return LocalTime.now().format(DateTimeFormatter.ofPattern(pattern));
} public static LocalDate parseLocalDate(String dateStr, String pattern) {
return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
} public static LocalDateTime parseLocalDateTime(String dateTimeStr, String pattern) {
return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(pattern));
} public static LocalTime parseLocalTime(String timeStr, String pattern) {
return LocalTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern));
} public static String formatLocalDate(LocalDate date, String pattern) {
return date.format(DateTimeFormatter.ofPattern(pattern));
} public static String formatLocalDateTime(LocalDateTime datetime, String pattern) {
return datetime.format(DateTimeFormatter.ofPattern(pattern));
} public static String formatLocalTime(LocalTime time, String pattern) {
return time.format(DateTimeFormatter.ofPattern(pattern));
} public static LocalDate parseLocalDate(String dateStr) {
return LocalDate.parse(dateStr, DATE_FORMATTER);
} public static LocalDateTime parseLocalDateTime(String dateTimeStr) {
return LocalDateTime.parse(dateTimeStr, DATETIME_FORMATTER);
} public static LocalTime parseLocalTime(String timeStr) {
return LocalTime.parse(timeStr, TIME_FORMATTER);
} public static String formatLocalDate(LocalDate date) {
return date.format(DATE_FORMATTER);
} public static String formatLocalDateTime(LocalDateTime datetime) {
return datetime.format(DATETIME_FORMATTER);
} public static String formatLocalTime(LocalTime time) {
return time.format(TIME_FORMATTER);
} /**
* 日期相隔天数
* @param startDateInclusive
* @param endDateExclusive
* @return
*/
public static long periodDays(LocalDate startDateInclusive, LocalDate endDateExclusive) {
return endDateExclusive.toEpochDay() - startDateInclusive.toEpochDay();
} /**
* 日期相隔小时
* @param startInclusive
* @param endExclusive
* @return
*/
public static long durationHours(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toHours();
} /**
* 日期相隔分钟
* @param startInclusive
* @param endExclusive
* @return
*/
public static long durationMinutes(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toMinutes();
} /**
* 日期相隔毫秒数
* @param startInclusive
* @param endExclusive
* @return
*/
public static long durationMillis(Temporal startInclusive, Temporal endExclusive) {
return Duration.between(startInclusive, endExclusive).toMillis();
} /**
* 是否当天
* @param date
* @return
*/
public static boolean isToday(LocalDate date) {
return getCurrentLocalDate().equals(date);
} /**
* 获取此日期时间与默认时区<Asia/Shanghai>组合的时间毫秒数
* @param dateTime
* @return
*/
public static Long toEpochMilli(LocalDateTime dateTime) {
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
} /**
* 获取此日期时间与指定时区组合的时间毫秒数
* @param dateTime
* @return
*/
public static Long toSelectEpochMilli(LocalDateTime dateTime, ZoneId zoneId) {
return dateTime.atZone(zoneId).toInstant().toEpochMilli();
}
}
java8时间工具类Localdate、LocaldateTime的更多相关文章
- java8时间类API安全问题(赠送新的时间工具类哟)
LocalDateTime等新出的日期类全是final修饰的类,不能被继承,且对应的日期变量都是final修饰的,也就是不可变类.赋值一次后就不可变,不存在多线程数据问题. simpleDateFor ...
- Java8 ,LocalDate,LocalDateTime处理日期和时间工具类,
Java8 ,LocalDate,LocalDateTime处理日期和时间工具类 1.获取今天的日期 2.在Java 8 中获取年.月.日信息 3.在Java 8 中处理特定日期 4.在Java 8 ...
- 基于Java8的日期时间工具类DateTimeFormatter
原文:https://blog.csdn.net/qq_36596145/article/details/85331002 import java.time.Instant; import java. ...
- Java时间处理类LocalDate和LocalDateTime常用方法
Java时间处理类LocalDate和LocalDateTime常用方法 https://blog.csdn.net/weixin_42579074/article/details/93721757
- Java8 时间日期类操作
Java8 时间日期类操作 Java8的时间类有两个重要的特性 线程安全 不可变类,返回的都是新的对象 显然,该特性解决了原来java.util.Date类与SimpleDateFormat线程不安全 ...
- jdk1.8 时间工具类,可以满足基本操作
时间工具类 public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; public static final S ...
- 代码片段:基于 JDK 8 time包的时间工具类 TimeUtil
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “知识的工作者必须成为自己时间的首席执行官.” 前言 这次泥瓦匠带来的是一个好玩的基于 JDK ...
- java时间工具类
在项目中,很多地方需要根据时间获取相应的数据,将时间格式化,或者时间比较等相关操作.一个良好的工具类不仅可以减少代码冗余,还能促进业务处理,加快进度. /** * @author: lxw * @Da ...
- Java日期工具类,Java时间工具类,Java时间格式化
Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...
随机推荐
- Android解析WindowManager(二)Window的属性
前言 在上一篇文章我们学习了WindowManager体系,了解了Window和WindowManager之间的关系,这一篇我们接着来学习Window的属性. 1.概述 上一篇文章中我们讲过了Wind ...
- Linux内核 网络数据接收流程图
各层主要函数以及位置功能说明: 1)sock_read:初始化msghdr{}的结构类型变量msg,并且将需要接收的数据存放的地址传给msg.msg_iov->iov_base. ...
- 让图片左右缓慢移动的MoveView
让图片左右缓慢移动的MoveView 效果: 源码: MoveView.h 与 MoveView.m // // MoveView.h // AnimationView // // Created b ...
- 使用MDScratchImageView实现刮奖效果
使用MDScratchImageView实现刮奖效果 https://github.com/moqod/iOS-Scratch-n-See 最终效果: 其实这是使用了别人的东西而已:) 源码: // ...
- C# 方法与参数 常见命名空间汇总 using的使用 main方法参数
本文主要讲 C# 常见命名空间 using static 指令 && 调用静态方法 嵌套命名空间&&作用域 别名 Main() 方法 C# 常见命名空间 命名空间 作用 ...
- 沉淀,再出发:AngularJS初探
沉淀,再出发:AngularJS初探 一.前言 知识的学习需要形成一个闭环,在这个闭环之内可以自圆其说,从而触类旁通,加以理想创造,从而产生灵感.关于前端的知识,我已经写得差不多了,但是还有一个知识点 ...
- 深入浅出VisualStudio——使用NuGet来安装AjaxControlToolKit
使用NuGet可以加速配置Visual Studio 2010的开发环境. 1.创建一个空的ASP.NET web站点项目. 2.右键单击此web 站点,选择Manage NuGet Packages ...
- angularjs中factory, service和provider
在Angular里面,services作为单例对象在需要到的时候被创建,只有在应用生命周期结束的时候(关闭浏览器)才会被清除.而controllers在不需要的时候就会被销毁了(因为service的底 ...
- DTCoreText 、WKWebView 、UIWebView的比较
DTCoreText .WKWebView .UIWebView的比较 HTML->View 数据解析: WebCore:排版引擎核心,WebCore包含主要以下模块:Loader, Parse ...
- 【[CQOI2016]手机号码】
递推版的数位dp 绝对的暴力美学 我们设\(dp[l][i][j][0/1][0/1][0/1]\)表示到了第\(l\)位,这一位上选择的数是\(i\),\(l-1\)位选择的数是\(j\),第一个\ ...