Java8中的日期时间类
测试类:
import java.time.*;
import java.time.format.DateTimeFormatter; public class App
{
public static void main( String[] args )
{ LocalDateTime time = LocalDateTime.now();
System.out.println(time.toString()); //输出日期时间:2019-05-04T18:27:55.240
System.out.println(time.toLocalDate()); //输出日期:2019-05-04
System.out.println(time.toLocalTime()); //输出时间:18:27:55.240
System.out.println(time.getDayOfMonth()); //输出当前日期月份的第几天:4
System.out.println(time.getDayOfWeek()); //输出档期日期周几:SATURDAY
System.out.println(time.getDayOfYear()); //当前日期在该年属于第几天:124
System.out.println(time.getHour()); //输出:18
System.out.println(time.getMinute()); //输出:27
System.out.println(time.getSecond()); //输出:55
System.out.println(time.getMonthValue()); //输出:5
System.out.println(time.getMonth()); //输出:MAY
System.out.println("=============================================="); //格式化输出:
DateTimeFormatter format = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
System.out.println(time.format(format)); //输出:2019-05-04 18:27:55 //构造时间
LocalDateTime startTime = LocalDateTime.of(2019,05,04,17,59);
System.out.println(startTime.format(format)); //输出:2019-05-04 17:59:00
LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(0,0,0));
System.out.println(endTime.format(format)); //输出:2019-05-04 00:00:00 //时间比较
System.out.println(time.isAfter(startTime)); //输出:true
System.out.println(time.isBefore(endTime)); //输出:false //时间运算
System.out.println(time.plusDays(-1).format(format)); //输出:2019-05-03 18:27:55
System.out.println(time.plusDays(1).format(format)); //输出:2019-05-05 18:27:55
System.out.println(time.plusMonths(-1).format(format)); //输出:2019-04-04 18:27:55
System.out.println(time.plusMonths(1).format(format)); //输出:2019-06-04 18:27:55
System.out.println(time.getHour()); //输出:18
System.out.println(time.withHour(1).format(format)); //输出:2019-05-04 01:27:55 //获取毫秒数时间戳
long milliSec = time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println(milliSec); //输出:1556965675240
//获取秒数时间戳
long sec = time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
System.out.println(sec); //输出:1556965675
//时间戳转换为时间
LocalDateTime time2 =LocalDateTime.ofInstant(Instant.ofEpochMilli(milliSec),ZoneId.systemDefault());
System.out.println(time2.format(format)); //输出:2019-05-04 18:27:55
LocalDateTime time3 = LocalDateTime.ofInstant(Instant.ofEpochSecond(sec),ZoneId.systemDefault());
System.out.println(time3.format(format)); //输出:2019-05-04 18:27:55 }
}
输出结果:
2019-05-04T18:27:55.240
2019-05-04
18:27:55.240
4
SATURDAY
124
18
27
55
5
MAY
==============================================
2019-05-04 18:27:55
2019-05-04 17:59:00
2019-05-04 00:00:00
true
false
2019-05-03 18:27:55
2019-05-05 18:27:55
2019-04-04 18:27:55
2019-06-04 18:27:55
18
2019-05-04 01:27:55
1556965675240
1556965675
2019-05-04 18:27:55
2019-05-04 18:27:55
Java8中的日期时间类的更多相关文章
- 都9012了,Java8中的日期时间API你还没有掌握?
一,Java8日期时间API产生的前因后果 1.1 为什么要重新定义一套日期时间API 操作不方便:java中最初的Date不能直接对指定字段进行加减操作也不支持国际化,后来新增了Calendar,但 ...
- 【Java8新特性】关于Java8中的日期时间API,你需要掌握这些!!
写在前面 Java8之前的日期和时间API,存在一些问题,比如:线程安全的问题,跨年的问题等等.这些问题都在Hava8中的日期和时间API中得到了解决,而且Java8中的日期和时间API更加强大.立志 ...
- 对Java8新的日期时间类的学习(二)
示例11 在Java中如何判断某个日期是在另一个日期的前面还是后面 这也是实际项目中常见的一个任务.你怎么判断某个日期是在另一个日期的前面还是后面,或者正好相等呢?在Java 8中,LocalDate ...
- 对Java8新的日期时间类的学习(一)
引用自Java译站http://it.deepinmind.com/java/2015/03/17/20-examples-of-date-and-time-api-from-Java8.html 除 ...
- jdk8环境下sprngboot/springmvc中JSR310新日期/时间类LocalDateTime显示效果带T
如图所示: 日期时间类中带了一个T,以上这种格式LocalDateTime格式化的时候默认日期时间格式:ISO.DATE_TIME(按笔者目前的知识理解是ISO8601规范中的日期时间格式化) 想要把 ...
- Java基础——常用类之日期时间类
如果有机会,请尝试Java8中全新的时间日期API!(参见Java8新特性随笔) 如果还是使用Java7及之前的版本,那么你可以尝试一些工具类(参考使用工具类相关的Hutool-DateUtil) 如 ...
- Android中关于日期时间与时区的使用总结
在开发Android的过程中,出现过几次由于日期时间导致的问题,而且主要是由于时区的原因导致,所以一直想总结一下,形成一个良好的开发规范. 一.Unix时间戳 Unix时间戳(Unix tim ...
- 在mysql数据库中关于日期时间字段的处理
在mysql数据库中关于日期时间字段的处理 在开发中,日期时间字段一般有如下几种设计 假设要获取2013-08-15日到2013-08-16日之间的记录 1. 直接使用日期时间类字段 相关sql语句如 ...
- 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结
1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018 年月日时分秒 CST代表北 ...
随机推荐
- nginx编译安装之-./configure 参数详解
参考官方文档 http://nginx.org/en/docs/configure.html --with开头的,默认是禁用的(没启动的,想使用的话需要在编译的时候加上) --without开头的,默 ...
- 宁波市第二届CTF部分WP之msc1,msc2
msc1签到 这题没啥好说的,修改一下图片宽高,flag到手 msc2 一开始用十六进制编辑器打开,分析文件,暂时无果,卡了一小时(线下没网) 后面,看着这部分文件头眼熟,猜测是GIF头, 于是,在硬 ...
- TODO的作用及如何使用
https://blog.csdn.net/jerry11112/article/details/82966142 文章标题:[C#]TODO的作用 可以方便后续找到要做的功能点.
- k8s安装之prometheus.yaml
这个系列的东东满多的.要另开系列说明. 这里为了内容连续完成,先贴一个吧,其它configmap,exporter就不展示. 为了保持统一,将prometheus也放到二级目录了. - '--web. ...
- Route all trafic for specific ip over specific network interface
15 I have a linux server that needs to get some routing. I'm fairly new at this and i don't find any ...
- P1052 过河[DP]
题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...
- CheckList 如何梳理可减少上线的验证时间(总结篇)
对CheckList的执行发起的思考? (1)功能越来越多,CheckList越补充越多,执行CheckList时间越来越长,如何减少上线的验证时间?(2)减少上线验证的时间外,如何保证质量?上线后少 ...
- Mysql存储引擎中InnoDB与Myisam的区别
1. 事务处理innodb 支持事务功能,myisam 不支持.Myisam 的执行速度更快,性能更好. 2. select ,update ,insert ,delete 操作MyISAM:如果执行 ...
- nginx配置文件服务器 linux
一,安装nginx服务器 点击打开链接 二,配置nginx服务器 ##测试配置文件 sudo /usr/sbin/nginx -t ##修改配置文件 ##1. 在nginx文件 ...
- 如何识别和解决SQL Server中的热闩锁(PAGELATCH_EX)
描述 在SQL Server中,内部闩锁体系结构可在SQL操作期间保护内存.通过页面上的读写操作,可以确保内存结构的一致性.从根本上讲,它具有两个类:缓冲区锁存器和非缓冲区锁存器,它们在SQL Eng ...