【深度思考,极客大学Java进阶训练营

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);

输出结果:

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

image

如果使用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")));

最后

由于篇幅限制,小编在此截出几张知识讲解的图解,有需要的程序猿(媛)可以点赞后戳这里免费领取全部资料获取哦

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

P8级大佬整理在Github上45K+star手册,吃透消化,面试跳槽不心慌

e.format(DateTimeFormatter.ofPattern(“yyyy/MM/dd”)));




### 最后

**由于篇幅限制,小编在此截出几张知识讲解的图解,有需要的程序猿(媛)可以点赞后[戳这里免费领取全部资料](https://codechina.csdn.net/m0_60958482/java-p7)获取哦**

[外链图片转存中...(img-o9js4Eij-1630169564758)]

[外链图片转存中...(img-k43olbGi-1630169564758)]

[外链图片转存中...(img-pdqgv8zx-1630169564759)]

[外链图片转存中...(img-N7KXNDDM-1630169564759)]

[外链图片转存中...(img-V5y41cnA-1630169564760)]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值