写点什么

【深度思考】JDK8 中日期类型该如何使用,java 面试题百度网盘

用户头像
极客good
关注
发布于: 刚刚

2020 is leap year:true

3. LocalTime

3.1 获取时分秒

如果使用java.util.Date,那代码是下面这样的:


Date date = new Date();


int hour = date.getHours();int minute = date.getMinutes();int second = date.getSeconds();


System.out.println("hour: " + hour);System.out.println("minute: " + minute);System.out.println("second: " + second);


输出结果:



注意事项:这几个方法已经过期了,因此强烈不建议在项目中使用:



如果使用java.util.Calendar,那代码是下面这样的:


Calendar calendar = Calendar.getInstance();


// 12 小时制 int hourOf12 = calendar.get(Calendar.HOUR);// 24 小时制 int hourOf24 = calendar.get(Calendar.HOUR_OF_DAY);int minute = calendar.get(Calendar.MINUTE);int second = calendar.get(Calendar.SECOND);int milliSecond = calendar.get(Calendar.MILLISECOND);


System.out.println("hourOf12: " + hourOf12);System.out.println("hourOf24: " + hourOf24);System.out.println("minute: " + minute);System.out.println("second: " + second);System.out.println("milliSecond: " + milliSecond);


输出结果:



**注意事项:**获取小时时,有 2 个选项,1 个返回 12 小时制的小时数,1 个返回 24 小时制的小时数,因为现在是晚上 8 点,所以`cale


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


ndar.get(Calendar.HOUR)返回8,而calendar.get(Calendar.HOUR_OF_DAY)`返回 20。


如果使用java.time.LocalTime,那代码是下面这样的:


LocalTime localTime = LocalTime.now();System.out.println("localTime:" + localTime);


int hour = localTime.getHour();int minute = localTime.getMinute();int second = localTime.getSecond();


System.out.println("hour: " + hour);System.out.println("minute: " + minute);System.out.println("second: " + second);


输出结果:



可以看出,LocalTime 只有时间没有日期。

4. LocalDateTime

4.1 获取当前时间

LocalDateTime localDateTime = LocalDateTime.now();System.out.println("localDateTime:" + localDateTime);


输出结果:


localDateTime: 2020-06-11T11:03:21.376

4.2 获取年月日时分秒

LocalDateTime localDateTime = LocalDateTime.now();System.out.println("localDateTime: " + localDateTime);


System.out.println("year: " + localDateTime.getYear());System.out.println("month: " + localDateTime.getMonthValue());System.out.println("day: " + localDateTime.getDayOfMonth());System.out.println("hour: " + localDateTime.getHour());System.out.println("minute: " + localDateTime.getMinute());System.out.println("second: " + localDateTime.getSecond());


输出结果:


4.3 增加天数/小时

LocalDateTime localDateTime = LocalDateTime.now();System.out.println("localDateTime: " + localDateTime);


LocalDateTime tomorrow = localDateTime.plusDays(1);System.out.println("tomorrow: " + tomorrow);


LocalDateTime nextHour = localDateTime.plusHours(1);System.out.println("nextHour: " + nextHour);


输出结果:


localDateTime: 2020-06-11T11:13:44.979

tomorrow: 2020-06-12T11:13:44.979

nextHour: 2020-06-11T12:13:44.979


LocalDateTime还提供了添加年、周、分钟、秒这些方法,这里就不一一列举了:


4.4 减少天数/小时

LocalDateTime localDateTime = LocalDateTime.now();System.out.println("localDateTime: " + localDateTime);


LocalDateTime yesterday = localDateTime.minusDays(1);System.out.println("yesterday: " + yesterday);


LocalDateTime lastHour = localDateTime.minusHours(1);System.out.println("lastHour: " + lastHour);


输出结果:


localDateTime: 2020-06-11T11:20:38.896

yesterday: 2020-06-10T11:20:38.896

lastHour: 2020-06-11T10:20:38.896


类似的,LocalDateTime还提供了减少年、周、分钟、秒这些方法,这里就不一一列举了:


4.5 获取时间是本周/本年的第几天

LocalDateTime localDateTime = LocalDateTime.now();System.out.println("localDateTime: " + localDateTime);


System.out.println("DayOfWeek: " + localDateTime.getDayOfWeek().getValue());System.out.println("DayOfYear: " + localDateTime.getDayOfYear());


输出结果:


localDateTime: 2020-06-11T11:32:31.731

DayOfWeek: 4

DayOfYear: 163

5. DateTimeFormatter

JDK8 中推出了java.time.format.DateTimeFormatter来处理日期格式化问题,《阿里巴巴 Java 开发手册》中也是建议使用DateTimeFormatter代替SimpleDateFormat

5.1 格式化 LocalDate

LocalDate localDate = LocalDate.now();


