diff --git a/pom.xml b/pom.xml index 118a635..ddf6114 100644 --- a/pom.xml +++ b/pom.xml @@ -17,11 +17,10 @@ 1.11 28.1-jre 1.7.24 - 1.2.60 + 1.2.49 5.0.9.RELEASE 5.0.9.RELEASE 2.6.11 - @@ -116,6 +115,12 @@ ${spring-boot.version} + + + + + + diff --git a/src/main/java/com/irrigation/icl/utils/DateFormat.java b/src/main/java/com/irrigation/icl/utils/DateFormat.java new file mode 100644 index 0000000..9175e7e --- /dev/null +++ b/src/main/java/com/irrigation/icl/utils/DateFormat.java @@ -0,0 +1,75 @@ +package com.irrigation.icl.utils; + +import java.text.SimpleDateFormat; +import java.util.Locale; + +public class DateFormat { + + + public static SimpleDateFormat getInstance(String format) { + return new SimpleDateFormat(format); + } + + public static SimpleDateFormat getInstance(String format, Locale locale) { + return new SimpleDateFormat(format,locale); + } + /** + * 标准日期格式 yyyy-MM-dd + */ + public final static SimpleDateFormat NORM_DATE_FORMAT = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN); + + /** + * 标准时间格式 :HH:mm:ss + */ + public final static SimpleDateFormat NORM_TIME_FORMAT = new SimpleDateFormat(DatePattern.NORM_TIME_PATTERN); + + /** + * 标准日期时间格式,精确到分 :yyyy-MM-dd HH:mm + */ + public final static SimpleDateFormat NORM_DATETIME_MINUTE_FORMAT = new SimpleDateFormat(DatePattern.NORM_DATETIME_MINUTE_PATTERN); + + /** + * 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss + */ + public final static String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; + /** + * 标准日期时间格式,精确到秒 :yyyy-MM-dd HH:mm:ss + */ + public final static SimpleDateFormat NORM_DATETIME_FORMAT = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN); + + + /** + * 标准日期时间格式,精确到毫秒 :yyyy-MM-dd HH:mm:ss.SSS + */ + public final static SimpleDateFormat NORM_DATETIME_MS_FORMAT = new SimpleDateFormat(DatePattern.NORM_DATETIME_MS_PATTERN); + + /** + * 标准日期格式 :yyyy年MM月dd日 + */ + public final static SimpleDateFormat CHINESE_DATE_FORMAT = new SimpleDateFormat(DatePattern.CHINESE_DATE_PATTERN); + + /** + * 标准日期格式 :yyyyMMdd + */ + public final static SimpleDateFormat PURE_DATE_FORMAT = new SimpleDateFormat(DatePattern.PURE_DATE_PATTERN); + + + /** + * 标准日期格式 :HHmmss + */ + public final static SimpleDateFormat PURE_TIME_FORMAT = new SimpleDateFormat(DatePattern.PURE_TIME_PATTERN); + + + /** + * 标准日期格式 :yyyyMMddHHmmss + */ + public final static SimpleDateFormat PURE_DATETIME_FORMAT = new SimpleDateFormat(DatePattern.PURE_DATETIME_PATTERN); + + + /** + * 标准日期格式 :yyyyMMddHHmmssSSS + */ + public final static SimpleDateFormat PURE_DATETIME_MS_FORMAT = new SimpleDateFormat(DatePattern.PURE_DATETIME_MS_PATTERN); + + +} diff --git a/src/main/java/com/irrigation/icl/utils/DatePattern.java b/src/main/java/com/irrigation/icl/utils/DatePattern.java new file mode 100644 index 0000000..4e46843 --- /dev/null +++ b/src/main/java/com/irrigation/icl/utils/DatePattern.java @@ -0,0 +1,102 @@ +package com.irrigation.icl.utils; + +public class DatePattern { + + private DatePattern() { + } + + //-------------------------------------------------------------------------------------------------------------------------------- Normal + /** + * 标准日期格式:yyyy-MM-dd + */ + public final static String NORM_DATE_PATTERN = "yyyy-MM-dd"; + /** + * 标准时间格式:HH:mm:ss + */ + public final static String NORM_TIME_PATTERN = "HH:mm:ss"; + + + /** + * 标准日期时间格式,精确到分:yyyy-MM-dd HH:mm + */ + public final static String NORM_DATETIME_MINUTE_PATTERN = "yyyy-MM-dd HH:mm"; + + /** + * 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss + */ + public final static String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; + + + /** + * 标准日期时间格式,精确到毫秒:yyyy-MM-dd HH:mm:ss.SSS + */ + public final static String NORM_DATETIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS"; + + + /** + * 标准日期格式:yyyy年MM月dd日 + */ + public final static String CHINESE_DATE_PATTERN = "yyyy年MM月dd日"; + + + //-------------------------------------------------------------------------------------------------------------------------------- Pure + /** + * 标准日期格式:yyyyMMdd + */ + public final static String PURE_DATE_PATTERN = "yyyyMMdd"; + + + /** + * 标准日期格式:HHmmss + */ + public final static String PURE_TIME_PATTERN = "HHmmss"; + + + /** + * 标准日期格式:yyyyMMddHHmmss + */ + public final static String PURE_DATETIME_PATTERN = "yyyyMMddHHmmss"; + + + /** + * 标准日期格式:yyyyMMddHHmmssSSS + */ + public final static String PURE_DATETIME_MS_PATTERN = "yyyyMMddHHmmssSSS"; + +// +// //-------------------------------------------------------------------------------------------------------------------------------- Others +// /** +// * HTTP头中日期时间格式:EEE, dd MMM yyyy HH:mm:ss z +// */ +// public final static String HTTP_DATETIME_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z"; +// +// +// /** +// * JDK中日期时间格式:EEE MMM dd HH:mm:ss zzz yyyy +// */ +// public final static String JDK_DATETIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy"; +// +// +// /** +// * UTC时间:yyyy-MM-dd'T'HH:mm:ss'Z' +// */ +// public final static String UTC_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'"; +// +// +// /** +// * UTC时间:yyyy-MM-dd'T'HH:mm:ssZ +// */ +// public final static String UTC_WITH_ZONE_OFFSET_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ"; +// +// +// /** +// * UTC时间:yyyy-MM-dd'T'HH:mm:ss.SSS'Z' +// */ +// public final static String UTC_MS_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; +// +// /** +// * UTC时间:yyyy-MM-dd'T'HH:mm:ssZ +// */ +// public final static String UTC_MS_WITH_ZONE_OFFSET_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; + +} \ No newline at end of file diff --git a/src/main/java/com/irrigation/icl/utils/DateUtils.java b/src/main/java/com/irrigation/icl/utils/DateUtils.java new file mode 100644 index 0000000..5736cc4 --- /dev/null +++ b/src/main/java/com/irrigation/icl/utils/DateUtils.java @@ -0,0 +1,368 @@ +package com.irrigation.icl.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +public class DateUtils { + + + /** + * 当前时间,转换为{@link Date}对象 + * + * @return 当前时间 + */ + public static Date date() { + return new Date(); + } + + + /** + * 创建Calendar对象,时间为默认时区的当前时间 + * + * @return Calendar对象 + * @since 4.6.6 + */ + public static Calendar calendar() { + return Calendar.getInstance(); + } + + /** + * 转换为Calendar对象 + * + * @param date 日期对象 + * @return Calendar对象 + */ + public static Calendar calendar(Date date) { + return calendar(date.getTime()); + } + + /** + * 转换为Calendar对象 + * + * @param millis 时间戳 + * @return Calendar对象 + */ + public static Calendar calendar(long millis) { + final Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(millis); + return cal; + } + + private static Calendar toCalendar(Date date) { + return toCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT), date); + } + + + private static Calendar toCalendar(TimeZone zone, Locale locale, Date date) { + if (null == locale) { + locale = Locale.getDefault(Locale.Category.FORMAT); + } + final Calendar cal = (null != zone) ? Calendar.getInstance(zone, locale) : Calendar.getInstance(locale); + cal.setFirstDayOfWeek(Calendar.MONDAY); + cal.setTime(date); + return cal; + } + + /** + * 当前时间的时间戳 + * + * @return 时间 + */ + public static long currentTimeMillis() { + return System.currentTimeMillis(); + } + + + /** + * 当前时间的时间戳(秒) + * + * @return 当前时间秒数 + */ + public static long currentSeconds() { + return System.currentTimeMillis() / 1000; + } + + /** + * 获得年的部分 + * + * @param date 日期 + * @return 年的部分 + */ + public static int year(Date date) { + return toCalendar(date).get(Calendar.YEAR); + } + + + /** + * 获得指定日期所属季度,从1开始计数 + * + * @param date 日期 + * @return 第几个季度 + * @since 4.1.0 + */ + public static int quarter(Date date) { + return month(date) / 3 + 1; + } + + + /** + * 获得月份,从0开始计数 + * + * @param date 日期 + * @return 月份,从0开始计数 + */ + public static int month(Date date) { + return toCalendar(date).get(Calendar.MONTH); + } + + + /** + * 获得指定日期是所在年份的第几周
+ * + * @param date 日期 + * @return 周 + */ + public static int weekOfYear(Date date) { + return toCalendar(date).get(Calendar.WEEK_OF_YEAR); + } + + /** + * 获得指定日期是所在月份的第几周
+ * + * @param date 日期 + * @return 周 + */ + public static int weekOfMonth(Date date) { + return toCalendar(date).get(Calendar.WEEK_OF_MONTH); + } + + /** + * 获得指定日期是这个日期所在月份的第几天
+ * + * @param date 日期 + * @return 天 + */ + public static int dayOfMonth(Date date) { + return toCalendar(date).get(Calendar.DAY_OF_MONTH); + } + + /** + * 获得指定日期是星期几,1表示周日,2表示周一 + * + * @param date 日期 + * @return 天 + */ + public static int dayOfWeek(Date date) { + return toCalendar(date).get(Calendar.DAY_OF_WEEK); + + } + + /** + * 获得指定日期的小时数部分
+ * + * @param date 日期 + * @return 小时数 + */ + public static int hour(Date date) { + return toCalendar(date).get(Calendar.HOUR_OF_DAY); + } + + /** + * 获得指定日期的分钟数部分
+ * 例如:10:04:15.250 =》 4 + * + * @param date 日期 + * @return 分钟数 + */ + public static int minute(Date date) { + return toCalendar(date).get(Calendar.MINUTE); + + } + + /** + * 获得指定日期的秒数部分
+ * + * @param date 日期 + * @return 秒数 + */ + public static int second(Date date) { + return toCalendar(date).get(Calendar.SECOND); + } + + /** + * 获得指定日期的毫秒数部分
+ * + * @param date 日期 + * @return 毫秒数 + */ + public static int millsecond(Date date) { + return toCalendar(date).get(Calendar.MILLISECOND); + + } + + /** + * 当前时间,格式 yyyy-MM-dd HH:mm:ss + * + * @return 当前时间的标准形式字符串 + */ + public static String now() { + return formatDateTime(new Date()); + } + + /** + * 当前日期,格式 yyyy-MM-dd + * + * @return 当前日期的标准形式字符串 + */ + public static String today() { + return formatDate(new Date()); + } + + // ------------------------------------ Format start ---------------------------------------------- + + + /** + * 根据特定格式格式化日期 + * + * @param date 被格式化的日期 + * @param format 日期格式,常用格式见: + * @return 格式化后的字符串 + */ + public static String format(Date date, String format) { + if (null == date || ObjectUtils.stringIsEmpty(format)) { + return null; + } + return DateFormat.getInstance(format).format(date); + } + + /** + * 格式化日期时间
+ * 格式 yyyy-MM-dd HH:mm:ss + * + * @param date 被格式化的日期 + * @return 格式化后的日期 + */ + public static String formatDateTime(Date date) { + if (null == date) { + return null; + } + return DateFormat.NORM_DATETIME_FORMAT.format(date); + } + + /** + * 格式化日期部分(不包括时间)
+ * 格式 yyyy-MM-dd + * + * @param date 被格式化的日期 + * @return 格式化后的字符串 + */ + public static String formatDate(Date date) { + if (null == date) { + return null; + } + return DateFormat.NORM_DATE_FORMAT.format(date); + } + + /** + * 格式化时间
+ * 格式 HH:mm:ss + * + * @param date 被格式化的日期 + * @return 格式化后的字符串 + * @since 3.0.1 + */ + public static String formatTime(Date date) { + if (null == date) { + return null; + } + return DateFormat.NORM_TIME_FORMAT.format(date); + } + + // ------------------------------------ Format end ---------------------------------------------- + + // ------------------------------------ Parse start ---------------------------------------------- + + + /** + * 构建DateTime对象 + * + * @param dateStr Date字符串 + * @param format 格式 + * @return DateTime对象 + */ + public static Date parse(String dateStr, String format) { + try { + return DateFormat.getInstance(format).parse(dateStr); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 将特定格式的日期转换为Date对象 + * + * @param dateStr 特定格式的日期 + * @param format 格式,例如yyyy-MM-dd + * @param locale 区域信息 + * @return 日期对象 + * @since 4.5.18 + */ + public static Date parse(String dateStr, String format, Locale locale) { + try { + return DateFormat.getInstance(format, locale).parse(dateStr); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 格式yyyy-MM-dd HH:mm:ss + * + * @param dateString 标准形式的时间字符串 + * @return 日期对象 + */ + public static Date parseDateTime(String dateString) { + return parse(dateString, DatePattern.NORM_DATETIME_PATTERN); + } + + /** + * 解析格式为yyyy-MM-dd的日期,忽略时分秒 + * + * @param dateString 标准形式的日期字符串 + * @return 日期对象 + */ + public static Date parseDate(String dateString) { + return parse(dateString, DatePattern.NORM_DATE_PATTERN); + } + + /** + * 解析时间,格式HH:mm:ss,日期部分默认为1970-01-01 + * + * @param timeString 标准形式的日期字符串 + * @return 日期对象 + */ + public static Date parseTime(String timeString) { + return parse(timeString, DatePattern.NORM_TIME_PATTERN); + } + + // ------------------------------------ Parse end ---------------------------------------------- + + /** + * date 转换为 long + * + * @param date + * @return + */ + public static Long dateToLong(Date date) { + return ObjectUtils.isNull(date) ? null : date.getTime(); + + } + + +} \ No newline at end of file diff --git a/src/main/java/com/irrigation/icl/utils/ObjectUtils.java b/src/main/java/com/irrigation/icl/utils/ObjectUtils.java new file mode 100644 index 0000000..03e6eee --- /dev/null +++ b/src/main/java/com/irrigation/icl/utils/ObjectUtils.java @@ -0,0 +1,211 @@ +package com.irrigation.icl.utils; + +import com.google.common.collect.Lists; +import com.irrigation.icl.exception.ContextRuntimeException; +import org.springframework.beans.BeanUtils; + +import java.util.*; + +/** + * 对象工具类 + */ +public class ObjectUtils { + + /** + * 复制对象错误提示 + */ + private final static String COPY_OBJECT_FAIL = "复制对象失败"; + + private ObjectUtils() { + } + + + /** + * 判断对象司是否是空 + * + * @param obj + * @return + */ + public static boolean isNull(Object obj) { + return obj == null; + } + + /** + * 判断对象司是否不是空 + * + * @param obj + * @return + */ + public static boolean nonNull(Object obj) { + return obj != null; + } + + /** + * 判断两个对象是否相等 + * + * @param a + * @param b + * @return + */ + public static boolean equals(Object a, Object b) { + return a != null && a.equals(b); + } + + /** + * 判断集合是否是空 + * + * @param collection + * @return + */ + public static boolean isEmpty(Collection collection) { + return collection == null || collection.isEmpty(); + } + + /** + * 判断集合是否不是空 + * + * @param collection + * @return + */ + public static boolean nonEmpty(Collection collection) { + return !isEmpty(collection); + } + + + /** + * 判断map 是否是空 + * + * @param map + * @return + */ + public static boolean isEmpty(Map map) { + return map == null || map.isEmpty(); + } + + /** + * 判断map 是否不是空 + * + * @param map + * @return + */ + public static boolean nonEmpty(Map map) { + return !isEmpty(map); + } + + + /** + * 判断字符串 是否是 null 或 length=0 + * + * @param str + * @return + */ + public static boolean stringIsEmpty(String str) { + return str == null || str.length() == 0; + } + + + /** + * 判断字符串 是否不是 null 并且 length!=0 + * + * @param str + * @return + */ + public static boolean stringNontEmpty(String str) { + return !stringIsEmpty(str); + } + + + /** + * 去掉字符串前后空白字符 + * + * @param str + * @return + */ + public static String stringTrim(String str) { + return str == null ? null : str.trim(); + } + + + /** + * 单个对象复制 + * + * @param source + * @param targetClass + * @param + * @return + */ + + public static T copyObject(Object source, Class targetClass) { + if (isNull(source)) { + return null; + } + try { + T target = targetClass.newInstance(); + BeanUtils.copyProperties(source, target); + return target; + } catch (Exception e) { + throw new ContextRuntimeException(COPY_OBJECT_FAIL, e); + } + } + + /** + * 集合复制 + * + * @param sourceList + * @param targetClass + * @param + * @return + */ + public static List copyObjectList(List sourceList, Class targetClass) { + List targetList = Lists.newArrayList(); + if (nonEmpty(sourceList)) { + for (S source : sourceList) { + targetList.add(copyObject(source, targetClass)); + } + } + return targetList; + } + + /** + * 集合通过连接符号返回字符串 + * + * @param iterable + * @param separator + * @return + */ + public static String stringJoin(Iterable iterable, String separator) { + return iterable == null ? null : stringJoin(iterable.iterator(), separator); + } + + private static String stringJoin(Iterator iterator, String separator) { + if (iterator == null) { + return null; + } else if (!iterator.hasNext()) { + return ""; + } else { + Object first = iterator.next(); + if (!iterator.hasNext()) { + return Objects.toString(first, ""); + } else { + StringBuilder buf = new StringBuilder(256); + if (first != null) { + buf.append(first); + } + + while (iterator.hasNext()) { + if (separator != null) { + buf.append(separator); + } + + Object obj = iterator.next(); + if (obj != null) { + buf.append(obj); + } + } + return buf.toString(); + } + } + } + + +} diff --git a/src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java b/src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java index 621a3ca..3f42132 100644 --- a/src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java +++ b/src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java @@ -11,26 +11,23 @@ public class RestResultGeneratorUtil { return result; } - public static RestResult getSuccessResult() { + public static RestResult getSuccessResult() { return getResult(ResultEnum.SUCCESS.getCode(), null, ResultEnum.SUCCESS.getMessage()); } + public static RestResult getSuccessResult(T data) { return getResult(ResultEnum.SUCCESS.getCode(), data, ResultEnum.SUCCESS.getMessage()); } - public static RestResult getSuccessResult(T data,String message) { + public static RestResult getSuccessResult(T data, String message) { return getResult(ResultEnum.SUCCESS.getCode(), data, message); } - public static RestResult getSuccessResultMsg(String message) { + public static RestResult getSuccessResultMsg(String message) { return getResult(ResultEnum.SUCCESS.getCode(), null, message); } -// public static RestResult getErrorResult(String message) { -// return getResult(ResultEnum.ERROR.getCode(), null, message); -// } - public static RestResult getErrorResult(String message) { return getResult(ResultEnum.ERROR.getCode(), null, message); }