Java8常用日期时间类

概述

Java8中推出了全新的日期时间类,使用起来更加简单和安全,常用的有以下几个
LocalDate:本地日期,只包括日期
LocalTime:本地时间,只包括时间
LocalDateTime:本地日期时间,包括日期和时间
ZonedDateTime:完整的本地日期时间,包括日期,时间,和时区

获取当前日期

LocalDate localDate = LocalDate.now();
System.out.println("当前日期为:" + localDate);//当前日期为:2020-06-19

LocalDate提供了获取年、月、日的快捷方法

System.out.println("当前年为:" + localDate.getYear());//当前年为:2020
System.out.println("当前月为:" + localDate.getMonth());//当前月为:JUNE
System.out.println("当前月为:" + localDate.getMonthValue());//当前月为:6
System.out.println("当前日为当月的第:" + localDate.getDayOfMonth() + "天");//当前日为当月的第:19天
System.out.println("当前日为当周的:" + localDate.getDayOfWeek());//当前日为当周的:FRIDAY
System.out.println("当前日为当年的第:" + localDate.getDayOfYear() + "天");//当前日为当年的第:171天

指定日期,这里的月对应的就是实际的月

 LocalDate zdDate = LocalDate.of(2020, 06, 18);
 System.out.println("指定日期为:" + zdDate);//指定日期为:2020-06-18

判断两个日期是否相等,直接使用equals即可

boolean flag = localDate.equals(zdDate);
System.out.println("两个日期是否相等" + flag);//两个日期是否相等false
boolean isEqual = localDate.isEqual(zdDate);
System.out.println("两个日期是否相等" + flag);//两个日期是否相等false

判断是否为闰年

System.out.println("是否为闰年:" + localDate.isLeapYear());//是否为闰年:true

只需要年月、月日,获取方式有多种

YearMonth yearMonth = YearMonth.now();
System.out.println("当前年月为:" + yearMonth);//当前年月为:2020-06
YearMonth currentYearMonth1 = YearMonth.from(localDate);
System.out.println("当前年月为:" + currentYearMonth1);//当前年月为:2020-06
YearMonth zdYearMonth = YearMonth.of(zdDate.getYear(), zdDate.getMonth());
System.out.println("指定年月为:" + zdYearMonth);//指定年月为:2020-06

返回当月天数

System.out.println("当月天数为:" + yearMonth.lengthOfMonth());//当月天数为:30

获取月日

MonthDay monthDay = MonthDay.now();
System.out.println("当前月日为:" + monthDay);//当前月日为:--06-19
MonthDay currentMonthDay = MonthDay.from(localDate);
System.out.println("当前月日为:" + currentMonthDay);//当前月日为:--06-19
MonthDay zdMonthDay = MonthDay.of(zdDate.getMonth(), zdDate.getDayOfMonth());
System.out.println("指定月日为:" + zdMonthDay);//指定月日为:--06-18

日期的加减

通过LocalDate的plus方法增加年、月、周、天,也可以使用plus后直接跟年(plusYears)、月(plusMonths)、周(plusWeeks)、日(plusDays);minus方法减少年、月、周、天,也可以使用minus后直接跟年(minusYears)、月(minusMonths)、周(minusWeeks)、日(minusDays)

LocalDate nextWeek = localDate.plus(1, ChronoUnit.WEEKS);
System.out.println("一周后日期为:" + nextWeek);//一周后日期为:2020-06-26
LocalDate newWeek = localDate.plusWeeks(1);
System.out.println("一周后日期为:" + newWeek);//一周后日期为:2020-06-26
LocalDate oldWeek = localDate.minus(1, ChronoUnit.WEEKS);
System.out.println("一周前日期为:" + oldWeek);//一周前日期为:2020-06-12

获取当前时间,默认时间格式为hh:mm:ss:nnn

LocalTime localTime = LocalTime.now();
System.out.println("当前时间为:" + localTime);//当前时间为:16:17:35.717
LocalTime zdLocalTime = LocalTime.of(14, 40, 30);
System.out.println("指定时间为:" + zdLocalTime);//指定时间为:14:40:30

时间的加减

通过LocalTime的plus方法增加时、分、秒,也可以使用plus后直接跟时(plusHours)、分(plusMinutes)、秒(plusSeconds);minus方法减少时、分、秒,也可以使用minus后直接跟时(minusHours)、分(minusMinutes)、秒(minusSeconds)

LocalTime newLocalTimeHour = localTime.plusHours(2);
System.out.println("2个小时后时间为:" + newLocalTimeHour);//2个小时后时间为:18:17:35.717
LocalTime oldLocalTimeHour = localTime.minusHours(2);
System.out.println("2个小时前时间为:" + oldLocalTimeHour);//2个小时前时间为:14:17:35.717
LocalTime newLocalTimeMinute = localTime.plusMinutes(20);
System.out.println("20分钟后时间为:" + newLocalTimeMinute);//20分钟后时间为:16:37:35.717
LocalTime oldLocalTimeMinute = localTime.minusMinutes(20);
System.out.println("20分钟前时间为:" + oldLocalTimeMinute);//20分钟前时间为:15:57:35.717

使用Duration计算两个时间间隔多少小时、分钟、秒

long minutes = Duration.between(zdLocalTime, localTime).toMinutes();
System.out.println("两个时间间隔为:" + minutes);//两个时间间隔为:97

使用Duration计算两个日期间隔天数

LocalDate localDate = LocalDate.now();
LocalDate agoDate = LocalDate.of(2020, 12, 20);
long days = Duration.between(agoDate.atStartOfDay(), localDate.atStartOfDay()).toDays();

使用ChronoUnit计算两个日期间隔天数

