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代表北 ...
随机推荐
- zabbix监控MySQL状态值获取不到值原因分析
在server端测试键值 [root@zbx-server etc]# zabbix_get -s MySQL-glibc -k "buffer_pool_wait_free" 如 ...
- ffmpeg常用命令-学习
文章标题:FFmpeg常用命令合集 文章地址:https://blog.csdn.net/lemon_tree12138/article/details/99719520
- 怎么保证redis集群的高并发和高可用的?
redis不支持高并发的瓶颈在哪里? 单机.单机版的redis支持上万到几万的QPS不等. 主要根据你的业务操作的复杂性,redis提供了很多复杂的操作,lua脚本. 2.如果redis要支撑超过10 ...
- 行为型模式(八) 职责链模式(Chain of Responsibility)
一.动机(Motivate) 在软件构建过程中,一个请求可能被多个对象处理,但是每个请求在运行时只能有一个接受者,如果显示指定,将必不可少地带来请求发送者与接受者的紧耦合.如何使请求的发送者不需要指定 ...
- sublime——开启自动保存
前言 懒 步骤 失去焦点自动保存 "save_on_focus_lost": true 首选项-->设置-->Ctrl+F搜索‘save’,找到“save_on_foc ...
- Mac OpenSSL 生成支付宝 2048位密钥
安装OpenSSL: brew install openssl 然后: OpenSSL> genrsa -out rsa_private_key.pem 2048 #生成私钥 OpenSSL&g ...
- ES的底层原理-倒排索引的概念
Elasticsearch底层使用的使用的lucene lucene使用的是倒排索引的方式来进行加快检索速度 倒排索引的原理 doc_1 The quick brown fox jumped ...
- tensorflow API _ 1 (control_flow_ops.cond)
该函数用来控制程序执行流,相当于if-else了import tensorflow as tffrom tensorflow.python.ops import control_flow_ops a ...
- ip address control获取ip字符串
1.环境:vs2010 & 默认项目字符集(貌似是unicode) 2.首先为ip address control添加control类型变量m_ipaddressedit, BYTE ips[ ...
- 用1 x 2的多米诺骨牌填满M x N矩形的方案数(完美覆盖)
题意 用 $1 \times 2$ 的多米诺骨牌填满 $M \times N$ 的矩形有多少种方案,$M \leq 5,N < 2^{31}$,输出答案模 $p$. 分析 当 $M=3$时,假设 ...