Java学习 时间类 Period类与Duration类 / LocalDate类与Instant类 用法详解
2024-08-29 03:32:14
前言
java 8 中引入的两个与日期相关的新类:Period 和 Duration。两个类看表示时间量或两个日期之间的差,两者之间的差异为:Period基于日期值,而Duration基于时间值。他们估计最大的作用就不需要你自己复杂的计算关于年龄的年数与余天.
Period类与Duration类都是一段持续时间的概念,如果需要对比时间他们就需要一个固定的时间值所以就需要 LocalDate类与Instant类来配合他们使用:
Period 对应使用 LocalDate 他们的作用范围域都是日期(年/月/日)
Duration 对应使用 Instant 他们的作用范围域都是时间(天/时/分/秒/毫秒/纳秒)
LocalDate
精度到日期记录固定时间值的LocalDate,创建方式:
LocalDate localDate1 = LocalDate.of(2019,9,1);
LocalDate localDate2 = LocalDate.ofYearDay(2019,150);
Period
对比时间
LocalDate start = LocalDate.of(2019,9,25);
LocalDate end = LocalDate.of(2019,9,29);
Period period = Period.between(start, end);
Log.e(TAG, "onClick: 天数="+period.getDays());
Instant
以精度到纳秒记录固定的时间值的Instant,创建方式:
Instant instant1 = Instant.parse("2017-10-03T10:15:30.00Z");//用解析字符串的形式创建
Instant instant2 = Instant.ofEpochMilli(System.currentTimeMillis());//用传入单位为毫秒的时间戳创建
Instant instant3 = Instant.ofEpochSecond(System.currentTimeMillis()/1000);//用传入为单位秒的时间戳创建
Instant instant4 = Instant.EPOCH; //获取一个1970纪元年0时的时间
以上代码都是创建了某个时间点的值
Duration
Duration字面意思是持续时间, 注意! 在Android中使用,因为Duration是Java 8才引入的,使用Android需要最低API26才能使用
设置指定单位的持续时间
Duration durationDays = Duration.ofDays(1);//天
Duration durationHours = Duration.ofHours(1);//小时
Duration durationMinutes = Duration.ofMinutes(1);//分
Duration durationSeconds = Duration.ofSeconds(1);//秒
Duration durationMillis = Duration.ofMillis(1);//毫秒
以上操作可以实例一个指定时间值的Duration.不要急,这是需要结合下面的 获取指定单位时间来使用的.
获取指定单位的持续时间
Duration duration = Duration.ofDays(1);//设置一天时间
long timeHours = duration.toHours();//小时
long timeMinutes = duration.toMinutes();//分钟
long timeMillis = duration.toMillis();//毫秒
long timeNanos = duration.toNanos();//纳秒
String timeString = duration.toString(); //此持续时间的字符串表示形式,使用基于ISO-8601秒*的表示形式,例如 PT8H6M12.345S
Log.e(TAG, "timeHours时间="+timeHours);
Log.e(TAG, "timeMinutes时间="+timeMinutes);
Log.e(TAG, "timeMillis时间="+timeMillis);
Log.e(TAG, "timeNanos时间="+timeNanos);
Log.e(TAG, "timeString时间="+timeString);
结果:
2019-09-29 15:27:33.942 28862-28862/demo_nav.yt.com.demo_nav E/demo.yt.com.demo.MainActivity: timeHours时间=24
2019-09-29 15:27:33.942 28862-28862/demo_nav.yt.com.demo_nav E/demo.yt.com.demo.MainActivity: timeMinutes时间=1440
2019-09-29 15:27:33.942 28862-28862/demo_nav.yt.com.demo_nav E/demo.yt.com.demo.MainActivity: timeMillis时间=86400000
2019-09-29 15:27:33.942 28862-28862/demo_nav.yt.com.demo_nav E/demo.yt.com.demo.MainActivity: timeNanos时间=86400000000000
2019-09-29 15:27:33.942 28862-28862/demo_nav.yt.com.demo_nav E/demo.yt.com.demo.MainActivity: timeString时间=PT24H
获取2个时间点之间差值的持续时间
long todayTimeMillis = System.currentTimeMillis();
long yesterdayTimeMillis = todayTimeMillis - 24 * 60 * 60 * 1000;
Instant start = Instant.ofEpochMilli(yesterdayTimeMillis);
Instant end = Instant.ofEpochMilli(todayTimeMillis); Duration duration = Duration.between(start, end);
Log.e(TAG, "onClick: 天数="+duration.toDays());
结果:
2019-09-29 16:33:38.063 1830-1830/demo_nav.yt.com.demo_nav E/demo.yt.com.demo.MainActivity: onClick: 天数=1
注意这个天数是可以负数,意味着如果开始时间比结束时间更后面就会得到负数天数
end
最新文章
- UDP通信
- 不使用return false阻止event默认行为
- Qt——一些工具的使用
- 软件测试第四周--关于int.parse()的类型转换问题
- Xamarin Android.Views.WindowManagerBadTokenException: Unable to add window -- token android.os.BinderProxy
- win7 下设置时间格式为yyyy-MM-dd 格式无效的解决方法
- 维护计划生成的SSIS包存储在哪
- LeetCode Spiral Matrix II (技巧)
- XSS攻击及防御(转)
- 横竖屏事件响应(viewWillLayoutSubviews和通知)两种方式
- 【转】oracle创建表空间
- ffmpeg ffplay ffprobe资料整理
- apache安装过程中的常见问题
- 【技术分析】DowginCw病毒家族解析
- .net 4种单例模式
- Xcode8 添加PCH文件
- 《读书报告 -- Elasticsearch入门 》-- 安装以及简单使用(1)
- 码云git使用五(创建远程分支和更新远程分支)
- trinitycore 魔兽服务器源码分析(一) 网络
- umi怎么去添加配置式路由
热门文章
- 《深入理解Java虚拟机》- 重载与重写
- hadoop系列(二)分布式文件系统HDFS
- SQLite 小调研
- 随笔记录 重置root密码 2019.8.7
- tf.matmul() 和tf.multiply() 的区别
- Codeigniter Session: Configured save path is not a directory
- windows下安装jenkins初级(2)
- Ubuntu's Software
- 欧拉定理、欧拉函数、a/b%c
- c++ exit() 函数