当前位置:   article > 正文

LocalDateTime常用时间,带有T和Z的时间处理_localdatetime z

localdatetime z

工作中用到的,记录一下

1、常用时间

  1. package com.zhxd.caochang.qingyan.utils;
  2. import java.time.LocalDate;
  3. import java.time.LocalDateTime;
  4. import java.time.LocalTime;
  5. import java.time.format.DateTimeFormatter;
  6. import java.time.temporal.TemporalAdjusters;
  7. /**
  8. * @Author: hzj
  9. * @Date: 2022/07/19/14:01
  10. */
  11. public class LocalDateTimeUtils {
  12. public static LocalDateTime now = LocalDateTime.now();
  13. public static LocalDate today = LocalDate.now();
  14. public static LocalDateTime todayBegin = LocalDateTime.of(today, LocalTime.MIN);
  15. public static LocalDateTime todayEnd = LocalDateTime.of(today, LocalTime.MAX);
  16. public static LocalDate lastDay = today.minusDays(1);
  17. public static LocalDateTime lastDayBegin = todayBegin.minusDays(1);
  18. public static LocalDateTime lastDayEnd = todayEnd.minusDays(1);
  19. public static LocalDateTime last2DayEnd = todayEnd.minusDays(2);
  20. public static LocalDate lastYearDay = lastDay.minusYears(1);
  21. public static LocalDateTime lastYearDayBegin = todayBegin.minusYears(1);
  22. public static LocalDateTime lastYearDayEnd = todayEnd.minusYears(1);
  23. public static LocalDateTime lastYearLastDayEnd = lastYearDayEnd.minusDays(1);
  24. public static LocalDateTime lastYearNextDayBegin = lastYearDayBegin.plusDays(1);
  25. public static LocalDateTime lastYear2DayBegin = lastYearDayBegin.minusDays(1);
  26. public static LocalDate nextDay = lastDay.plusDays(1);
  27. public static LocalDateTime nextDayBegin = todayBegin.plusDays(1);
  28. public static LocalDateTime nextDayEnd = todayEnd.plusDays(1);
  29. public static LocalDate currentMonth = LocalDate.now();
  30. public static LocalDateTime currentMonthBegin = now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
  31. public static LocalDateTime currentMonthEnd = now.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
  32. public static LocalDateTime nextMonthBegin = currentMonthBegin.plusDays(1);
  33. public static LocalDate lastMonthDay = currentMonth.minusMonths(1);
  34. public static String lastMonthStr = currentMonth.minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
  35. public static LocalDateTime lastMonthBegin = currentMonthBegin.minusMonths(1);
  36. public static LocalDateTime lastMonthEnd = currentMonthEnd.minusMonths(1);
  37. public static LocalDateTime lastMonth2Begin = lastMonthBegin.plusMonths(1);
  38. public static LocalDateTime lastMonth2End = currentMonthEnd.minusMonths(2);
  39. public static int lastYearValue = today.getYear() - 1;
  40. public static LocalDateTime lastYearMonthBegin = currentMonthBegin.minusYears(1);
  41. public static LocalDateTime lastYearMonthEnd = currentMonthEnd.minusYears(1);
  42. public static LocalDateTime lastYearLastMonthEnd = lastYearMonthEnd.minusMonths(1);
  43. public static LocalDateTime lastYearNextMonthBegin = lastYearMonthBegin.plusMonths(1);
  44. public static LocalDateTime currentYearBegin = now.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
  45. public static LocalDateTime currentYearEnd = now.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
  46. public static LocalDateTime lastYearBegin = currentYearBegin.minusYears(1);
  47. public static LocalDateTime lastYearEnd = currentYearEnd.minusYears(1);
  48. public static LocalDateTime nextYearBegin = currentYearBegin.plusYears(1);
  49. public static LocalDateTime last2YearEnd = lastYearEnd.minusYears(1);
  50. }

2、带有T和Z的时间处理

  1. public static void main(String[] args) {
  2. String p = "2022-07-20T15:55:55.999Z";
  3. LocalDateTime z = LocalDateTime.parse(p.replace("Z", "+0000"),
  4. DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
  5. System.out.println(z);
  6. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/572121
推荐阅读
相关标签
  

闽ICP备14008679号