LocalDateTime基本使用

时间初始化


   
   
   
   
  1. //获取当前时间
  2. LocalDateTime nowTime= LocalDateTime.now();
  3. System.out.println( "现在时间"+nowTime);
  4. //自定义时间 of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
  5. LocalDateTime endTime = LocalDateTime.of( 2020, 5, 20, 5, 20, 10, 00);
  6. System.out.println( "设定时间"+endTime);

时间比较


   
   
   
   
  1. //比较 现在的时间 比 设定的时间 之前 返回的类型是Boolean类型
  2. boolean isBefore = nowTime.isBefore(endTime);
  3. System.out.println( "现在的时间 比 设定的时间 之前"+isBefore);
  4. //比较 现在的时间 和 设定的时候 相等 返回类型是Boolean类型
  5. boolean euqal =nowTime.equals(endTime);
  6. System.out.println( "现在的时间 和 设定的时候 相等"+euqal);
  7. //比较 现在的时间 比 设定的时间 之后 返回的类型是Boolean类型
  8. boolean isAfter = nowTime.isAfter(endTime);
  9. System.out.println( "现在的时间 比 设定的时间 之后"+isAfter);

 时间获取


   
   
   
   
  1. // 获取当前时间
  2. int year = nowTime.getYear();
  3. int monthValue = nowTime.getMonthValue();
  4. int dayofMonth = nowTime.getDayOfMonth();
  5. int hour = nowTime.getHour();
  6. int minute = nowTime.getMinute();
  7. int second = nowTime.getSecond();
  8. System.out.println( "获得时间:" + year + "年" +monthValue+ "月"+dayofMonth+ "日" + hour+ "时"+minute+ "分"+second+ "秒"); // 打印当前时间的
  9. DayOfWeek dayofWeek = nowTime.getDayOfWeek(); //获取星期几
  10. System.out.println( "获得星期:"+dayofWeek);
  11. int dayofYear = nowTime.getDayOfYear(); //获取当前日子为年的第几天
  12. System.out.println( "全年:"+dayofYear+ "天");

时间操作


   
   
   
   
  1. // 获取一年之前.minusYears(1) 这个 1 代表一年之前,如果是2就是两年之前
  2. LocalDateTime minusYears = nowTime.minusYears( 1);
  3. System.out.println(minusYears);
  4. LocalDateTime plusYears = nowTime.plusYears( 1); //一年之后
  5. System.out.println(plusYears);
  6. LocalDateTime minusMonths = nowTime.minusMonths( 1); //一个月前
  7. System.out.println(minusMonths);
  8. LocalDateTime plusMonths = nowTime.plusMonths( 1); //一个月后
  9. System.out.println(plusMonths);
  10. LocalDateTime BeforeWeek = nowTime.minusWeeks( 1); //一个星期前
  11. System.out.println(BeforeWeek);
  12. LocalDateTime AfterWeek = nowTime.plusWeeks( 1); //一个星期后
  13. System.out.println(AfterWeek);
  14. LocalDateTime minusDays = nowTime.minusDays( 1); //一天前
  15. System.out.println(minusDays);
  16. LocalDateTime plusDays = nowTime.plusDays( 1); //一天后
  17. System.out.println(plusDays);
  18. LocalDateTime minusHours = nowTime.minusHours( 1); //一个小时前
  19. System.out.println(minusHours);
  20. LocalDateTime plusHours = nowTime.plusHours( 1); //一个小时后
  21. System.out.println(plusHours);
  22. LocalDateTime minusMinutes = nowTime.minusMinutes( 1); //一分钟前
  23. System.out.println(minusMinutes);
  24. LocalDateTime plusMinutes = nowTime.plusMinutes( 1); //一分钟后
  25. System.out.println(plusMinutes);
  26. LocalDateTime minusSeconds = nowTime.minusSeconds( 1); //一秒前
  27. System.out.println(minusSeconds);
  28. LocalDateTime plusSeconds = nowTime.plusSeconds( 1); //一秒后
  29. System.out.println(plusSeconds);
  30. LocalDateTime minusTime = nowTime.minus( 23, ChronoUnit.MONTHS); //当前时间减少23个月
  31. System.out.println(minusTime);

时间做差


   
   
   
   
  1. //计算时间差
  2. Duration duration = Duration.between(nowTime,endTime); //时间差
  3. long durationdays = duration.toDays(); //相差的天数
  4. System.out.println( "duration相差"+durationdays+ "天");
  5. long durationhours = duration.toHours(); //相差的小时数
  6. System.out.println( "duration相差"+durationhours+ "小时");
  7. long durationminutes = duration.toMinutes(); //相差的分钟数
  8. System.out.println( "duration相差"+durationminutes+ "分钟");
  9. long durationmillis = duration.toMillis(); //相差毫秒数
  10. System.out.println( "duration相差"+durationmillis+ "毫秒");
  11. long durationnanos = duration.toNanos(); //相差的纳秒数
  12. System.out.println( "duration相差"+durationnanos+ "纳秒");

Period时间差 


   
   
   
   
  1. Period period = Period.between(nowTime.toLocalDate(),endTime.toLocalDate());
  2. int periodyear = period.getYears();
  3. System.out.println( "period相差"+periodyear+ "年");
  4. int periodmonth = period.getMonths();
  5. System.out.println( "period相差"+periodmonth+ "月");
  6. int perioddays= period.getDays();
  7. System.out.println( "period相差"+perioddays+ "日");

获得特殊时间


   
   
   
   
  1. //根据需求需要取得当天的零点
  2. LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); //当天零点
  3. System.out.println(today_start);
  4. LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX); //获取当天结束时间
  5. System.out.println(today_end);

LocalDataTime转化


   
   
   
   
  1. //LocalDataTIme的转化
  2. String localDataTimeString= nowTime.format(DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss"));
  3. LocalDateTime StringLocalDataTime = LocalDateTime.parse( "2017-09-28 17:07:05",DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss")); //String转化为LocalDataTime
  4. System.out.println( "LocalDateTime转成String类型的时间:"+localDataTimeString);
  5. System.out.println( "String类型的时间转成LocalDateTime:"+StringLocalDataTime);

Data转化


   
   
   
   
  1. //Date的转化
  2. Date date = Date.from(nowTime.atZone(ZoneId.systemDefault()).toInstant());
  3. LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); //转化为LocalDataTime
  4. System.out.println(dateTime);
  5. String dataString = nowTime.format(DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss")); //转为为String
  6. System.out.println(dataString);

你可能感兴趣的:(基础)