/** * Clock 获取指定日期时间 * Duration 获取一段时间 * Instant 获取具体的一个时刻 * LocalDate 代表不带时区的日期 * LocalTime 代表不带时区的时间 * LocalDateTime 代表不带时区的日期和时间 * Monthday 代表年月日 * Year 代表年 * YearMonth 代表年月 * ZonedDateTime 代表一个时区化的日期、时间 * ZoneId 代表一个时区 * DayOfWeek 枚举类 定义了周日到周六的枚举 * Month 枚举类 定义了1到12月的枚举 */ //----------------------关于Clock用法-------------------- //获取当前Clock用法 Clock clock = Clock.systemUTC(); //通过Clock获取当前时刻 System.out.println("当前时刻为:"+clock.instant()); //获取Clock对应的毫秒数 System.out.println(clock.millis()); System.out.println(System.currentTimeMillis()); //--------------关于Duration---------------- Duration duration = Duration.ofSeconds(8000); System.out.println("6000秒相当于"+duration.toMinutes()+"分钟"); System.out.println("6000秒相当于"+duration.toHours()+"小时"); System.out.println("6000秒相当于"+duration.toDays()+"天"); //在Clock基础上增加6000秒,返回新增的Clock Clock clock2 = Clock.offset(clock, duration); System.out.println("当前时刻增加6000秒为:"+clock2.instant()); //----------------Instant用法---------------- //获取当前时间 Instant instant = Instant.now(); System.out.println(instant); //instant增加6000秒 返回新的Instant Instant ins = instant.plusSeconds(6000); System.out.println(ins); //根据字符串解析Instant对象 String ti = "2014-02-23T10:12:35.342Z"; Instant instant3 = Instant.parse(ti); System.out.println(instant3); //在instant3的基础上添加5小时4分钟 Instant instant4 = instant3.plus(Duration.ofHours(5).plusMinutes(3)); System.out.println(instant4); //获取Instant4的5天前的时刻 Instant instant5 = instant4.minus(Duration.ofDays(5)); System.out.println(instant5); //---------------LocalDate--------------- //获取当前时间 LocalDate localDate = LocalDate.now(); System.out.println(localDate); //获取2017年的第5天 localDate = LocalDate.ofYearDay(2017, 5); System.out.println(localDate); //--------------LocalTime---------------- //获取当前时间 LocalTime localTime = LocalTime.now(); System.out.println(localTime); //设置为22点1分 localTime = LocalTime.of(22, 1); System.out.println(localTime); //返回一天中的第5503秒 localTime = LocalTime.ofSecondOfDay(5503); System.out.println(localTime); //-------------LocalDateTime-------------- //获取当前日期时间 LocalDateTime dateTime = LocalDateTime.now(); System.out.println(dateTime); //当前日期加上5小时,3分钟 LocalDateTime fu = dateTime.plusHours(5).plusMinutes(3); System.out.println(dateTime+"加上5小时,3分钟之后为:"+fu); //------------关于Year-YearMonth-MonthDay----------- Year year = Year.now(); //获取当前年份 System.out.println(year+"年,在过4年为:"+Year.now().plusYears(4)+"年"); YearMonth yearMonth = YearMonth.now(); //获取月 System.out.println(yearMonth+"月"); MonthDay monthDay = MonthDay.now(); System.out.println(monthDay+"日"); //设置5月23日 System.out.println(monthDay.with(Month.MAY).withDayOfMonth(23));
Java 8时间
猜你喜欢
转载自blog.csdn.net/qq_34095828/article/details/70948776
今日推荐
周排行