JDK8日期时间操作小汇总
统一使用java.time.*包下的类
1、获取当前的日期、时间、日期加时间
LocalDate todayDate = LocalDate.now(); //今天的日期 LocalTime now = LocalTime.now(); //此刻的时间--精确到毫秒 LocalDateTime now = LocalDateTime.now(); //此刻时间--年-月-日-时-分-秒-毫秒
2、获取单独年日月时分秒
LocalDateTime now = LocalDateTime.now();
System.out.println("年:"+now.getYear());
System.out.println("月:"+now.getMonthValue());
System.out.println("日:"+now.getDayOfMonth());
System.out.println("时:"+now.getHour());
System.out.println("分:"+now.getMinute());
System.out.println("秒:"+now.getSecond());
System.out.println("该日期是该年的第"+now.getDayOfYear()+"天");
3、将特定的时间转成LocalDate、LocalTime、LocalDateTime 都有很多的重载的方法
LocalDate of1 = LocalDate.of(2019, 1, 16);
System.out.println(of1);
LocalTime of2 = LocalTime.of(9, 25, 36);
System.out.println(of2);
LocalDateTime of3 = LocalDateTime.of(of1, of2);
System.out.println(of3);
LocalDateTime of4 = LocalDateTime.of(2019, 1, 14, 9, 26, 25);
System.out.println(of4);
4、判断时间是否相等(日期、时间、日期加时间)时间的比较精确到纳秒,可以先将时间进行格式化,然后比较,可以控制精度问题
// 日期
LocalDate date1 = LocalDate.of(2019, 1, 17);
LocalDate date2 = LocalDate.now();
if (date1.equals(date2)) {
System.out.println("日期相等");
} else {
System.out.println("日期不相等");
}
// 时间
LocalTime date1 = LocalTime.of(9, 46);
LocalTime date2 = LocalTime.now();
if (date1.equals(date2)) {
System.out.println("时间相等");
} else {
System.out.println("时间不相等");
}
// 时间加日期
LocalDateTime date1 = LocalDateTime.of(2019,1,17,9,47);
LocalTime date2 = LocalTime.now();
if (date1.equals(date2)) {
System.out.println("时间加日期相等");
} else {
System.out.println("时间加日期不相等");
}
5、检查周几这种周期性的事件
DayOfWeek of = DayOfWeek.THURSDAY;
DayOfWeek from = DayOfWeek.from(LocalDate.now());
if (from.equals(of)) {
System.out.println("今天是周四");
} else {
System.out.println("今天不是周四");
}
DayOfWeek from1 = DayOfWeek.from(LocalDate.of(2019, 1, 10));
if (from1.equals(of)) {
System.out.println("该日期是周四");
} else {
System.out.println("该日期不是周四");
}
6、指定时间之后或之前XXX的时间
LocalDateTime now = LocalDateTime.now();
System.out.println("当前时间"+now);
System.out.println("当前时间之前2天:"+now.minusDays(2));
System.out.println("当前时间之后2天:"+now.plusDays(2));
System.out.println("当前时间之前2周:"+now.minusWeeks(2));
System.out.println("当前时间之后2周:"+now.plusWeeks(2));
System.out.println("当前时间之前2月:"+now.minusMonths(2));
System.out.println("当前时间之后2月:"+now.plusMonths(2));
System.out.println("当前时间之前2年:"+now.minusYears(2));
System.out.println("当前时间之后2年:"+now.plusYears(2));
System.out.println("当前时间之前2小时:"+now.minusHours(2));
System.out.println("当前时间之后2小时:"+now.plusHours(2));
System.out.println("当前时间之前2分钟:"+now.minusMinutes(2));
System.out.println("当前时间之后2分钟:"+now.plusMinutes(2));
System.out.println("当前时间之前2秒:"+now.minusSeconds(2));
System.out.println("当前时间之后2秒:"+now.plusSeconds(2));
7、比较时间的先后
LocalDateTime now = LocalDateTime.now();
System.out.println("当前时间:"+now);
LocalDateTime appointTime = LocalDateTime.of(2019, 1, 17, 9, 32, 12);
System.out.println("指定时间:"+appointTime);
System.out.println(now.isAfter(appointTime));
System.out.println(now.isBefore(appointTime));
8、格式化时间格式及按照一定格式解析
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
//格式化
String format = now.format(formatter);
String format2 = now.format(formatter2);
String format3 = now.format(formatter3);
System.out.println("格式化1:" + format);
System.out.println("格式化2:" + format2);
System.out.println("格式化3:" + format3);
//解析
LocalDateTime localDateTime = LocalDateTime.parse(format, formatter);
LocalDate localDate = LocalDate.parse(format2, formatter2);
LocalTime localTime = LocalTime.parse(format3, formatter3);
System.out.println("解析1:" + localDateTime);
System.out.println("解析2:" + localDate);
System.out.println("解析3:" + localTime);
9、根据指定的毫秒数获取LocalDateTime对象
Long nowMilli = new Date().getTime();
LocalDateTime localDateTime = Instant.ofEpochMilli(nowMilli).atZone(ZoneId.of("+8")).toLocalDateTime();
System.out.println(localDateTime);
注:+8 表示东八区,即常说的北京时间
10、通过LocalDateTime对象获取毫秒数
LocalDateTime now = LocalDateTime.now();
long l = now.atZone(ZoneId.of("+8")).toInstant().toEpochMilli();
System.out.println(l);
注:+8 表示东八区,即常说的北京时间
11、通过LocalDateTime对象获取秒数
LocalDateTime now = LocalDateTime.now();
long second = now.toEpochSecond(ZoneOffset.of("+8"));
System.out.println(second);
注:+8 表示东八区,即常说的北京时间
12、判断指定日期是否是闰年(只LocalDate)
LocalDate now = LocalDate.now();
if (now.isLeapYear()) {
System.out.println("now是闰年");
} else {
System.out.println("now不是闰年");
}
13、使用默认格式解析时间(LocalDateTime对象可解析的格式yyyy-MM-ddTHH:mm:ss)--不是空格了,是T
LocalDateTime dateTime = LocalDateTime.parse("2019-01-14T12:04:12");
int monthValue = dateTime.getMonthValue();
int minute = dateTime.getMinute();
System.out.println(monthValue+":"+minute);
LocalTime time = LocalTime.parse("19:14:25");
int hour = time.getHour();
System.out.println(hour);
LocalDate date = LocalDate.parse("2019-01-12");
int year = date.getYear();
System.out.println(year);
14、总结
jdk8中提供三个类(LocalDate,LocalTime,LocalDateTime),它们分别对年月日、时分秒和年月日时分秒进行单独的处理,使用时可以根据自己的实际情况进行选择,它们的API类似。
Date与LocalDateTime、LocalDate、LocalTime互转参考: https://www.cnblogs.com/exmyth/p/6425878.html
JDK8日期时间操作小汇总的更多相关文章
- paip.日期时间操作以及时间戳uapi php java python 总结
paip.日期时间操作以及时间戳uapi php java python 总结 ///uapi Date 函数 | Day 函数 | Hour 函数 | Minute 函数 | Month 函数 | ...
- Firebird日期时间操作
最近在使用Firebird数据做 一项目,使用FireBird边用边学.(以下转贴) 查询2007年度以后的,12月份以上的数据记录,datetime为timestamp字段 select * fro ...
- Python实用日期时间处理方法汇总
这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...
- Python基础 | 日期时间操作
目录 获取时间 时间映射 格式转换 字符串转日期 日期转字符串 unixtime 时间计算 时间偏移 时间差 "日期时间数据"作为三大基础数据类型之一,在数据分析中会经常遇到. 本 ...
- Java日期时间操作基础——包含JDK1.8时间操作新特性
JDK1.7日期时间操作 示例小结 public class DateTest { public static final String FORMAT_DATE = "yyyy-MM-dd& ...
- Mysql——日期函数,时间操作(汇总)
英文文档连接:https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html 中文文档连接:https://www.docs4 ...
- MySQL tips (日期时间操作/concat 等)
1. Query结尾要加一个分号: 2. 数据库和表 SHOW DATABASES; USE YOUR_DB; SHOW TABLES; SHOW COLUMNS FROM study或者D ...
- mysql日期时间操作
select curdate(); --获取当前日期 select last_day(curdate()); --获取当月最后一天. select DATE_ADD(curdate(),interva ...
- php 学习笔记之日期时间操作一箩筐
格式化日期时间 date : 格式化日期时间 场景 将当前日期时间或者特定日期时间格式化输出为特定格式的字符串,常用于人性化展示信息. 说明 返回给定时间戳格式化后所产生的日期时间字符串,如果没有给出 ...
随机推荐
- spring AOP 编程--AspectJ注解方式 (4)
1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...
- mybatis 查询sql时foreach使用法
找到俩个例子摘下来 sql查询用户in传list参数 <select id="getEmpsByConditionForeach" resultType="com. ...
- 2019-7-3-如何通过命令行-msbuild-编译项目
title author date CreateTime categories 如何通过命令行 msbuild 编译项目 lindexi 2019-07-03 19:12:19 +0800 2019- ...
- UBOOT的的 C 语言代码部分
调用一系列的初始化函数 1. 指定初始函数表: init_fnc_t *init_sequence[] = { cpu_init, /* cpu 的基本设置 */ ...
- JQuery和JavaScript常用方法的一些区别
jquery 就对javascript的一个扩展,封装,就是让javascript更好用,更简单,为了说明区别,下面与大家分享下JavaScript 与JQuery 常用方法比较 jquery 就 ...
- Vim操作 -- 多段复位粘贴
Vim可以多段复制.粘贴.即,内容X复制到寄存器“1”,内容Y复制到寄存器“2”:粘贴时可以选择从“1”还是“2”粘贴. (1) Vim有13个粘贴板,分别是0.1.2.....9.a.“.+:用:r ...
- nicescroll 使用与配置
使用// 1. 简单模式,设置html元素滚动 $(document).ready(function() { $("html").niceScroll(); }); // 2. 返 ...
- 19-10-26-Night-D
压表的技巧. ZJ一下: T1,考试不会哈夫曼树只压到$1MB$最后截掉了一部分. T2,直接暴力丢上去.$\Theta(N+\sqrt{N}\log N)$ T3,现场码出左右旋然后就不会了$QAQ ...
- codeforces 1195D2-Submarine in the Rybinsk Sea
传送门:QAQQAQ 题意:自己看 思路:就是一个类似于数位DP的东西... 统计a[i]数位分解的数在每一位出现的个数,即分两种讨论: 1.位数小于当前j,则j会出现在q+i,而且计算顺序互换会计算 ...
- centos7下Elasticsearch5.2.2和head 插件环境搭建
ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便.支持通过HTTP使用JSON进行数据索引 ...