【JAVA8】新时间API操作大全
文章目录
- (1)LocalDate 操作日期值(仅包含年 月 日)信息
- (2)LocalDateTime 操作时间值(详细的时间 包含年月日时分秒)
- (3)Duration 求时间差(基于时间值LocalDateTime)
- (4)ChronoUnit 求时间差(基于时间值LocalDateTime)
- (5)Period 求时间差(基于日期值 LocalDate)
(1)LocalDate 操作日期值(仅包含年 月 日)信息
public class LocalDateDemo {public static void main(String[] args) {// 获取现在时间 格式:yyyy-MM-dd ex:2021-08-28LocalDate now = LocalDate.now();System.out.println(now);// 格式化 ex:yyyyMMdd 20210828String formatResult = now.format(DateTimeFormatter.BASIC_ISO_DATE);System.out.println(formatResult);// 对时间进行偏移 可以偏移 周 月 年 天等// plusXX 对时间做右偏移 (加时间)LocalDate afterDate = now.plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1);// 我这里是对当前时间+1年+1月+1周+1天 2022-10-06System.out.println(afterDate);// minusXX 对时间做左偏移 (减时间)// 我这里是对当前时间 -1年-1月 2020-07-28LocalDate preDate = now.minusYears(1).minusMonths(1);System.out.println(preDate);// 从指定年月日中获取时间实例LocalDate dateInstance = LocalDate.of(2021, 8, 28);// LocalDate 实例 2021-08-28System.out.println(dateInstance);// 获取年 2021System.out.println(dateInstance.getYear());// 获取月 AUGUSTSystem.out.println(dateInstance.getMonth());// 获取月份数值 1就是1月 2就是2月.....System.out.println(dateInstance.getMonthValue());// 今日是当前时间月的第几天System.out.println(dateInstance.getDayOfMonth());// 一年中的第几天System.out.println(dateInstance.getDayOfYear());// 一个月中的第几天System.out.println(dateInstance.getDayOfMonth());// 礼拜几 ex:SATURDAY 周六System.out.println(dateInstance.getDayOfWeek());// 礼拜几 数字System.out.println(dateInstance.getDayOfWeek().getValue());// 一个月中的第几天System.out.println(dateInstance.get(ChronoField.DAY_OF_MONTH));// 一年中的第几天System.out.println(dateInstance.get(ChronoField.DAY_OF_YEAR));// 获取某天开始于结束时间戳LocalDate startDate = LocalDate.now();// +8是北京时间Long minTime = LocalDateTime.of(startDate, LocalTime.MIN).toInstant(ZoneOffset.of("+8")).toEpochMilli();Long maxTime = LocalDateTime.of(startDate, LocalTime.MAX).toInstant(ZoneOffset.of("+8")).toEpochMilli();System.out.println(minTime);System.out.println(maxTime);}
}
(2)LocalDateTime 操作时间值(详细的时间 包含年月日时分秒)
public class LocalDateTimeDemo {public static void main(String[] args) {//获取当前时间 2021-08-28T21:02:38.644LocalDateTime now = LocalDateTime.now();System.out.println(now);//格式化 20210828System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE));//格式化 2021-08-28 21:02:38DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");System.out.println(now.format(formatter));// 时间偏移 我这里是减一天加一月 并格式化 2021-09-27 21:02:38System.out.println(now.minusDays(1).plusMonths(1).format(formatter));// 获取当前年 月 日 当前年:2021 当前月:8 当前日:28System.out.println("当前年:" + now.getYear() + " 当前月:" + now.getMonthValue() + " 当前日:" + now.getDayOfMonth());//LocalDate + LocalTime 组合为 LocalDateTimeLocalDate localDate = LocalDate.now();LocalTime localTime = LocalTime.now();// 2021-08-28 21:02:38System.out.println(LocalDateTime.of(localDate, localTime).format(formatter));}
}
(3)Duration 求时间差(基于时间值LocalDateTime)
注意between两个参数前后顺序 param1 为开始时间 必须要小于param2 结束时间,如开始时间大于结束时间,那么结果为负数
public class ChronoUnitDemo {public static void main(String[] args) {LocalDateTime time1 = LocalDateTime.of(2019, 12, 2, 0, 0);LocalDateTime time2 = LocalDateTime.now();System.out.println("相差秒:" + ChronoUnit.SECONDS.between(time1, time2));System.out.println("相差小时:" + ChronoUnit.HOURS.between(time1, time2));System.out.println("相差半天:" + ChronoUnit.HALF_DAYS.between(time1, time2));System.out.println("相差天:" + ChronoUnit.DAYS.between(time1, time2));System.out.println("相差周:" + ChronoUnit.WEEKS.between(time1, time2));System.out.println("相差月:" + ChronoUnit.MONTHS.between(time1, time2));System.out.println("相差年" + ChronoUnit.YEARS.between(time1, time2));System.out.println("相差年" + ChronoUnit.YEARS.between(time1, time2));}
}
public class DurationDemo {public static void main(String[] args) {LocalDateTime now = LocalDateTime.now();// 2021-08-28T21:04:39.961System.out.println(now);LocalDateTime of = LocalDateTime.of(2020, 9, 28, 9, 5, 25, 720);// 2020-09-28T09:05:25.000000720System.out.println(of);// between(p1,p1) p1为开始时间 p2为结束时间 求值结果是是p2-p1 所以要注意前后顺序,否则结果为负数Duration duration = Duration.between(of, now);System.out.println(duration);//两个时间相差天数: 334天System.out.println(duration.toDays());//两个时间相差小时: 8028小时System.out.println(duration.toHours());//两个时间相差分钟: 481680 分钟System.out.println(duration.toMinutes());//两个时间相差毫秒: 28900809381 毫秒System.out.println(duration.toMillis());}
}
(4)ChronoUnit 求时间差(基于时间值LocalDateTime)
java8工具 ChronoUnit 求时间差
注意between两个参数前后顺序 param1 为开始时间 必须要小于param2 结束时间,如开始时间大于结束时间,那么结果为负数
public class ChronoUnitDemo {public static void main(String[] args) {LocalDateTime time1 = LocalDateTime.of(2019, 12, 2, 0, 0);LocalDateTime time2 = LocalDateTime.now();System.out.println("相差秒:" + ChronoUnit.SECONDS.between(time1, time2));System.out.println("相差小时:" + ChronoUnit.HOURS.between(time1, time2));System.out.println("相差半天:" + ChronoUnit.HALF_DAYS.between(time1, time2));System.out.println("相差天:" + ChronoUnit.DAYS.between(time1, time2));System.out.println("相差周:" + ChronoUnit.WEEKS.between(time1, time2));System.out.println("相差月:" + ChronoUnit.MONTHS.between(time1, time2));System.out.println("相差年" + ChronoUnit.YEARS.between(time1, time2));System.out.println("相差年" + ChronoUnit.YEARS.between(time1, time2));}
}
(5)Period 求时间差(基于日期值 LocalDate)
Period 比较的是两个日期之间相差数据,比如 2020-08-28 与 2021年 08-11 相差多久?
答案是一共相差 0年 10个月 零13天,getXXX即可取得对应相差值
public class PeriodDemo {public static void main(String[] args) {Period period = Period.between(LocalDate.of(2020, 8, 28),LocalDate.of(2021, 7, 11));// 0System.out.println(period.getYears());// 11System.out.println(period.getMonths());// 13System.out.println(period.getDays());}
}
【JAVA8】新时间API操作大全相关推荐
- Java8新特性之新时间API
Java8新特性之新时间API 一.新时间API 1.1 之前时间API存在问题:线程安全问题.设计混乱 1.2 本地化日期时间API:LoaclDate.LocalTime.LocalDateTim ...
- Java8日期时间API,Java高级多线程面试
plusNanos(int offset):增加指定纳秒 减少相关的方法 minusYears(int offset):减少指定年 minusMonths(int offset):减少指定月 minu ...
- Java8 日期时间的操作技巧
在 Java 8 中 推出了LocalDate.LocalTime.LocalDateTime这个三个时间处理类,以此来弥补之前的日期时间类的不足,简化日期时间的操作. Java8 日期和时间类包含L ...
- JAVA时间类及java8新时间类
文章目录 java.util.Date 时区 java.util.Date基本使用 初始化 输出时间 输出时差 打印毫秒数 比较时间 java.util.Date的子类 java.sql.Date的使 ...
- Java8————日期时间 API
引言 Java 1.0 中引入了 Date 类,Java 1.1 中引入了 Calendar ,Java 8 中引入了java.time API ,这是 Java 非常吸引人的一次对时间日期的升级.它 ...
- JDK8中的新时间API:Duration Period和ChronoUnit介绍
文章目录 简介 Duration Period ChronoUnit 简介 在JDK8中,引入了三个非常有用的时间相关的API:Duration,Period和ChronoUnit. 他们都是用来对时 ...
- 这么详细的Java8日期时间API,还不赶紧收藏起来(四):ZoneId和ZonedDateTime
ZoneId java.time.ZoneId类表示时区ID 声明: public abstract class ZoneId implements Serializable ZoneId可以用于在I ...
- 6 日期字符串转日期_Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类...
因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了 ...
- java 包结构 枚举类_Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类...
因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了 ...
最新文章
- Linux+Apache+MySQL+PHP5的安装与配置与phpBB2论坛的架设
- SAP 电商云 UI 如何知道当前的 breakpoint 是 xl - extremely large?
- 005---基于UDP的套接字
- linux-shell-变量参数
- Week of year bug in Ax3
- Android 调试桥 (adb)
- Ubuntu20.04 Clion/Pycharm/IDEA 输入中文+光标跟随解决方案
- stack的常见用法详解
- bbs.php168,PHP168与PHPWIND深度联手 CMS+BBS整合将成趋势
- CentOS 7 iSCSI 多路径(Multipath)配置
- python3 输出 26个大小写英文字母
- windows powershell/cmd更改字体
- FileStore omap的实现
- 29、RFM客户价值分析(有图有案例)
- IE8经常无故卡死终极解决办法分享
- 神经网络中验证集起了什么作用?
- Linux下打包命令tar
- 38亿人财富比不过最富26人 乐施会呼吁富人增税
- 国家气象局天气预报接口
- 微信打开域名网址被拦截 域名网址被微信拦截怎么办