jdk1.8新日期时间类(DateTime、LocalDateTime)demo代码
//获取当前时间
LocalDateTime d0 = LocalDateTime.now();
System.out.println(DataConvertUtil.localDateTimeToStr(d0, "yyyy-MM-dd HH:mm:ss")); //输入年月日时分秒实例化新的时间对象
LocalDateTime d1 = LocalDateTime.of(2017, 5, 15, 20, 32, 12);
System.out.println(DataConvertUtil.localDateTimeToStr(d1, "yyyy-MM-dd HH:mm:ss")); //对一个时间对象加5分钟(同理也可加/减,年/月/日/时/分/秒),加是plus,减是minus
LocalDateTime d2 = d1.plusMinutes(5);
System.out.println(DataConvertUtil.localDateTimeToStr(d2, "yyyy-MM-dd HH:mm:ss")); //对一个时间对象单独设置年月日时分秒
//把小时设为5点
LocalDateTime d21 = d2.withHour(5);
//把小时设为5点,0分,0秒
d21 = d2.withHour(6).withMinute(0).withSecond(0);
System.out.println(DataConvertUtil.localDateTimeToStr(d21, "yyyy-MM-dd HH:mm:ss")); //获取日(同理也可获取年月日时分秒)
int day = d2.getDayOfMonth();
System.out.println(day); //判断两个时间的先后,可以用isAfter或isBefore,如下例就是判断d2是否晚于d1
boolean b1 = d2.isAfter(d1);
System.out.println(b1); //两个时间相减,获取时间差(例子为d2-d1,并获取时间差(单位:分钟))
//类似Duration的还有Period,Period专用于计算年月日
Duration duration = Duration.between(d1, d2);
long m1 = duration.toMinutes();
System.out.println(m1); //Date和LocalDateTime互转
Date dx1 = DataConvertUtil.localDateTimeToDate(d1);
LocalDateTime d3 = DataConvertUtil.dateToLocalDateTime(dx1);
System.out.println(DataConvertUtil.localDateTimeToStr(d3, "yyyy-MM-dd HH:mm:ss")); //Date和LocalDate互转
LocalDate d31 = LocalDate.now();
dx1 = DataConvertUtil.localDateToDate(d31);
d31 = DataConvertUtil.dateToLocalDate(dx1); //LocalDateTime按指定格式输出string
//还有可指定输出格式版本localDateTimeToStr(LocalDateTime value, String format)
String s1 = DataConvertUtil.localDateTimeToStr(d1, "yyyy-MM-dd HH:mm:ss");
System.out.println("s1=" + s1); //string按指定格式转为LocalDateTime
//还有可指定格式版本strToLocalDateTime(String value, String format)
LocalDateTime d4 = DataConvertUtil.strToLocalDateTime(s1, "yyyy-MM-dd HH:mm:ss");
System.out.println(DataConvertUtil.localDateTimeToStr(d4, "yyyy-MM-dd HH:mm:ss"));
//注意!!!!!
//由于LocalDateTime类型的限制,字符串转LocalDateTime时字符串值必须完整包含年月日时分秒,而实际情况经常会只有年月日
//对于这种情况,可以用下例的DataConvertUtil.strToLocalDate转为LocalDate类型,然后再把LocalDate转为LocalDateTime
String s2 = DataConvertUtil.localDateTimeToStr(d1, "yyyy-MM-dd");
//字符串转LocalDate
LocalDate d5 = DataConvertUtil.strToLocalDate(s2, "yyyy-MM-dd");
System.out.println(DataConvertUtil.localDateToStr(d5));
//LocalDate转LocalDateTime
LocalDateTime d6 = LocalDateTime.of(d5, LocalTime.of(0, 0));
System.out.println(DataConvertUtil.localDateTimeToStr(d6, "yyyy-MM-dd HH:mm:ss")); //以下补充DataConvertUtil类的相关函数
/**
* LocalDateTime转Date
*
* @param localDateTime
* @return
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
if (localDateTime == null) return null; return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
} /**
* Date转LocalDateTime
*
* @param date
* @return
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
if (date == null) return null; return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
} /**
* LocalDate转Date
*
* @param localDate
* @return
*/
public static Date localDateToDate(LocalDate localDate) {
if (localDate == null) return null; return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
} /**
* Date转LocalDate
*
* @param date
* @return
*/
public static LocalDate dateToLocalDate(Date date) {
if (date == null) return null; return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
} /**
* 日期时间(LocalDateTime)按默认格式转字符串
*
* @param value
* @return
*/
public static String localDateTimeToStr(LocalDateTime value) {
return localDateTimeToStr(value, "yyyy-MM-dd");
} /**
* 日期时间(LocalDateTime)按指定格式转字符串
*
* @param value
* @param format
* @return
*/
public static String localDateTimeToStr(LocalDateTime value, String format) {
String dateString;
if (value == null) {
dateString = "";
} else {
DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(format);
dateString = value.format(formatDate);
} return dateString;
} /**
* 字符串按默认格式转日期时间(LocalDateTime)
*
* @param value
* @return
*/
public static LocalDateTime strToLocalDateTime(String value) {
return strToLocalDateTime(value, "yyyy-MM-dd HH:mm:ss");
} /**
* 字符串按指定格式转日期时间(LocalDateTime)
*
* @param value
* @param format
* @return
*/
public static LocalDateTime strToLocalDateTime(String value, String format) {
if (value != null && value.trim().length() > 0) {
DateTimeFormatter formatDate = DateTimeFormatter.ofPattern(format); try {
return LocalDateTime.parse(value, formatDate);
} catch (Exception e) {
return null;
}
} return null;
}
jdk1.8新日期时间类(DateTime、LocalDateTime)demo代码的更多相关文章
- Java 之 JDK1.8之前日期时间类
一.JDK1.8之前日期时间类 二. java.lang.System类 System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1 ...
- jdk8环境下sprngboot/springmvc中JSR310新日期/时间类LocalDateTime显示效果带T
如图所示: 日期时间类中带了一个T,以上这种格式LocalDateTime格式化的时候默认日期时间格式:ISO.DATE_TIME(按笔者目前的知识理解是ISO8601规范中的日期时间格式化) 想要把 ...
- springboot Thymeleaf中格式化jsr310新日期时间类(LocalDateTime,LocalDate)--thymeleaf格式化LocalDateTime,LocalDate等JDK8新时间类
依赖maven包 <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>th ...
- 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结
1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018 年月日时分秒 CST代表北 ...
- 详解 JDK8 新增的日期时间类
JDK8 新增的日期时间类 在本人之前的博文<处理时间的类 -- System类.Date类 .SimpleDateFormat类 与 Calendar类>中,讲到过表示时间的类,有三类: ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- Java 8 新日期时间 API
Java 8 新日期时间 API 1. LocalDate.LocalTime.LocalDateTime LocalDate.LocalTime.LocalDateTime 实例是不可变的对象,分别 ...
- 第十一章 容器类&新日期时间
11.1.Optional 容器类 11.1.1.概述 Optional 类是一个容器类,代表一个值存在或不存在, 原来用 null 表示一个值不存在,现在 Optional类 可以更好的表达这个概念 ...
- 日期时间类:Date,Calendar,计算类:Math
日期时间类 计算机如何表示时间? 时间戳(timestamp):距离特定时间的时间间隔. 计算机时间戳是指距离历元(1970-01-01 00:00:00:000)的时间间隔(ms). 计算机中时间2 ...
随机推荐
- iconfont的图文混排
最近在使用iconfont排版,但是发现完全没法混到textarea中. 希望借助 contentEditable 解决这个问题
- 哈希表---线性探测再散列(hash)
//哈希表---线性探测再散列 #include <iostream> #include <string> #include <stdio.h> #include ...
- 计算机学院大学生程序设计竞赛(2015’12)The Magic Tower
The Magic Tower Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- dedecms文章内页获取缩略图的调用标签
点评:文章内容页缩略图的调用,图片集内容页缩略图的调用很容易混淆,内页想调用缩略图用[filed:picname/]来实现是错误的 文章内容页缩略图的调用,图片集内容页缩略图的调用,相信大家都想找这个 ...
- dedecms获取顶级栏目名称、二级栏目名称实现方法
织梦DEDECMS文章.栏目页获取当前页面顶级栏目名称的方法 在用织梦做一些项目时,时常会碰到需要在当前页面调用顶级栏目名称的时候,织梦默认{dede:field name='typename' /} ...
- Swift语言学习(三)基础操作符
操作符是用于检测.更改或者组合值的特殊符号或短语.例如,加法操作符 (+) 将两个数字加到一起 (如 let i = 1 + 2).更复杂的例子包括逻辑与操作符 && (如 if en ...
- Silverlight实用窍门系列:2.Silverlight动态加载外部XML指定地址的WebService---(动态加载外部XML文件中指定的WebService地址)【附带实例源码】
接上节所讲的,Silverlight可以加载外部的XML文件里面的内容,那么我们可不可以在外部XML里面配置一个WebService地址,并且以此加载这个地址来动态加载WebService呢?这样子就 ...
- 一步一步学Silverlight 2系列(24):与浏览器交互相关辅助方法
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- fuse的编译安装(Centos7-minimal)
打算寒假在家跟着THU的一个分布式系统的课程:http://thu-cmu.cs.tsinghua.edu.cn/curriculum/dscourse/schedule.htm 第0个lab就是要在 ...
- C#方法参数总结
C#中方法的参数的四种类型 C#中方法的参数有四种类型: 1. 值参数类型 (不加任何修饰符,是默认的类型) 2. 引用型参数 (以ref 修饰符声明) 3. ...