Jdk8新特性 【时间】 api LocalDate,LocalTime,LocalDateTime
LocalDate,LocalTime,LocalDateTime
接口api: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
java8时间:LocalDate、LocalTime、LocalDateTime【java8新提供的类】
使用方式,包括创建、获取、格式化、解析、计算、修改
@org.junit.Test public void testJdk8Time(){ creat(); get(); calculate(); format(); parse(); }
创建
public void creat(){ //创建 LocalDate nowDate = LocalDate.now(); //直接获取 LocalDate nowDateOf = LocalDate.of(2020, 2, 25);//指定年-月-日 LocalDate nowDateTo = LocalDateTime.now().toLocalDate(); System.out.println(nowDate); //2020-02-25 System.out.println(nowDateOf); //2020-02-25 System.out.println(nowDateTo); //2020-02-25 LocalTime nowTime = LocalTime.now(); LocalTime nowTimeOf = LocalTime.of(13, 50, 10); LocalTime nowTimeTo = LocalDateTime.now().toLocalTime(); System.out.println(nowTime); System.out.println(nowTimeOf); System.out.println(nowTimeTo); LocalDateTime nowDateTime = LocalDateTime.now(); LocalDateTime nowDateTimeOf = LocalDateTime.of(2020, 2, 25, 13, 50, 10); System.out.println(nowDateTime); System.out.println(nowDateTimeOf); }
获取
public void get(){ //获取数字: 年, 月, 日, 周几 LocalDate nowDate = LocalDate.now(); int year = nowDate.getYear(); //int year1 = nowDate.get(ChronoField.YEAR); int month = nowDate.getMonth().getValue(); //int month1 = nowDate.get(ChronoField.MONTH_OF_YEAR); int day = nowDate.getDayOfMonth(); int week = nowDate.getDayOfWeek().getValue(); System.out.println(year); //2020 System.out.println(month); //2 System.out.println(day);//25 System.out.println(week);//2 //获取数字: 时, 分, 秒 LocalTime nowTime = LocalTime.now(); int hour = nowTime.getHour(); //int hour1 = nowTime.get(ChronoField.HOUR_OF_DAY); int minute = nowTime.getMinute(); //int minute1 = nowTime.get(ChronoField.MINUTE_OF_HOUR); int second = nowTime.getMinute(); //int second1 = nowTime.get(ChronoField.SECOND_OF_MINUTE); System.out.println(hour); System.out.println(minute); System.out.println(second); //获取秒数 Instant instant = Instant.now(); long currentSecond = instant.getEpochSecond(); long currentMilli = instant.toEpochMilli(); long currentMilliSys = System.currentTimeMillis(); System.out.println(currentSecond); System.out.println(currentMilli); System.out.println(currentMilliSys); }
修改
public void update(){ //LocalDate、LocalTime、LocalDateTime、Instant为不可变对象,修改这些对象对象会返回一个副本 //增加、减少年数、月数、天数等 //plus LocalDate localDate = LocalDate.now(); localDate = localDate.plusYears(1); //加一年 localDate = localDate.plusMonths(1); //加一月 localDate = localDate.plusDays(1); //加一天 localDate = localDate.plusWeeks(1); //加一周 localDate = localDate.minusMonths(1); //减少一个月 //with 修改年为2019, 月为1 localDate = localDate.withYear(2019); localDate = localDate.withMonth(1); }
计算
public void calculate(){ //通过with计算: 这个月的第一天,最后一天、下个周末 LocalDate localDate = LocalDate.now(); LocalDate localDate1 = localDate.with(TemporalAdjusters.firstDayOfYear()); LocalDate localDate2 = localDate.with(TemporalAdjusters.lastDayOfYear()); LocalDate localDate3 = localDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)); System.out.println(localDate1); System.out.println(localDate2); System.out.println(localDate3); }
格式化
public void format(){ //格式化时间 LocalDate localDate = LocalDate.of(2019, 9, 10); String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE); String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); //自定义格式化 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); String s3 = localDate.format(dateTimeFormatter); System.out.println(s1); System.out.println(s2); System.out.println(s3); }
解析
public void parse(){ //解析时间 DateTimeFormatter是线程安全的 LocalDate localDate1 = LocalDate.parse("20190910", DateTimeFormatter.BASIC_ISO_DATE); LocalDate localDate2 = LocalDate.parse("2019-09-10", DateTimeFormatter.ISO_LOCAL_DATE); System.out.println(localDate1); System.out.println(localDate2); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
2019-02-25 Jemter进行压力测试
2019-02-25 Tomcat8优化方式(两个方向:tomcat和jvm)
2019-02-25 OSI七层模型和TCP五层模型