LocalDate localDate = LocalDate.now();
LocalDate agoDate = LocalDate.of(2020, 12, 20);
long days = ChronoUnit.DAYS.between(agoDate, localDate);

使用LocalDate计算两个日期间隔天数

LocalDate localDate = LocalDate.now();
LocalDate agoDate = LocalDate.of(2020, 12, 20);
long days = localDate.toEpochDay() - agoDate.toEpochDay();

使用Period计算两个日期间隔多少年、月、天

long days = Period.between(zdDate, localDate).getDays();
System.out.println("两个日期间隔为:" + days);//两个日期间隔为:1

Instant时间戳,默认使用 UTC 时区(世界协调时间)

Instant instant = Instant.now();
System.out.println(instant);//2020-06-19T08:17:35.718Z
System.out.println("时间戳秒" + instant.getEpochSecond());//时间戳秒1592554655
System.out.println("时间戳毫秒" + instant.toEpochMilli());//时间戳毫秒1592554655718
System.out.println("在1970-01-01T00:00:00上加上20秒为:" + Instant.ofEpochSecond(20));//在1970-01-01T00:00:00上加上20秒为:1970-01-01T00:00:20Z
System.out.println("在1970-01-01T00:00:00上加上20毫秒为:" + Instant.ofEpochMilli(20));//在1970-01-01T00:00:00上加上20毫秒为:1970-01-01T00:00:00.020Z

使用LocalDateTime获取时间戳

LocalDateTime localDateTime = LocalDateTime.now();
long second = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
System.out.println("当前日期时间戳(精确到秒)为:" + second);
long milli = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
System.out.println("当前日期时间戳(精确到毫秒)为:" + milli);

将时间戳转化为LocalDateTime

LocalDateTime localDateTime1 = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
System.out.println("将时间戳(精确到秒)转化为时间为:" + localDateTime1);
LocalDateTime localDateTime2 = LocalDateTime.ofInstant(Instant.ofEpochSecond(second), ZoneId.systemDefault());
System.out.println("将时间戳(精确到秒)转化为时间为:" + localDateTime2);
LocalDateTime localDateTime3 = LocalDateTime.ofInstant(Instant.ofEpochMilli(milli), ZoneId.systemDefault());
System.out.println("将时间戳(精确到毫秒)转化为时间为:" + localDateTime3);

时区偏移量计算

OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println("偏移8小时后为:" + offsetDateTime);//偏移8小时后为:2020-06-19T16:30:55.011+08:00

Clock时钟类获取当前时间戳

Clock clock = Clock.systemUTC();
System.out.println("Clock:" + clock);//Clock:SystemClock[Z]
System.out.println("Clock时间戳为" + clock.millis());//Clock时间戳为1592555455013
Clock defaultClock = Clock.systemDefaultZone();
System.out.println(defaultClock);//SystemClock[Asia/Shanghai]
System.out.println("Clock系统时间戳为" + defaultClock.millis());//Clock系统时间戳为1592555455013

判断日期早于或晚于

boolean isBeforeDate = localDate.isBefore(zdDate);
System.out.println("当前日期是否早于指定日期" + isBeforeDate);//当前日期是否早于指定日期false
boolean isAfterDate = localDate.isAfter(zdDate);
System.out.println("当前日期是否晚于指定日期" + isAfterDate);//当前日期是否晚于指定日期true

判断时间早于或晚于

boolean isBeforeTime = localTime.isBefore(zdLocalTime);
System.out.println("当前时间是否早于指定时间" + isBeforeTime);//当前时间是否早于指定时间false
boolean isAfterTime = localTime.isAfter(zdLocalTime);
System.out.println("当前时间是否晚于指定时间" + isAfterTime);//当前时间是否晚于指定时间true

获取当前日期时间

LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("当前日期时间为:" + localDateTime);//当前日期时间为:2020-06-19T16:30:55.013
LocalDateTime zdLocalDateTime1 = LocalDateTime.of(2020, 06, 19, 15, 20, 30);
System.out.println("指定日期时间为:" + zdLocalDateTime1);//指定日期时间为:2020-06-19T15:20:30
LocalDateTime zdLocalDateTime2 = LocalDateTime.of(zdDate, zdLocalTime);
System.out.println("指定日期时间为:" + zdLocalDateTime2);//指定日期时间为:2020-06-18T14:40:30
LocalDateTime nextLocalDateTime = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println("下一个周日的日期时间为:" + nextLocalDateTime);//下一个周日的日期时间为:2020-06-21T16:30:55.013

带时区的日期时间

ZoneId america = ZoneId.of("America/New_York");
ZonedDateTime zonedDateTimeNewYork = ZonedDateTime.of(localDateTime, america);
System.out.println("指定时区日期时间为:" + zonedDateTimeNewYork);//指定时区日期时间为:2020-06-19T16:30:55.013-04:00[America/New_York]

格式化日期时间

String formatLocalDate = localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
System.out.println("格式化日期为:" + formatLocalDate);//格式化日期为:20200619
String formatLocalDateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
System.out.println("格式化日期时间为:" + formatLocalDateTime);//格式化日期时间为:20200619163055

将字符串转换为日期

LocalDate parseLocalDate = LocalDate.parse("2020-06-19", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println("将指定的日期字符串转换为日期为:" + parseLocalDate);//将指定的日期字符串转换为日期为:2020-06-19

Date转换为LocalDateTime、LocalDate

Date date = new Date();
Instant instant = date.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
LocalDateTime localDateTime1 = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDate localDate1 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();
LocalDate localDate2 = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();

LocalDateTime、LocalDate转换为Date

LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
LocalDate localDate = LocalDate.now();
Date date1 = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值