java JDK8 时间处理
时间格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
String localDateTimeStr = formatter.format(localDateTime);
System.out.println(localDateTimeStr);
String str = "2008年08月23日 23:59:59";
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
LocalDateTime localDateTime2 = LocalDateTime.parse(str,formatter2);
System.out.println(localDateTime2);
LocalDate:年月日
LocalDate today = LocalDate.now(); // -> 2019-01-31
System.out.println("取当前日期: "+today);
LocalDate crischristmas = LocalDate.of(2018, 12, 25); // -> 2018-12-25
System.out.println("根据年月日取日期,12月就是12: "+crischristmas);
LocalDate endOfFeb = LocalDate.parse("2018-12-25"); // 严格按照ISO yyyy-MM-dd验证,02写成2都不行,当然也有一个重载方法允许自己定义格式
System.out.println("根据字符串取: "+endOfFeb);
LocalDate oneToday = today.plus(1, ChronoUnit.WEEKS); // ->2019-02-07
System.out.println("如何获取1周后的日期: "+oneToday);
LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
System.out.println("一年前的日期: "+previousYear);
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2019-01-01
System.out.println("取本月第1天: "+firstDayOfThisMonth);
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2019-01-02
System.out.println("取本月第2天: "+secondDayOfThisMonth);
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2019-01-31
System.out.println("取本月最后一天,再也不用计算是28,29,30还是31: "+lastDayOfThisMonth);
LocalDate firstDay = lastDayOfThisMonth.plusDays(1); // 变成了2019-02-01
System.out.println("取下一天: "+firstDay);
LocalDate nextSuday = today.with(DayOfWeek.SUNDAY);
System.out.println("下一个周日: "+nextSuday);
LocalDate nextSaturday = today.with(DayOfWeek.SATURDAY);
System.out.println("下一个周六: "+nextSaturday);
LocalDate firstMonday = LocalDate.parse("2019-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2019-01-07
System.out.println("取2019年1月第一个周一 "+firstMonday);
//对比日期是否为同一天(生日对比)
LocalDate birthday = LocalDate.of(1990, 10, 12);
MonthDay birthdayMd = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
MonthDay today = MonthDay.from(LocalDate.of(2019, 10, 12));
System.out.println(today.equals(birthdayMd));
LocalTime:时分秒毫秒
LocalTime now = LocalTime.now(); // 11:09:09.240
System.out.println("时间包含毫秒: "+now);
LocalTime now1 = LocalTime.now().withNano(0); // 11:09:09
System.out.println("时间包含不毫秒"+now1);
LocalTime zero = LocalTime.of(1, 2, 1); // 00:00:00
System.out.println("构造时间: "+zero);
LocalTime mid = LocalTime.parse("12:00:00"); // 12:00:00
System.out.println("字符串转时间: "+mid);
LocalTime twoHour = LocalTime.now().plusHours(2);
System.out.println("添加2小时: "+twoHour);
LocalDateTime:年月日时分秒
LocalDateTime ldt1 = LocalDateTime.of(2017, Month.JANUARY, 4, 17, 23, 52);
System.out.println("创建日期时间方法一: "+ldt1);
LocalDate localDate = LocalDate.of(2017, Month.JANUARY, 4);
LocalTime localTime = LocalTime.of(17, 23, 52);
LocalDateTime ldt2 = localDate.atTime(localTime);
System.out.println("创建日期时间方法二: "+ldt2);
Instant:纳秒时间戳
LocalDate date = ldt1.toLocalDate();
System.out.println("日期时间获取日期"+date);
LocalTime time = ldt1.toLocalTime();
System.out.println("日期时间获取时间"+time);
Instant instantNow =Instant.now();
System.out.println("时间戳(精确到纳秒)nanos表示纳秒部分 : "+instantNow.toEpochMilli()+instantNow.getNano());
Instant instant = Instant.ofEpochSecond(120, 100000);
System.out.println("时间戳(精确到纳秒) : "+instant);
Duration:两时间间隔
LocalDateTime from = LocalDateTime.of(2019, Month.JANUARY, 5, 10, 7, 0); // 2019-01-05 10:07:00
LocalDateTime to = LocalDateTime.of(2019, Month.FEBRUARY, 5, 10, 7, 0); // 2019-02-05 10:07:00
Duration duration = Duration.between(from, to); // 表示从 2019-01-05 10:07:00 到 2019-02-05 10:07:00 这段时间
long days = duration.toDays();
System.out.println("这段时间的总天数"+days);
long hours = duration.toHours();
System.out.println("这段时间的小时数"+hours);
long minutes = duration.toMinutes();
System.out.println("这段时间的分钟数"+minutes);
long seconds = duration.getSeconds();
System.out.println("这段时间的秒数"+seconds);
long milliSeconds = duration.toMillis();
System.out.println("这段时间的毫秒数"+milliSeconds);
long nanoSeconds = duration.toNanos();
System.out.println("这段时间的纳秒数"+nanoSeconds);
Duration:处理两个时间之间的差值
Duration duration1 = Duration.of(5, ChronoUnit.DAYS); // 5天
System.out.println("这段时间的分钟数"+duration1.toMinutes());
Duration duration2 = Duration.of(1000, ChronoUnit.MILLIS); // 1000毫秒
System.out.println("这段时间的分钟数"+duration2.toMinutes());
ZonedDateTime:引入地区
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);
Instant instant = Instant.now();
ZoneId zoneId1 = ZoneId.of("GMT");
ZonedDateTime zonedDateTime2 = ZonedDateTime.ofInstant(instant,zoneId1);
System.out.println(zonedDateTime2);
java JDK8 时间处理的更多相关文章
- 全面解析Java日期时间API
时区 GMT(Greenwich Mean Time):格林尼治时间,格林尼治标准时间的正午是指当太阳横穿格林尼治子午线时(也就是在格林尼治上空最高点时)的时间. UTC(Universal Time ...
- Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析
目录 0.前言 1.TemporalAccessor源码 2.Temporal源码 3.TemporalAdjuster源码 4.ChronoLocalDate源码 5.LocalDate源码 6.总 ...
- Java 进行时间处理
Java 进行时间处理 一.Calendar (1).Calender介绍 Calendar的中文翻译是日历,实际上,在历史上有着许多种计时的方法.所以为了计时的统一,必需指定一个日历的选择.那现在最 ...
- Java实现时间动态显示方法汇总
这篇文章主要介绍了Java实现时间动态显示方法汇总,很实用的功能,需要的朋友可以参考下 本文所述实例可以实现Java在界面上动态的显示时间.具体实现方法汇总如下: 1.方法一 用TimerTask: ...
- Java 对时间和日期的相关处理
1. 获取当前系统时间和日期并格式化输出 import java.util.Date; import java.text.SimpleDateFormat; public class NowStrin ...
- java JDK8 学习笔记——助教学习博客汇总
java JDK8 学习笔记——助教学习博客汇总 1-6章 (by肖昱) Java学习笔记第一章——Java平台概论 Java学习笔记第二章——从JDK到IDEJava学习笔记第三章——基础语法Jav ...
- java JDK8 学习笔记——第16章 整合数据库
第十六章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API中的接口会有数据库厂商操作,称为JDBC驱动程 ...
- java中时间的获取(二)
java中时间的获取2 /** * 获取数据库操作记录时间 */ public static String getOpreateDbTime() { Calendar c = Calendar.get ...
- Java 日期时间
Java 日期时间 标签 : Java基础 Date java.util.Date对象表示一个精确到毫秒的瞬间; 但由于Date从JDK1.0起就开始存在了,历史悠久,而且功能强大(既包含日期,也包含 ...
随机推荐
- [hdu6989]Didn't I Say to Make My Abilities Average in the Next Life?!
显然问题即求$\frac{\sum_{x\le l\le r\le y}(\max_{l\le i\le r}a_{i}+\min_{l\le i\le r}a_{i})}{(y-x+2)(y-x+1 ...
- springboot项目中常遇到的问题-初学者最容易犯的错
1.在spring中有两个main方法 2.在idea中少提代码类了,或者某类中代码依赖关系没解决掉
- HTML四种定位-绝对定位
绝对定位 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset=&q ...
- CF1288
A 考虑\(x + 1 = \sqrt{d}\)时在有理域上有最优界. 那我在整数域上附近取三个点取min就行了. // code by fhq_treap #include<bits/stdc ...
- 【Plink】Error: Multiple instances of '_' in sample ID.?
目录 前言 原因 解决方法 方法一:修改样本名 方法二:修改--id-delim 方法三:加入--double_id或--const-fid参数 前言 将vcf转化为plink格式时,命令如下: pl ...
- PAML 选择压力的计算
简介 PAML(Phylogenetic Analysis by Maximum Likelihood)是伦敦大学的杨子恒(Yang Ziheng)教 授开发的一套基于最大似然估计来对蛋白质和核酸序列 ...
- R shinydashboard——3.外观
目录 1.皮肤 2.注销面板 3.CSS 4. 标题延长 5.侧边栏宽度 6.图标 7.状态和颜色 1.皮肤 shinydashboard有很多颜色主题和外观的设置.默认为蓝色,可指定黑丝.紫色.绿色 ...
- 36-Same Tree
Same Tree My Submissions QuestionEditorial Solution Total Accepted: 126116 Total Submissions: 291884 ...
- Spring Security 基于URL的权限判断
1. FilterSecurityInterceptor 源码阅读 org.springframework.security.web.access.intercept.FilterSecurityI ...
- angular中路由跳转并传值四种方式
一.路由传值 步骤1 路由传递参数 注意 一定是要传递 索引值 let key = index 这种情况是在浏览器中可以显示对应的参数 这种的是问号 localhost:8080/news?id=2& ...