加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java1.8新特性时间与日期

发布时间:2020-12-15 07:25:56 所属栏目:Java 来源:网络整理
导读://获取当前时间LocalDateTime currentTime = LocalDateTime.now();System.out.println("当前时间: " + currentTime);//把当前时间转成当前日期。比如:2019-10-1LocalDate date1 = currentTime.toLocalDate();System.out.println("date1 = " + date1);//获取
//获取当前时间LocalDateTime currentTime = LocalDateTime.now();System.out.println("当前时间:  " + currentTime);//把当前时间转成当前日期。比如:2019-10-1LocalDate date1 = currentTime.toLocalDate();System.out.println("date1 = " + date1);//获取月份Month month = currentTime.getMonth();//获取日期int day = currentTime.getDayOfMonth();//获取秒int seconds = currentTime.getSecond();System.out.println("月:"+month+",日:"+day+",秒:"+seconds);//把当前年换成2012年月换成10月LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);System.out.println("date2 = " + date2);//2014-12-12,Month.DECEMBER是12月。第一个参数是年,第二个参数是月,第三个参数是日LocalDate date3 = LocalDate.of(2014,Month.DECEMBER,12);System.out.println("date3 = " + date3);//这是设置时间,小时分。最后输出是22:15,不包含日期LocalTime date4 = LocalTime.of(22,15);System.out.println("date4 = " + date4);//把字符串转成时间类型LocalTime date5 = LocalTime.parse("20:15:30");System.out.println("date5 = " + date5);//时区下的示例//获取当前时间日期//Asia : 亚洲    Shanghai :上海ZonedDateTime date6 = ZonedDateTime.parse("2019-10-02T10:21:32+05:50[Asia/Shanghai]");System.out.println("date6 = " + date6);//Europe :欧洲    Paris:巴黎ZoneId id = ZoneId.of("Europe/Paris");System.out.println("id = " + id);//获取系统默认的时区ZoneId currentZone = ZoneId.systemDefault();System.out.println("当前时区 : " + currentZone);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读