Java 8 LocalDateTime 初使用
LocalTime : 只包括时间
LocalDate : 只包括日期
LocalDateTime : 包括日期和时间
JDBC映射
LocalTime 对应 time
LocalDate 对应 date
LocalDateTime 对应 timestamp
以下测试代码
简直好用到爆
public class TimeTest {
@Test
public void testTime() {
LocalDateTime time = LocalDateTime.now();
System.out.println(time.toString()); //字符串表示
System.out.println(time.toLocalTime()); //获取时间(LocalTime)
System.out.println(time.toLocalDate()); //获取日期(LocalDate)
System.out.println(time.getDayOfMonth()); //获取当前时间月份的第几天
System.out.println(time.getDayOfWeek()); //获取当前周的第几天
System.out.println(time.getDayOfYear()); //获取当前时间在该年属于第几天
System.out.println(time.getHour());
System.out.println(time.getMinute());
System.out.println(time.getMonthValue());
System.out.println(time.getMonth());
System.out.println("-----------------------------------");
//格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");
System.out.println(time.format(formatter));
//构造时间
LocalDateTime startTime = LocalDateTime.of(2018, 1, 1, 20, 31, 20);
LocalDateTime endTime = LocalDateTime.of(2018, 1, 3, 20, 31, 20);
//比较时间
System.out.println(time.isAfter(startTime));
System.out.println(time.isBefore(endTime));
//时间运算,相加相减
System.out.println(time.plusYears(2)); //加2年
System.out.println(time.plusDays(2)); //加两天
System.out.println(time.minusYears(2)); //减两年
System.out.println(time.minusDays(2)); //减两天
//获取毫秒数(使用Instant)
System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
//获取秒数(使用Instant)
System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());
}
}
Java 8 LocalDateTime 初使用的更多相关文章
- BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]
错误提示: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationExce ...
- JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String
在使用Postman测试Spring Boot项目接口时,接口返回JSON parse error: Cannot deserialize value of type `java.time.Local ...
- java 8 LocalDateTime 20 例
http://www.importnew.com/15637.html 伴随lambda表达式.streams以及一系列小优化,Java 8 推出了全新的日期时间API,在教程中我们将通过一些简单的实 ...
- Apache Beam入门及Java SDK开发初体验
1 什么是Apache Beam Apache Beam是一个开源的统一的大数据编程模型,它本身并不提供执行引擎,而是支持各种平台如GCP Dataflow.Spark.Flink等.通过Apache ...
- java画图之初体验
1.实现画图程序所需的API类 JFrame JButton ActionListener 动作事件接口 ActionEvent ...
- 阿里云部署Java web项目初体验(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何在阿里云上安装JDK.Tomcat以及其配置过程.最后以一个实例来演示在 ...
- 阿里云部署Java web项目初体验(转)/linux 上配置jdk和安装tomcat
摘要:本文主要讲了如何在阿里云上安装JDK.Tomcat以及其配置过程.最后以一个实例来演示在阿里云上部署Java web项目. 一.准备工作 购买了阿里云的云解析,和云服务器ecs. 2.下载put ...
- java的多线程初体验
这里以车站售票作为模拟场景,多个票务人员同时出售某一列高铁(G250?)的车票,售票的基本保证有2点: 1.不能售出多于预定限额的车票. 2.不能售出具有相同座位的车票. 初学java,有不当的地方各 ...
- 阿里巴巴 Java 开发规约插件初体验
阿里巴巴 Java 开发手册 又一次来谈<阿里巴巴 Java 开发手册>,经过这大半年的版本迭代,这本阿里工程师们总结出来避免写出那么多 Bug 的规范,对于 Java 开发者简直就是必备 ...
随机推荐
- Laravel 加载自定义的 helpers.php 函数
Laravel 提供了很多 辅助函数,有时候我们也需要创建自己的辅助函数. 必须 把所有的『自定义辅助函数』存放于 bootstrap 文件夹中. 并在 bootstrap/app.php 文件的最顶 ...
- 在delphi XE5 里面编译kbmmw4.3
Delphi XE5 仓促的发布了,虽然开始支持Android 开发了,但是经过试用,发现那个模拟器慢到无法用, 真机可以运行,但是调试也几乎无法用.由于XE5 的主要增加的是Android 的开发支 ...
- python之基础补充
一 bit,和bytes的关系 bit:就是计算机的最小的表示单位. bytes:就是计算机的最小的储存单位. 1 字节(bytes) = 8 位(bit) 格式: print(bytes('字符' ...
- 2018.09.05 bzoj2726: [SDOI2012]任务安排(斜率优化dp+二分)
传送门 跟Ti" role="presentation" style="position: relative;">TiTi为正数的时候差不多. ...
- IntelliJ IDEA 2017版 SpringBoot的web项目补充
一.注解 @SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置. @Configuration:这是一个配置Sprin ...
- 配置 struts2 时掉进 web.xml 的坑
这个声明不好用 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quo ...
- Reactor 模式在Netty中的应用
Reactor 模式在Netty中的应用 典型的Rector模式 mainReactor 服务端创建成功后,会监听Accept操作,其中ServerSocketchannel中的PipeLine中现在 ...
- Spark RDD详解
1.RDD是什么 RDD(Resilient Distributed Dataset):是Spark的核心数据结构,指的是一个只读的.可分区的分布式数据集,这个数据集的全部或部分可以缓存在内存中,在多 ...
- hdu 1505,1506
1506题目 1505题目 1506: #include<stdio.h> #include<string.h> #include<iostream> using ...
- ACL授权实例
上一篇关于ACL的文章中:位运算实现ACL授权与认证过程的原理解析,我们学习了通过位运算实现ACL授权与认证的原理核心,今天我们一起来看授权的实例. 实现的功能很简单:打开授权界面时,加载已授权信息. ...