欧博客户端:【深度思索】JDK8中日期类型该若何使用?

admin 5个月前 (06-12) 科技 40 0

在JDK8之前,处置日期时间,我们主要使用3个类,DateSimpleDateFormatCalendar

这3个类在使用时都或多或少的存在一些问题,好比SimpleDateFormat不是线程平安的,

好比DateCalendar获取到的月份是0到11,而不是现实生活中的1到12,关于这一点,《阿里巴巴Java开发手册》中也有提及,由于很容易犯错:

不外,JDK8推出了全新的日期时间处置类解决了这些问题,好比InstantLocalDateLocalTimeLocalDateTimeDateTimeFormatter,在《阿里巴巴Java开发手册》中也推荐使用Instant

LocalDateTimeDateTimeFormatter

但我发现很多多少项目中实在并没有使用这些类,使用的照样之前的DateSimpleDateFormatCalendar,以是本篇博客就讲解下JDK8新推出的日期时间类,主要是下面几个:

  1. Instant
  2. LocalDate
  3. LocalTime
  4. LocalDateTime
  5. DateTimeFormatter

1. Instant

1.1 获取当前时间

既然Instant可以取代Date类,那它一定可以获取当前时间:

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

输出效果:

2020-06-10T08:22:13.759Z

仔细的你会发现,这个时间比北京时间少了8个小时,若是要输出北京时间,可以加上默认时区:

System.out.println(instant.atZone(ZoneId.systemDefault()));

输出效果:

2020-06-10T16:22:13.759+08:00[Asia/Shanghai]

1.2 获取时间戳

Instant instant = Instant.now();

// 当前时间戳:单元为秒
System.out.println(instant.getEpochSecond());
// 当前时间戳:单元为毫秒
System.out.println(instant.toEpochMilli());

输出效果:

1591777752

1591777752613

固然,也可以通过System.currentTimeMillis()获取当前毫秒数。

1.3 将long转换为Instant

1)凭据秒数时间戳转换:

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

long epochSecond = instant.getEpochSecond();
System.out.println(Instant.ofEpochSecond(epochSecond));
System.out.println(Instant.ofEpochSecond(epochSecond, instant.getNano()));

输出效果:

2020-06-10T08:40:54.046Z

2020-06-10T08:40:54Z

2020-06-10T08:40:54.046Z

2)凭据毫秒数时间戳转换:

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

long epochMilli = instant.toEpochMilli();
System.out.println(Instant.ofEpochMilli(epochMilli));

输出效果:

2020-06-10T08:43:25.607Z

2020-06-10T08:43:25.607Z

1.4 将String转换为Instant

String text = "2020-06-10T08:46:55.967Z";
Instant parseInstant = Instant.parse(text);
System.out.println("秒时间戳:" + parseInstant.getEpochSecond());
System.out.println("豪秒时间戳:" + parseInstant.toEpochMilli());
System.out.println("纳秒:" + parseInstant.getNano());

输出效果:

秒时间戳:1591778815

豪秒时间戳:1591778815967

纳秒:967000000

若是字符串花样纰谬,好比修改成2020-06-10T08:46:55.967,就会抛出java.time.format.DateTimeParseException异常,如下图所示:

2. LocalDate

2.1 获取当前日期

使用LocalDate获取当前日期异常简朴,如下所示:

LocalDate today = LocalDate.now();
System.out.println("today: " + today);

输出效果:

today: 2020-06-10

不用任何花样化,输出效果就异常友好,若是使用Date,输出这样的花样,还得配合SimpleDateFormat指定yyyy-MM-dd举行花样化,一不小心还会出个bug,好比去年年底很火的1个bug,我那时照样截了图的:

这2个密友是2019/12/31关注我的,但我2020年1月2号查看时,却显示成了2020/12/31,为啥呢?花样化日期时花样写错了,应该是yyyy/MM/dd,却写成了YYYY/MM/dd,恰好那周跨年,就显示成下一年,也就是2020年了,那时好几个博主写过文章剖析缘故原由,我这里就不做过多注释了。

划重点:都说到这了,给人人安利下我新注册的民众号「申城异乡人」,迎接人人关注,更多原创文章等着你哦,哈哈。

2.2 获取年月日

LocalDate today = LocalDate.now();

int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();

System.out.println("year: " + year);
System.out.println("month: " + month);
System.out.println("day: " + day);

输出效果:

year: 2020

month: 6

day: 10

获取月份终于返回1到12了,不像java.util.Calendar获取月份返回的是0到11,获取完还得加1。

2.3 指定日期

LocalDate specifiedDate = LocalDate.of(2020, 6, 1);
System.out.println("specifiedDate: " + specifiedDate);

输出效果:

specifiedDate: 2020-06-01

若是确定月份,推荐使用另一个重载方式,使用枚举指定月份:

LocalDate specifiedDate = LocalDate.of(2020, Month.JUNE, 1);

2.4 对照日期是否相等

LocalDate localDate1 = LocalDate.now();
LocalDate localDate2 = LocalDate.of(2020, 6, 10);
if (localDate1.equals(localDate2)) {
    System.out.println("localDate1 equals localDate2");
}

输出效果:

localDate1 equals localDate2

2.5 获取日期是本周/本月/今年的第几天

LocalDate today = LocalDate.now();

System.out.println("Today:" + today);
System.out.println("Today is:" + today.getDayOfWeek());
System.out.println("今天是本周的第" + today.getDayOfWeek().getValue() + "天");
System.out.println("今天是本月的第" + today.getDayOfMonth() + "天");
System.out.println("今天是今年的第" + today.getDayOfYear() + "天");

输出效果:

Today:2020-06-11

Today is:THURSDAY

今天是本周的第4天

今天是本月的第11天

今天是今年的第163天

2.6 判断是否为闰年

LocalDate today = LocalDate.now();

System.out.println(today.getYear() + " is leap year:" + today.isLeapYear());

输出效果:

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点,以是calendar.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

7. 总结

JDK8推出了全新的日期时间类,如InstantLocaleDateLocalTimeLocalDateTimeDateTimeFormatter,设计比之前更合理,也是线程平安的。

《阿里巴巴Java开发规范》中也推荐使用Instant取代DateLocalDateTime 取代 CalendarDateTimeFormatter 取代 SimpleDateFormat

因此,若是条件允许,建议在项目中使用,没有使用的,可以思量升级下。

,

ALLBET官网娱乐平台开户

欢迎进入ALLBET官网娱乐平台开户:www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。

皇冠体育声明:该文看法仅代表作者自己,与本平台无关。转载请注明:欧博客户端:【深度思索】JDK8中日期类型该若何使用?

网友评论

  • (*)

最新评论

文章归档

站点信息

  • 文章总数:625
  • 页面总数:0
  • 分类总数:8
  • 标签总数:1148
  • 评论总数:268
  • 浏览总数:8678