龙空技术网

1分钟熟知jdk8新特性(4 时间日期api)

不开心就撸代码 71

前言:

现在咱们对“java日期秒”大体比较着重,小伙伴们都想要知道一些“java日期秒”的相关内容。那么小编也在网上搜集了一些对于“java日期秒””的相关资讯,希望咱们能喜欢,大家一起来学习一下吧!

(一)使用 LocalDate、LocalTime、LocalDateTime

LocalDate、LocalTime、LocalDateTime 类的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。

方法

描述

now()

静态方法,根据当前时间创建对象

@Testpublic void demo1(){   LocalTime localTime = LocalTime.now();   System.out.println(localTime);   System.out.println("================");   LocalDate localDate = LocalDate.now();   System.out.println(localDate);   System.out.println("================");   LocalDateTime localDateTime = LocalDateTime.now();   System.out.println(localDateTime);}

of()

静态方法,根据指定日期/时间创建对象

@Testpublic void demo2(){   LocalTime localTime = LocalTime.of(1, 10, 30);   System.out.println(localTime);   System.out.println("================");   LocalDate localDate = LocalDate.of(2022,10,20);   System.out.println(localDate);   System.out.println("================");   LocalDateTime localDateTime = LocalDateTime.of(2022,10,20,10,10,10);   System.out.println(localDateTime);}

plusDays, plusWeeks,

plusMonths, plusYears

向当前 LocalDate 对象添加几天、几周、几个月、几年

minusDays, minusWeeks,

minusMonths, minusYears

从当前 LocalDate 对象减去几天、 几周、几个月、几年

plus, minus

添加或减少一个 Duration 或 Period

@Testpublic void demo3(){   LocalDateTime now = LocalDateTime.now();   now = now.plusDays(3).minusMonths(1);   System.out.println(now);}

withDayOfMonth,

withDayOfYear,

withMonth,

将月份天数、年份天数、月份、年份修改为指定的值并返回新 LocalDate 对象

@Testpublic void demo4(){   LocalDateTime now = LocalDateTime.now();   LocalDateTime localDateTime = now.withDayOfMonth(10).withHour(16);   System.out.println(localDateTime);}

getDayOfMonth

获得月份天数(1-31)

getDayOfYear

获得年份天数(1-366)

getDayOfWeek

获得星期几(返回一个 DayOfWeek枚举值)

getMonth

获得月份, 返回一个 Month 枚举值

getMonthValue

获得月份(1-12)

getYear

获得年份

@Testpublic void demo5(){   LocalDateTime now = LocalDateTime.now();   System.out.println("天"+now.getDayOfMonth());   System.out.println("星期"+now.getDayOfWeek());   System.out.println("一年的多少天"+now.getDayOfYear());   System.out.println("月"+now.getMonthValue());   System.out.println("年"+now.getYear());   System.out.println("月(英文)"+now.getMonth());   System.out.println("时"+now.getHour());   System.out.println("分"+now.getMinute());   System.out.println("秒"+now.getSecond());}

until

获得两个日期之间的 Period 对象,或者指定 ChronoUnits 的数字

isBefore, isAfter

比较两个 LocalDate

isLeapYear

判断是否是闰年

@Testpublic void demo6(){   /*   until 获得两个日期之间的 Period 对象,或者指定 ChronoUnits 的数字   isBefore, isAfter 比较两个 LocalDate   isLeapYear 判断是否是闰年   */   LocalDateTime now = LocalDateTime.now();   try {   Thread.sleep(2000);   } catch (InterruptedException e) {   e.printStackTrace();   }   LocalDateTime now1= LocalDateTime.now();   boolean before = now.isAfter(now1);   System.out.println(before);   LocalDate date = LocalDate.now();   boolean leapYear = date.isLeapYear();   System.out.println(leapYear);}
(二)Instant 时间戳

l 用于“时间戳”的运算。它是以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算

@Testpublic void demo7(){ Instant now = Instant.now(); //设置时区 OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8)); System.out.println(offsetDateTime); int nano = now.getNano(); System.out.println(nano); //获取毫秒值 long l = now.toEpochMilli(); System.out.println(l);}
(三)Duration 和 Period

Duration:用于计算两个“时间”间隔

Period:用于计算两个“日期”间隔

@Testpublic void demo8(){ /** * Duration:用于计算两个“时间”间隔 * l Period:用于计算两个“日期”间隔 */ LocalTime now = LocalTime.now();   try {   	Thread.sleep(2000);   } catch (InterruptedException e) {   	e.printStackTrace();   }   LocalTime now1 = LocalTime.now();   Duration between = Duration.between(now, now1);   System.out.println(between);   System.out.println(between.getSeconds());}
(四)日期的操纵

TemporalAdjuster : 时间校正器。有时我们可能需要获取例如:将日期调整到“下个周日”等操作。

TemporalAdjusters : 该类通过静态方法提供了大量的常用 TemporalAdjuster 的实现。

@Test public void demo9(){   LocalDateTime now = LocalDateTime.now();   DayOfWeek dayOfWeek = now.getDayOfWeek();   System.out.println(dayOfWeek);   //修改为这个月最后一天  // LocalDateTime with = now.with(TemporalAdjusters.lastDayOfMonth());   //修改为下周  // LocalDateTime with = now.with(TemporalAdjusters.next(dayOfWeek));   //修改为这个月第一天  // LocalDateTime with = now.with(TemporalAdjusters.firstDayOfMonth());   //修改为下个月第一天   LocalDateTime with = now.with(TemporalAdjusters.firstDayOfNextMonth());   System.out.println(with); }
(五)解析与格式化

java.time.format.DateTimeFormatter 类:该类提供了三种格式化方法:

预定义的标准格式

语言环境相关的格式

自定义的格式

@Testpublic void demo10(){   DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E");   String format = dateTimeFormatter.format(LocalDateTime.now());   System.out.println(format);   LocalDateTime parse = LocalDateTime.now().parse(format, dateTimeFormatter);   System.out.println(parse);}
(六)时区的处理

l Java8 中加入了对时区的支持,带时区的时间为分别为: ZonedDate、ZonedTime、ZonedDateTime 其中每个时区都对应着 ID,地区ID都为 “{区域}/{城市}”的格式。

例如 :Asia/Shanghai 等

ZoneId:该类中包含了所有的时区信息

getAvailableZoneIds() : 可以获取所有时区时区信息

of(id) : 用指定的时区信息获取 ZoneId 对象

@Testpublic void demo11(){   Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();   availableZoneIds.forEach(System.out::println);   LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));   System.out.println(now);   //========================   LocalDateTime now1 = LocalDateTime.now(ZoneId.of("US/Pacific"));   System.out.println(now1);}

结语:

授人以鱼不如授人以渔

标签: #java日期秒