判断当前时间或者当前时刻是否在某个时间范围内
例如:
public static final DateTimeFormatter YYYYMMDD_PATTERN_FORMARTTER = DateTimeFormatter .ofPattern("yyyyMMdd"); public static final DateTimeFormatter YYYY_MM_DD_PATTERN_FORMARTTER = DateTimeFormatter .ofPattern("yyyy_MM_dd"); public static final DateTimeFormatter YYYYMMDDHHMMSS_PATTERN_FORMARTTER = DateTimeFormatter .ofPattern("yyyyMMddHHmmss"); public static final DateTimeFormatter YYYY_MM_DD_HH_MM_SS_PATTERN_FORMARTTER = DateTimeFormatter .ofPattern("yyyy_MM_dd HH:mm:ss"); String start = "20211101"; String end = "20211108"; LocalDate startDate = LocalDate.parse(getDateString(start),YYYY_MM_DD_PATTERN_FORMARTTER ); LocalDate endDate = LocalDate.parse(getDateString(end ),YYYY_MM_DD_PATTERN_FORMARTTER ); LocalDate nowDate = LocalDate.now(); String begin = ""; if(endDate .isBefore(nowDate ) || startDate .isAfter(nowDate )){ begin = "false"; }else{ begin = "on"; } String startTime = "20211101100000"; String endTime = "20211108100000"; LocalDateTime startDateTime = LocalDateTime.parse(getDateTimeString(startTime ),YYYY_MM_DD_HH_MM_SS_PATTERN_FORMARTTER ); LocalDateTime endDateTime = LocalDateTime.parse(getDateTimeString(endTime ),YYYY_MM_DD_HH_MM_SS_PATTERN_FORMARTTER ); LocalDateTime nowDateTime = LocalDateTime.now(); String begin1 = ""; if(endDateTime .isBefore(nowDateTime) || startDateTime .isAfter(nowDateTime )){ begin1 = "false"; }else{ begin1 = "on"; } //将“yyyyMMdd”格式字符串转换成“yyyy_MM_dd”格式字符串 private String getDateString(String str){ LocalDate parse = **LocalDate**.parse(str, YYYYMMDD_PATTERN_FORMARTTER ); String format = parse.format (YYYY_MM_DD_PATTERN_FORMARTTER); return format ; } //将“yyyyMMddHHmmss”格式字符串转换成“yyyy_MM_dd HH:mm:ss”格式字符串 private String getDateTimeString(String str){ LocalDate parse = **LocalDateTime**.parse(str, YYYYMMDDHHMMSS_PATTERN_FORMARTTER ); String format = parse.format(YYYY_MM_DD_HH_MM_SS_PATTERN_FORMARTTER ); return format ; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)