System.out.println("ISO_DATE: " + localDate.format(DateTimeFormatter.ISO_DATE));System.out.println("BASIC_ISO_DATE: " + localDate.format(DateTimeFormatter.BASIC_ISO_DATE));System.out.println("ISO_WEEK_DATE: " + localDate.format(DateTimeFormatter.ISO_WEEK_DATE));System.out.println("ISO_ORDINAL_DATE: " + localDate.format(DateTimeFormatter.ISO_ORDINAL_DATE));


输出结果:



如果提供的格式无法满足你的需求,你还可以像以前一样自定义格式:


LocalDate localDate = LocalDate.now();


System.out.println("yyyy/MM/dd: " + localDate.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));


输出结果:


yyyy/MM/dd: 2020/06/11

5.2 格式化 LocalTime

LocalTime localTime = LocalTime.now();System.out.println(localTime);System.out.println("ISO_TIME: " + localTime.format(DateTimeFormatter.ISO_TIME));System.out.println("HH:mm:ss: " + localTime.format(DateTimeFormatter.ofPattern("HH:mm:ss")));


输出结果:


14:28:35.230

ISO_TIME: 14:28:35.23

HH:mm:ss: 14:28:35

5.3 格式化 LocalDateTime

LocalDateTime localDateTime = LocalDateTime.now();System.out.println(localDateTime);System.out.println("ISO_DATE_TIME: " + localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));System.out.println("ISO_DATE: " + localDateTime.format(DateTimeFormatter.ISO_DATE));


输出结果:


2020-06-11T14:33:18.303

ISO_DATE_TIME: 2020-06-11T14:33:18.303

ISO_DATE: 2020-06-11

6. 类型相互转换

6.1 Instant 转 Date

JDK8 中,Date新增了from()方法,将Instant转换为Date,代码如下所示:


Instant instant = Instant.now();System.out.println(instant);


Date dateFromInstant = Date.from(instant);System.out.println(dateFromInstant);


输出结果:


2020-06-11T06:39:34.979Z

Thu Jun 11 14:39:34 CST 2020

6.2 Date 转 Instant

JDK8 中,Date新增了toInstant方法,将Date转换为Instant,代码如下所示:


Date date = new Date();Instant dateToInstant = date.toInstant();System.out.println(date);System.out.println(dateToInstant);


输出结果:


Thu Jun 11 14:46:12 CST 2020

2020-06-11T06:46:12.112Z

6.3 Date 转 LocalDateTime

Date date = new Date();Instant instant = date.toInstant();LocalDateTime localDateTimeOfInstant = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());System.out.println(date);System.out.println(localDateTimeOfInstant);


输出结果:


Thu Jun 11 14:51:07 CST 2020

2020-06-11T14:51:07.904

6.4 Date 转 LocalDate

Date date = new Date();Instant instant = date.toInstant();LocalDateTime localDateTimeOfInstant = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());LocalDate localDate = localDateTimeOfInstant.toLocalDate();System.out.println(date);System.out.println(localDate);


输出结果:


Thu Jun 11 14:59:38 CST 2020

2020-06-11


可以看出,Date是先转换为Instant,再转换为LocalDateTime,然后通过LocalDateTime获取LocalDate

6.5 Date 转 LocalTime

Date date = new Date();Instant instant = date.toInstant();LocalDateTime localDateTimeOfInstant = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());LocalTime toLocalTime = localDateTimeOfInstant.toLocalTime();System.out.println(date);System.out.println(toLocalTime);


输出结果:


Thu Jun 11 15:06:14 CST 2020

15:06:14.531


可以看出,Date是先转换为Instant,再转换为LocalDateTime,然后通过LocalDateTime获取LocalTime

6.6 LocalDateTime 转 Date

LocalDateTime localDateTime = LocalDateTime.now();


Instant toInstant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();Date dateFromInstant = Date.from(toInstant);System.out.println(localDateTime);System.out.println(dateFromInstant);


输出结果:


2020-06-11T15:12:11.600

Thu Jun 11 15:12:11 CST 2020

6.7 LocalDate 转 Date

LocalDate today = LocalDate.now();


LocalDateTime localDateTime = localDate.atStartOfDay();Instant toInstant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();Date dateFromLocalDate = Date.from(toInstant);System.out.println(dateFromLocalDate);


输出结果:


Thu Jun 11 00:00:00 CST 2020

6.8 LocalTime 转 Date

LocalDate localDate = LocalDate.now();LocalTime localTime = LocalTime.now();


LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);Instant instantFromLocalTime = localDateTime.atZone(ZoneId.systemDefault()).toInstant();Date dateFromLocalTime = Date.from(instantFromLocalTime);


System.out.println(dateFromLocalTime);


输出结果:


Thu Jun 11 15:24:18 CST 2020

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
【深度思考】JDK8中日期类型该如何使用,java面试题百度网盘