常用API

您所在的位置:网站首页 jdk常用的类 常用API

常用API

2023-03-14 12:07| 来源: 网络整理| 查看: 265

为什么要学JDK8新增时间相关类?

 JDK8时间类分类:

 1.ZoneId 时区

方法名说明static  Set  getAvailableZoneIds()获取Java中支持的所有时区static  ZoneId  systemDefaylt()获取系统默认时区static  ZoneId  of(String  zoneId)获取一个指定时区 import java.time.ZoneId; import java.util.Set; public class ZoneIdDemo1 { public static void main(String[] args) { //获取所有时区名称 Set zoneIds = ZoneId.getAvailableZoneIds(); System.out.println(zoneIds); System.out.println(zoneIds.size());//601 //获取当前系统默认时区 ZoneId zoneId = ZoneId.systemDefault(); System.out.println(zoneId);//Asia/Shanghai //获取指定的时区 ZoneId zoneId1 = ZoneId.of("Asia/Tbilisi"); System.out.println(zoneId1);//Asia/Tbilisi } }

2.Instant 时间戳

方法名说明static  Instant  now()获取当前时间的Instant对象(标准时间)static  Instant  ofXxxx(long  epochMilli)根据(秒/毫秒/纳秒)获取Instant对象ZonedDateTime  atZone(ZoneId  zone)指定时区获取带时区的时间boolean  isXxx(Instant  otherInstant)判断系列的方法Instant  minusXxx(long  millisToSubtract)减少时间系列的方法Instant  plusXxx(long  millisToSubtract)增加时间系列的方法 import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; public class InstantDemo2 { public static void main(String[] args) { //获取当前时间的Instant对象(标准时间) Instant now = Instant.now(); System.out.println(now);//2023-03-11T05:08:42.641Z //根据(秒/毫秒/纳秒)获取Instant对象 Instant instant1 = Instant.ofEpochMilli(0L); System.out.println(instant1);//1970-01-01T00:00:00Z Instant instant2 = Instant.ofEpochSecond(1L); System.out.println(instant2);//1970-01-01T00:00:01Z Instant instant3 = Instant.ofEpochSecond(1L, 1000000000L); System.out.println(instant3);//1970-01-01T00:00:02Z //指定时区获取带时区的时间 ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai")); System.out.println(time);//2023-03-11T13:19:04.716+08:00[Asia/Shanghai] //isXxx 判断 Instant instant4 = Instant.ofEpochMilli(0L); Instant instant5 = Instant.ofEpochMilli(20L); boolean result1 = instant4.isAfter(instant5); System.out.println(result1);//false boolean result2 = instant4.isBefore(instant5); System.out.println(result2);//true //minusXxx 减少时间 Instant instant6 = Instant.ofEpochMilli(2000L); System.out.println(instant6);//1970-01-01T00:00:02Z Instant instant7 = instant6.minusSeconds(1L); System.out.println(instant7);//1970-01-01T00:00:01Z } }

3.ZoneDateTime 带时区的时间

方法名说明static  ZonedDateTime  now()获取当前时间的ZonedDateTime对象static  ZonedDateTime  ofXxxx(...)获取指定时间的ZonedDateTime对象ZonedDateTime  withXxx(时间)修改时间系列方法ZonedDateTime  minusXxx(时间)减少时间系列的方法ZonedDateTime  plusXxx(时间)增加时间系列的方法 import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; public class ZonedDateTimeDemo1 { public static void main(String[] args) { //获取带时区当前时间对象 ZonedDateTime now = ZonedDateTime.now(); System.out.println(now);//2023-03-11T14:02:28.113+08:00[Asia/Shanghai] //获取带时区指定的时间对象 ZonedDateTime time1 = ZonedDateTime.of(2023, 3, 11, 14, 4, 0, 0, ZoneId.of("Asia/Shanghai")); System.out.println(time1);//2023-03-11T14:04+08:00[Asia/Shanghai] Instant instant = Instant.ofEpochSecond(0L); ZoneId zoneId = ZoneId.of("Asia/Shanghai"); ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId); System.out.println(time2);//1970-01-01T08:00+08:00[Asia/Shanghai] //withXxx 修改时间 ZonedDateTime time3 = time2.withYear(2000); System.out.println(time3);//2000-01-01T08:00+08:00[Asia/Shanghai] ZonedDateTime time4 = time3.withDayOfMonth(20); System.out.println(time4);//2000-01-20T08:00+08:00[Asia/Shanghai] //minusXxx 减少时间 ZonedDateTime time5 = time4.minusYears(2L); System.out.println(time5);//1998-01-20T08:00+08:00[Asia/Shanghai] //plusXxx 增加时间 ZonedDateTime time6 = time5.plusYears(2L); System.out.println(time6);//2000-01-20T08:00+08:00[Asia/Shanghai] } }

4.DateTimeFormatter 用于时间的格式化和解析

方法名说明static  DateTimeFormatter  ofPattern(格式)获取格式对象String  format(时间对象)按照指定方式格式化 import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class DateTimeFormatterDemo1 { public static void main(String[] args) { //获取时间对象 ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai")); //获取格式对象 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a"); String time1 = dtf.format(time); System.out.println(time1);//2023-03-11 14:33:26 星期六 下午 } }

5.LocalDate、LocalTime、LocalDateTime

方法名说明public  LocalDate  toLocalDate()LocalDateTime转化成一个LocalDate对象public  LocalTime  toLocalTime()LocalDateTime转化成一个LocalTime对象

LocalDate、LocalTime、LocalDateTime相关方法

方法名说明static  XXX  now()获取当前时间的对象static  XXX  of(...)获取指定时间的对象get开头的方法获取日历中的年、月、日、时、分、秒等信息isBefore,isAfter比较两个时间谁先谁后with开头的修改时间系列的方法minus开头的减少时间系列的方法plus开头的增加时间系列的方法

6.工具类 Duration、Period、ChronoUnit

Duration:用于计算两个"时间"间隔(秒,纳秒)

Period:用于计算两个"日期"间隔(年、月、日)

ChronoUnit:用于计算两个"日期"间隔

import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class demo4 { public static void main(String[] args) { //用代码计算你活了多少天 JDK8 LocalDate ld1 = LocalDate.of(1999,12,15); LocalDate ld2 = LocalDate.now(); long days = ChronoUnit.DAYS.between(ld1,ld2); System.out.println(days); } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3