zoukankan      html  css  js  c++  java
  • jdk 8 日期处理。

    ZoneId id = ZoneId.systemDefault();
    LocalDateTime dateTime = LocalDateTime.now(id);
    System.out.println("时区ID:"+id);
    System.out.println("当前时间:"+dateTime);
    
    LocalDateTime addYear = dateTime.plus(1, ChronoUnit.YEARS);
    System.out.println("当前时间加1年:"+addYear);
    
    LocalDateTime addDay = dateTime.plusDays(56);
    System.out.println("距56天后是几号:"+addDay);
    
    System.out.println("从默认时区的系统获取当前日期时间:"+ZonedDateTime.now());
    String now = "2018-09-01";
    String now1 = "2019-09-01 11:12:23";
    
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    LocalDate nowTime = LocalDate.parse(now,formatter);
    LocalDateTime nowTime1 = LocalDateTime.parse(now1,formatter1);
    
    System.out.println("yyyy-MM-dd 字符串转时间:"+nowTime);
    System.out.println("yyyy-MM-dd HH:mm:ss 字符串转时间:"+nowTime1);
    
    Long now2 = System.currentTimeMillis();
    LocalDateTime date = LocalDateTime.ofEpochSecond(now2/1000,0,ZoneOffset.ofHours(8));
    System.out.println("时间戳转时间:"+date);
    
    LocalDateTime startDateTime = LocalDateTime.of(2018,9,19,23,34,12);
    LocalDateTime endDateTime = LocalDateTime.of(2018,9,15,4,34,12);
    System.out.println("手动构造开始时间:"+startDateTime);
    System.out.println("手动构造结束时间:"+endDateTime);
    Duration duration = Duration.between(endDateTime,startDateTime);
    System.out.printf("时间1,时间2相差:%s天%n",duration.toDays());
    System.out.printf("时间1,时间2相差:%s分钟%n",duration.toMinutes());
    
    LocalDate startDate = LocalDate.of(2018,10,19);
    LocalDate endDate = LocalDate.of(2018,9,15);
    
    Period period = Period.between(endDate,startDate);
    System.out.printf("日期1,日期2相差:%s天%n",period.getDays());
    
    System.out.println("本月有多少天:"+YearMonth.now().lengthOfMonth());
    System.out.println("今年有多少天:"+YearMonth.now().lengthOfYear());








  • 相关阅读:
    【纯水题】POJ 1852 Ants
    【树形DP】BZOJ 1131 Sta
    【不知道怎么分类】HDU
    【树形DP】CF 1293E Xenon's Attack on the Gangs
    【贪心算法】CF Emergency Evacuation
    【思维】UVA 11300 Spreading the Wealth
    【树形DP】NOI2003 逃学的小孩
    【树形DP】BZOJ 3829 Farmcraft
    【树形DP】JSOI BZOJ4472 salesman
    【迷宫问题】CodeForces 1292A A NEKO's Maze Game
  • 原文地址:https://www.cnblogs.com/1-Admin/p/9636836.html
Copyright © 2011-2022 走看看