| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package com.cyzh.repeater.utils;
- import cn.hutool.core.convert.Convert;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.StrUtil;
- import org.apache.log4j.Logger;
- import java.util.HashMap;
- import java.util.Map;
- public class DateUti {
- private static final Logger logger = Logger.getLogger(DateUti.class);
- /**
- * 比较开始时间和结束时间
- * @param sDate
- * @param eDate
- * @return
- */
- public static Map<String, Object> dateCompare(String sDate, String eDate){
- Map<String, Object> map = new HashMap<>();
- String date = formatDate(eDate);
- if(StrUtil.isBlank(date)){
- map.put("isOk", false);
- map.put("errMsg", "结束时间格式有误");
- return map;
- }
- if(isBigCurrentDate(date)){
- map.put("isOk", false);
- map.put("errMsg", "结束时间要大于当前时间");
- return map;
- }
- String keyStartDate = formatDate(sDate);
- if(StrUtil.isBlank(keyStartDate)){
- map.put("isOk", false);
- map.put("errMsg", "开始时间格式有误");
- return map;
- }
- if(Convert.toLong(keyStartDate) >= Convert.toLong(date)){
- map.put("isOk", false);
- map.put("errMsg", "结束时间要大于开始时间");
- return map;
- }
- map.put("isOk", true);
- map.put("errMsg", "");
- map.put("startDate", keyStartDate);
- map.put("endDate", date);
- return map;
- }
- /**
- * 格式化日期,格式:yyyyMMddHHmmss
- * @param date
- * @return
- */
- public static String formatDate(String date){
- try {
- return DateUtil.format(DateUtil.parse(date), "yyyyMMddHHmmss");
- }catch (Exception e){
- logger.error("格式化yyyyMMddHHmmss异常:", e);
- return null;
- }
- }
- /**
- * 日期是否大于当前时间
- * @param date 比较的时间
- * @return true:大于,false 小于
- */
- public static boolean isBigCurrentDate(String date){
- String currentDate = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");
- date = DateUtil.format(DateUtil.parse(date), "yyyyMMddHHmmss");
- if(Convert.toLong(currentDate) > Convert.toLong(date)){
- return true;
- }
- return false;
- }
- public static void main(String[] args) {
- dateCompare("2017-04-01 23:33:23", "2021-04-01 23:33:23");
- }
- }
|