日期区间

  1. import com.example.demo.dto.QueryRangTimeDto;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.time.LocalDate;
  4. import java.time.format.DateTimeFormatter;
  5. /**
  6. * @author HUAWEI
  7. */
  8. @Slf4j
  9. public class SumTestController {
  10. private final static String MIN_TIME = "2021-07-01";
  11. public static void main(String[] args) {
  12. QueryRangTimeDto condition = new QueryRangTimeDto("2021-09-01", "2021-09-20");
  13. LocalDate startTime = LocalDate.parse(condition.getStartMonth(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  14. LocalDate endTime = LocalDate.parse(condition.getEndMonth(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  15. if (!startTime.isBefore(endTime)) {
  16. if (!startTime.isEqual(endTime)) {
  17. log.info("请输入正常日期");
  18. return;
  19. }
  20. }
  21. boolean begin = getBelongDate(startTime);
  22. boolean end = getBelongDate(endTime);
  23. if (begin && end) {
  24. log.info("正常查询");
  25. return;
  26. }
  27. log.info("请输入正常日期");
  28. }
  29. /**
  30. * 判断时间是否在某段时间内
  31. *
  32. * @return 比较结果
  33. */
  34. public static boolean getBelongDate(LocalDate now) {
  35. //时间当前年
  36. int year = LocalDate.now().getYear();
  37. //自定义开始时间
  38. LocalDate begin = LocalDate.of(year, 7, 1);
  39. //自定义结束时间
  40. LocalDate end = LocalDate.of(year, 9, 20);
  41. return now.isAfter(begin) && now.isBefore(end);
  42. }
  43. }