Commit 113e618041f3ed4e59e0c19cc873df0903482162
1 parent
53902648
feat #添加时间、对象工具类
Showing
6 changed files
with
767 additions
and
9 deletions
pom.xml
| ... | ... | @@ -17,11 +17,10 @@ |
| 17 | 17 | <commons-codec.version>1.11</commons-codec.version> |
| 18 | 18 | <guava.vsersion>28.1-jre</guava.vsersion> |
| 19 | 19 | <slf4j.version>1.7.24</slf4j.version> |
| 20 | - <fastjson.version>1.2.60</fastjson.version> | |
| 20 | + <fastjson.version>1.2.49</fastjson.version> | |
| 21 | 21 | <spring-web.version>5.0.9.RELEASE</spring-web.version> |
| 22 | 22 | <spring-security-core.version>5.0.9.RELEASE</spring-security-core.version> |
| 23 | 23 | <ehcache.version>2.6.11</ehcache.version> |
| 24 | - | |
| 25 | 24 | </properties> |
| 26 | 25 | |
| 27 | 26 | |
| ... | ... | @@ -116,6 +115,12 @@ |
| 116 | 115 | <version>${spring-boot.version}</version> |
| 117 | 116 | <!--<scope>provided</scope>--> |
| 118 | 117 | </dependency> |
| 118 | + <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> | |
| 119 | +<!-- <dependency>--> | |
| 120 | +<!-- <groupId>org.apache.commons</groupId>--> | |
| 121 | +<!-- <artifactId>commons-lang3</artifactId>--> | |
| 122 | +<!-- <version>3.9</version>--> | |
| 123 | +<!-- </dependency>--> | |
| 119 | 124 | |
| 120 | 125 | </dependencies> |
| 121 | 126 | <build> | ... | ... |
src/main/java/com/irrigation/icl/utils/DateFormat.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | |
| 2 | + | |
| 3 | +import java.text.SimpleDateFormat; | |
| 4 | +import java.util.Locale; | |
| 5 | + | |
| 6 | +public class DateFormat { | |
| 7 | + | |
| 8 | + | |
| 9 | + public static SimpleDateFormat getInstance(String format) { | |
| 10 | + return new SimpleDateFormat(format); | |
| 11 | + } | |
| 12 | + | |
| 13 | + public static SimpleDateFormat getInstance(String format, Locale locale) { | |
| 14 | + return new SimpleDateFormat(format,locale); | |
| 15 | + } | |
| 16 | + /** | |
| 17 | + * 标准日期格式 yyyy-MM-dd | |
| 18 | + */ | |
| 19 | + public final static SimpleDateFormat NORM_DATE_FORMAT = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN); | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * 标准时间格式 :HH:mm:ss | |
| 23 | + */ | |
| 24 | + public final static SimpleDateFormat NORM_TIME_FORMAT = new SimpleDateFormat(DatePattern.NORM_TIME_PATTERN); | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * 标准日期时间格式,精确到分 :yyyy-MM-dd HH:mm | |
| 28 | + */ | |
| 29 | + public final static SimpleDateFormat NORM_DATETIME_MINUTE_FORMAT = new SimpleDateFormat(DatePattern.NORM_DATETIME_MINUTE_PATTERN); | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss | |
| 33 | + */ | |
| 34 | + public final static String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; | |
| 35 | + /** | |
| 36 | + * 标准日期时间格式,精确到秒 :yyyy-MM-dd HH:mm:ss | |
| 37 | + */ | |
| 38 | + public final static SimpleDateFormat NORM_DATETIME_FORMAT = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN); | |
| 39 | + | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * 标准日期时间格式,精确到毫秒 :yyyy-MM-dd HH:mm:ss.SSS | |
| 43 | + */ | |
| 44 | + public final static SimpleDateFormat NORM_DATETIME_MS_FORMAT = new SimpleDateFormat(DatePattern.NORM_DATETIME_MS_PATTERN); | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * 标准日期格式 :yyyy年MM月dd日 | |
| 48 | + */ | |
| 49 | + public final static SimpleDateFormat CHINESE_DATE_FORMAT = new SimpleDateFormat(DatePattern.CHINESE_DATE_PATTERN); | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * 标准日期格式 :yyyyMMdd | |
| 53 | + */ | |
| 54 | + public final static SimpleDateFormat PURE_DATE_FORMAT = new SimpleDateFormat(DatePattern.PURE_DATE_PATTERN); | |
| 55 | + | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * 标准日期格式 :HHmmss | |
| 59 | + */ | |
| 60 | + public final static SimpleDateFormat PURE_TIME_FORMAT = new SimpleDateFormat(DatePattern.PURE_TIME_PATTERN); | |
| 61 | + | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * 标准日期格式 :yyyyMMddHHmmss | |
| 65 | + */ | |
| 66 | + public final static SimpleDateFormat PURE_DATETIME_FORMAT = new SimpleDateFormat(DatePattern.PURE_DATETIME_PATTERN); | |
| 67 | + | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * 标准日期格式 :yyyyMMddHHmmssSSS | |
| 71 | + */ | |
| 72 | + public final static SimpleDateFormat PURE_DATETIME_MS_FORMAT = new SimpleDateFormat(DatePattern.PURE_DATETIME_MS_PATTERN); | |
| 73 | + | |
| 74 | + | |
| 75 | +} | ... | ... |
src/main/java/com/irrigation/icl/utils/DatePattern.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | |
| 2 | + | |
| 3 | +public class DatePattern { | |
| 4 | + | |
| 5 | + private DatePattern() { | |
| 6 | + } | |
| 7 | + | |
| 8 | + //-------------------------------------------------------------------------------------------------------------------------------- Normal | |
| 9 | + /** | |
| 10 | + * 标准日期格式:yyyy-MM-dd | |
| 11 | + */ | |
| 12 | + public final static String NORM_DATE_PATTERN = "yyyy-MM-dd"; | |
| 13 | + /** | |
| 14 | + * 标准时间格式:HH:mm:ss | |
| 15 | + */ | |
| 16 | + public final static String NORM_TIME_PATTERN = "HH:mm:ss"; | |
| 17 | + | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * 标准日期时间格式,精确到分:yyyy-MM-dd HH:mm | |
| 21 | + */ | |
| 22 | + public final static String NORM_DATETIME_MINUTE_PATTERN = "yyyy-MM-dd HH:mm"; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss | |
| 26 | + */ | |
| 27 | + public final static String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; | |
| 28 | + | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * 标准日期时间格式,精确到毫秒:yyyy-MM-dd HH:mm:ss.SSS | |
| 32 | + */ | |
| 33 | + public final static String NORM_DATETIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS"; | |
| 34 | + | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * 标准日期格式:yyyy年MM月dd日 | |
| 38 | + */ | |
| 39 | + public final static String CHINESE_DATE_PATTERN = "yyyy年MM月dd日"; | |
| 40 | + | |
| 41 | + | |
| 42 | + //-------------------------------------------------------------------------------------------------------------------------------- Pure | |
| 43 | + /** | |
| 44 | + * 标准日期格式:yyyyMMdd | |
| 45 | + */ | |
| 46 | + public final static String PURE_DATE_PATTERN = "yyyyMMdd"; | |
| 47 | + | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * 标准日期格式:HHmmss | |
| 51 | + */ | |
| 52 | + public final static String PURE_TIME_PATTERN = "HHmmss"; | |
| 53 | + | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * 标准日期格式:yyyyMMddHHmmss | |
| 57 | + */ | |
| 58 | + public final static String PURE_DATETIME_PATTERN = "yyyyMMddHHmmss"; | |
| 59 | + | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * 标准日期格式:yyyyMMddHHmmssSSS | |
| 63 | + */ | |
| 64 | + public final static String PURE_DATETIME_MS_PATTERN = "yyyyMMddHHmmssSSS"; | |
| 65 | + | |
| 66 | +// | |
| 67 | +// //-------------------------------------------------------------------------------------------------------------------------------- Others | |
| 68 | +// /** | |
| 69 | +// * HTTP头中日期时间格式:EEE, dd MMM yyyy HH:mm:ss z | |
| 70 | +// */ | |
| 71 | +// public final static String HTTP_DATETIME_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z"; | |
| 72 | +// | |
| 73 | +// | |
| 74 | +// /** | |
| 75 | +// * JDK中日期时间格式:EEE MMM dd HH:mm:ss zzz yyyy | |
| 76 | +// */ | |
| 77 | +// public final static String JDK_DATETIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy"; | |
| 78 | +// | |
| 79 | +// | |
| 80 | +// /** | |
| 81 | +// * UTC时间:yyyy-MM-dd'T'HH:mm:ss'Z' | |
| 82 | +// */ | |
| 83 | +// public final static String UTC_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'"; | |
| 84 | +// | |
| 85 | +// | |
| 86 | +// /** | |
| 87 | +// * UTC时间:yyyy-MM-dd'T'HH:mm:ssZ | |
| 88 | +// */ | |
| 89 | +// public final static String UTC_WITH_ZONE_OFFSET_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ"; | |
| 90 | +// | |
| 91 | +// | |
| 92 | +// /** | |
| 93 | +// * UTC时间:yyyy-MM-dd'T'HH:mm:ss.SSS'Z' | |
| 94 | +// */ | |
| 95 | +// public final static String UTC_MS_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; | |
| 96 | +// | |
| 97 | +// /** | |
| 98 | +// * UTC时间:yyyy-MM-dd'T'HH:mm:ssZ | |
| 99 | +// */ | |
| 100 | +// public final static String UTC_MS_WITH_ZONE_OFFSET_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; | |
| 101 | + | |
| 102 | +} | |
| 0 | 103 | \ No newline at end of file | ... | ... |
src/main/java/com/irrigation/icl/utils/DateUtils.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | |
| 2 | + | |
| 3 | +import java.text.ParseException; | |
| 4 | +import java.text.SimpleDateFormat; | |
| 5 | +import java.util.Calendar; | |
| 6 | +import java.util.Date; | |
| 7 | +import java.util.Locale; | |
| 8 | +import java.util.TimeZone; | |
| 9 | + | |
| 10 | +public class DateUtils { | |
| 11 | + | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * 当前时间,转换为{@link Date}对象 | |
| 15 | + * | |
| 16 | + * @return 当前时间 | |
| 17 | + */ | |
| 18 | + public static Date date() { | |
| 19 | + return new Date(); | |
| 20 | + } | |
| 21 | + | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * 创建Calendar对象,时间为默认时区的当前时间 | |
| 25 | + * | |
| 26 | + * @return Calendar对象 | |
| 27 | + * @since 4.6.6 | |
| 28 | + */ | |
| 29 | + public static Calendar calendar() { | |
| 30 | + return Calendar.getInstance(); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * 转换为Calendar对象 | |
| 35 | + * | |
| 36 | + * @param date 日期对象 | |
| 37 | + * @return Calendar对象 | |
| 38 | + */ | |
| 39 | + public static Calendar calendar(Date date) { | |
| 40 | + return calendar(date.getTime()); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * 转换为Calendar对象 | |
| 45 | + * | |
| 46 | + * @param millis 时间戳 | |
| 47 | + * @return Calendar对象 | |
| 48 | + */ | |
| 49 | + public static Calendar calendar(long millis) { | |
| 50 | + final Calendar cal = Calendar.getInstance(); | |
| 51 | + cal.setTimeInMillis(millis); | |
| 52 | + return cal; | |
| 53 | + } | |
| 54 | + | |
| 55 | + private static Calendar toCalendar(Date date) { | |
| 56 | + return toCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT), date); | |
| 57 | + } | |
| 58 | + | |
| 59 | + | |
| 60 | + private static Calendar toCalendar(TimeZone zone, Locale locale, Date date) { | |
| 61 | + if (null == locale) { | |
| 62 | + locale = Locale.getDefault(Locale.Category.FORMAT); | |
| 63 | + } | |
| 64 | + final Calendar cal = (null != zone) ? Calendar.getInstance(zone, locale) : Calendar.getInstance(locale); | |
| 65 | + cal.setFirstDayOfWeek(Calendar.MONDAY); | |
| 66 | + cal.setTime(date); | |
| 67 | + return cal; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * 当前时间的时间戳 | |
| 72 | + * | |
| 73 | + * @return 时间 | |
| 74 | + */ | |
| 75 | + public static long currentTimeMillis() { | |
| 76 | + return System.currentTimeMillis(); | |
| 77 | + } | |
| 78 | + | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * 当前时间的时间戳(秒) | |
| 82 | + * | |
| 83 | + * @return 当前时间秒数 | |
| 84 | + */ | |
| 85 | + public static long currentSeconds() { | |
| 86 | + return System.currentTimeMillis() / 1000; | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * 获得年的部分 | |
| 91 | + * | |
| 92 | + * @param date 日期 | |
| 93 | + * @return 年的部分 | |
| 94 | + */ | |
| 95 | + public static int year(Date date) { | |
| 96 | + return toCalendar(date).get(Calendar.YEAR); | |
| 97 | + } | |
| 98 | + | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * 获得指定日期所属季度,从1开始计数 | |
| 102 | + * | |
| 103 | + * @param date 日期 | |
| 104 | + * @return 第几个季度 | |
| 105 | + * @since 4.1.0 | |
| 106 | + */ | |
| 107 | + public static int quarter(Date date) { | |
| 108 | + return month(date) / 3 + 1; | |
| 109 | + } | |
| 110 | + | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * 获得月份,从0开始计数 | |
| 114 | + * | |
| 115 | + * @param date 日期 | |
| 116 | + * @return 月份,从0开始计数 | |
| 117 | + */ | |
| 118 | + public static int month(Date date) { | |
| 119 | + return toCalendar(date).get(Calendar.MONTH); | |
| 120 | + } | |
| 121 | + | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * 获得指定日期是所在年份的第几周<br> | |
| 125 | + * | |
| 126 | + * @param date 日期 | |
| 127 | + * @return 周 | |
| 128 | + */ | |
| 129 | + public static int weekOfYear(Date date) { | |
| 130 | + return toCalendar(date).get(Calendar.WEEK_OF_YEAR); | |
| 131 | + } | |
| 132 | + | |
| 133 | + /** | |
| 134 | + * 获得指定日期是所在月份的第几周<br> | |
| 135 | + * | |
| 136 | + * @param date 日期 | |
| 137 | + * @return 周 | |
| 138 | + */ | |
| 139 | + public static int weekOfMonth(Date date) { | |
| 140 | + return toCalendar(date).get(Calendar.WEEK_OF_MONTH); | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * 获得指定日期是这个日期所在月份的第几天<br> | |
| 145 | + * | |
| 146 | + * @param date 日期 | |
| 147 | + * @return 天 | |
| 148 | + */ | |
| 149 | + public static int dayOfMonth(Date date) { | |
| 150 | + return toCalendar(date).get(Calendar.DAY_OF_MONTH); | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * 获得指定日期是星期几,1表示周日,2表示周一 | |
| 155 | + * | |
| 156 | + * @param date 日期 | |
| 157 | + * @return 天 | |
| 158 | + */ | |
| 159 | + public static int dayOfWeek(Date date) { | |
| 160 | + return toCalendar(date).get(Calendar.DAY_OF_WEEK); | |
| 161 | + | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * 获得指定日期的小时数部分<br> | |
| 166 | + * | |
| 167 | + * @param date 日期 | |
| 168 | + * @return 小时数 | |
| 169 | + */ | |
| 170 | + public static int hour(Date date) { | |
| 171 | + return toCalendar(date).get(Calendar.HOUR_OF_DAY); | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * 获得指定日期的分钟数部分<br> | |
| 176 | + * 例如:10:04:15.250 =》 4 | |
| 177 | + * | |
| 178 | + * @param date 日期 | |
| 179 | + * @return 分钟数 | |
| 180 | + */ | |
| 181 | + public static int minute(Date date) { | |
| 182 | + return toCalendar(date).get(Calendar.MINUTE); | |
| 183 | + | |
| 184 | + } | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * 获得指定日期的秒数部分<br> | |
| 188 | + * | |
| 189 | + * @param date 日期 | |
| 190 | + * @return 秒数 | |
| 191 | + */ | |
| 192 | + public static int second(Date date) { | |
| 193 | + return toCalendar(date).get(Calendar.SECOND); | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * 获得指定日期的毫秒数部分<br> | |
| 198 | + * | |
| 199 | + * @param date 日期 | |
| 200 | + * @return 毫秒数 | |
| 201 | + */ | |
| 202 | + public static int millsecond(Date date) { | |
| 203 | + return toCalendar(date).get(Calendar.MILLISECOND); | |
| 204 | + | |
| 205 | + } | |
| 206 | + | |
| 207 | + /** | |
| 208 | + * 当前时间,格式 yyyy-MM-dd HH:mm:ss | |
| 209 | + * | |
| 210 | + * @return 当前时间的标准形式字符串 | |
| 211 | + */ | |
| 212 | + public static String now() { | |
| 213 | + return formatDateTime(new Date()); | |
| 214 | + } | |
| 215 | + | |
| 216 | + /** | |
| 217 | + * 当前日期,格式 yyyy-MM-dd | |
| 218 | + * | |
| 219 | + * @return 当前日期的标准形式字符串 | |
| 220 | + */ | |
| 221 | + public static String today() { | |
| 222 | + return formatDate(new Date()); | |
| 223 | + } | |
| 224 | + | |
| 225 | + // ------------------------------------ Format start ---------------------------------------------- | |
| 226 | + | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * 根据特定格式格式化日期 | |
| 230 | + * | |
| 231 | + * @param date 被格式化的日期 | |
| 232 | + * @param format 日期格式,常用格式见: | |
| 233 | + * @return 格式化后的字符串 | |
| 234 | + */ | |
| 235 | + public static String format(Date date, String format) { | |
| 236 | + if (null == date || ObjectUtils.stringIsEmpty(format)) { | |
| 237 | + return null; | |
| 238 | + } | |
| 239 | + return DateFormat.getInstance(format).format(date); | |
| 240 | + } | |
| 241 | + | |
| 242 | + /** | |
| 243 | + * 格式化日期时间<br> | |
| 244 | + * 格式 yyyy-MM-dd HH:mm:ss | |
| 245 | + * | |
| 246 | + * @param date 被格式化的日期 | |
| 247 | + * @return 格式化后的日期 | |
| 248 | + */ | |
| 249 | + public static String formatDateTime(Date date) { | |
| 250 | + if (null == date) { | |
| 251 | + return null; | |
| 252 | + } | |
| 253 | + return DateFormat.NORM_DATETIME_FORMAT.format(date); | |
| 254 | + } | |
| 255 | + | |
| 256 | + /** | |
| 257 | + * 格式化日期部分(不包括时间)<br> | |
| 258 | + * 格式 yyyy-MM-dd | |
| 259 | + * | |
| 260 | + * @param date 被格式化的日期 | |
| 261 | + * @return 格式化后的字符串 | |
| 262 | + */ | |
| 263 | + public static String formatDate(Date date) { | |
| 264 | + if (null == date) { | |
| 265 | + return null; | |
| 266 | + } | |
| 267 | + return DateFormat.NORM_DATE_FORMAT.format(date); | |
| 268 | + } | |
| 269 | + | |
| 270 | + /** | |
| 271 | + * 格式化时间<br> | |
| 272 | + * 格式 HH:mm:ss | |
| 273 | + * | |
| 274 | + * @param date 被格式化的日期 | |
| 275 | + * @return 格式化后的字符串 | |
| 276 | + * @since 3.0.1 | |
| 277 | + */ | |
| 278 | + public static String formatTime(Date date) { | |
| 279 | + if (null == date) { | |
| 280 | + return null; | |
| 281 | + } | |
| 282 | + return DateFormat.NORM_TIME_FORMAT.format(date); | |
| 283 | + } | |
| 284 | + | |
| 285 | + // ------------------------------------ Format end ---------------------------------------------- | |
| 286 | + | |
| 287 | + // ------------------------------------ Parse start ---------------------------------------------- | |
| 288 | + | |
| 289 | + | |
| 290 | + /** | |
| 291 | + * 构建DateTime对象 | |
| 292 | + * | |
| 293 | + * @param dateStr Date字符串 | |
| 294 | + * @param format 格式 | |
| 295 | + * @return DateTime对象 | |
| 296 | + */ | |
| 297 | + public static Date parse(String dateStr, String format) { | |
| 298 | + try { | |
| 299 | + return DateFormat.getInstance(format).parse(dateStr); | |
| 300 | + } catch (ParseException e) { | |
| 301 | + e.printStackTrace(); | |
| 302 | + } | |
| 303 | + return null; | |
| 304 | + } | |
| 305 | + | |
| 306 | + /** | |
| 307 | + * 将特定格式的日期转换为Date对象 | |
| 308 | + * | |
| 309 | + * @param dateStr 特定格式的日期 | |
| 310 | + * @param format 格式,例如yyyy-MM-dd | |
| 311 | + * @param locale 区域信息 | |
| 312 | + * @return 日期对象 | |
| 313 | + * @since 4.5.18 | |
| 314 | + */ | |
| 315 | + public static Date parse(String dateStr, String format, Locale locale) { | |
| 316 | + try { | |
| 317 | + return DateFormat.getInstance(format, locale).parse(dateStr); | |
| 318 | + } catch (ParseException e) { | |
| 319 | + e.printStackTrace(); | |
| 320 | + } | |
| 321 | + return null; | |
| 322 | + } | |
| 323 | + | |
| 324 | + /** | |
| 325 | + * 格式yyyy-MM-dd HH:mm:ss | |
| 326 | + * | |
| 327 | + * @param dateString 标准形式的时间字符串 | |
| 328 | + * @return 日期对象 | |
| 329 | + */ | |
| 330 | + public static Date parseDateTime(String dateString) { | |
| 331 | + return parse(dateString, DatePattern.NORM_DATETIME_PATTERN); | |
| 332 | + } | |
| 333 | + | |
| 334 | + /** | |
| 335 | + * 解析格式为yyyy-MM-dd的日期,忽略时分秒 | |
| 336 | + * | |
| 337 | + * @param dateString 标准形式的日期字符串 | |
| 338 | + * @return 日期对象 | |
| 339 | + */ | |
| 340 | + public static Date parseDate(String dateString) { | |
| 341 | + return parse(dateString, DatePattern.NORM_DATE_PATTERN); | |
| 342 | + } | |
| 343 | + | |
| 344 | + /** | |
| 345 | + * 解析时间,格式HH:mm:ss,日期部分默认为1970-01-01 | |
| 346 | + * | |
| 347 | + * @param timeString 标准形式的日期字符串 | |
| 348 | + * @return 日期对象 | |
| 349 | + */ | |
| 350 | + public static Date parseTime(String timeString) { | |
| 351 | + return parse(timeString, DatePattern.NORM_TIME_PATTERN); | |
| 352 | + } | |
| 353 | + | |
| 354 | + // ------------------------------------ Parse end ---------------------------------------------- | |
| 355 | + | |
| 356 | + /** | |
| 357 | + * date 转换为 long | |
| 358 | + * | |
| 359 | + * @param date | |
| 360 | + * @return | |
| 361 | + */ | |
| 362 | + public static Long dateToLong(Date date) { | |
| 363 | + return ObjectUtils.isNull(date) ? null : date.getTime(); | |
| 364 | + | |
| 365 | + } | |
| 366 | + | |
| 367 | + | |
| 368 | +} | |
| 0 | 369 | \ No newline at end of file | ... | ... |
src/main/java/com/irrigation/icl/utils/ObjectUtils.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | |
| 2 | + | |
| 3 | +import com.google.common.collect.Lists; | |
| 4 | +import com.irrigation.icl.exception.ContextRuntimeException; | |
| 5 | +import org.springframework.beans.BeanUtils; | |
| 6 | + | |
| 7 | +import java.util.*; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * 对象工具类 | |
| 11 | + */ | |
| 12 | +public class ObjectUtils { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * 复制对象错误提示 | |
| 16 | + */ | |
| 17 | + private final static String COPY_OBJECT_FAIL = "复制对象失败"; | |
| 18 | + | |
| 19 | + private ObjectUtils() { | |
| 20 | + } | |
| 21 | + | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * 判断对象司是否是空 | |
| 25 | + * | |
| 26 | + * @param obj | |
| 27 | + * @return | |
| 28 | + */ | |
| 29 | + public static boolean isNull(Object obj) { | |
| 30 | + return obj == null; | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * 判断对象司是否不是空 | |
| 35 | + * | |
| 36 | + * @param obj | |
| 37 | + * @return | |
| 38 | + */ | |
| 39 | + public static boolean nonNull(Object obj) { | |
| 40 | + return obj != null; | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * 判断两个对象是否相等 | |
| 45 | + * | |
| 46 | + * @param a | |
| 47 | + * @param b | |
| 48 | + * @return | |
| 49 | + */ | |
| 50 | + public static boolean equals(Object a, Object b) { | |
| 51 | + return a != null && a.equals(b); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * 判断集合是否是空 | |
| 56 | + * | |
| 57 | + * @param collection | |
| 58 | + * @return | |
| 59 | + */ | |
| 60 | + public static boolean isEmpty(Collection<?> collection) { | |
| 61 | + return collection == null || collection.isEmpty(); | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * 判断集合是否不是空 | |
| 66 | + * | |
| 67 | + * @param collection | |
| 68 | + * @return | |
| 69 | + */ | |
| 70 | + public static boolean nonEmpty(Collection<?> collection) { | |
| 71 | + return !isEmpty(collection); | |
| 72 | + } | |
| 73 | + | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * 判断map 是否是空 | |
| 77 | + * | |
| 78 | + * @param map | |
| 79 | + * @return | |
| 80 | + */ | |
| 81 | + public static boolean isEmpty(Map<?, ?> map) { | |
| 82 | + return map == null || map.isEmpty(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * 判断map 是否不是空 | |
| 87 | + * | |
| 88 | + * @param map | |
| 89 | + * @return | |
| 90 | + */ | |
| 91 | + public static boolean nonEmpty(Map<?, ?> map) { | |
| 92 | + return !isEmpty(map); | |
| 93 | + } | |
| 94 | + | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * 判断字符串 是否是 null 或 length=0 | |
| 98 | + * | |
| 99 | + * @param str | |
| 100 | + * @return | |
| 101 | + */ | |
| 102 | + public static boolean stringIsEmpty(String str) { | |
| 103 | + return str == null || str.length() == 0; | |
| 104 | + } | |
| 105 | + | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * 判断字符串 是否不是 null 并且 length!=0 | |
| 109 | + * | |
| 110 | + * @param str | |
| 111 | + * @return | |
| 112 | + */ | |
| 113 | + public static boolean stringNontEmpty(String str) { | |
| 114 | + return !stringIsEmpty(str); | |
| 115 | + } | |
| 116 | + | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * 去掉字符串前后空白字符 | |
| 120 | + * | |
| 121 | + * @param str | |
| 122 | + * @return | |
| 123 | + */ | |
| 124 | + public static String stringTrim(String str) { | |
| 125 | + return str == null ? null : str.trim(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + | |
| 129 | + /** | |
| 130 | + * 单个对象复制 | |
| 131 | + * | |
| 132 | + * @param source | |
| 133 | + * @param targetClass | |
| 134 | + * @param <T> | |
| 135 | + * @return | |
| 136 | + */ | |
| 137 | + | |
| 138 | + public static <T> T copyObject(Object source, Class<T> targetClass) { | |
| 139 | + if (isNull(source)) { | |
| 140 | + return null; | |
| 141 | + } | |
| 142 | + try { | |
| 143 | + T target = targetClass.newInstance(); | |
| 144 | + BeanUtils.copyProperties(source, target); | |
| 145 | + return target; | |
| 146 | + } catch (Exception e) { | |
| 147 | + throw new ContextRuntimeException(COPY_OBJECT_FAIL, e); | |
| 148 | + } | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * 集合复制 | |
| 153 | + * | |
| 154 | + * @param sourceList | |
| 155 | + * @param targetClass | |
| 156 | + * @param <T> | |
| 157 | + * @return | |
| 158 | + */ | |
| 159 | + public static <S, T> List<T> copyObjectList(List<S> sourceList, Class<T> targetClass) { | |
| 160 | + List<T> targetList = Lists.newArrayList(); | |
| 161 | + if (nonEmpty(sourceList)) { | |
| 162 | + for (S source : sourceList) { | |
| 163 | + targetList.add(copyObject(source, targetClass)); | |
| 164 | + } | |
| 165 | + } | |
| 166 | + return targetList; | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * 集合通过连接符号返回字符串 | |
| 171 | + * | |
| 172 | + * @param iterable | |
| 173 | + * @param separator | |
| 174 | + * @return | |
| 175 | + */ | |
| 176 | + public static String stringJoin(Iterable<?> iterable, String separator) { | |
| 177 | + return iterable == null ? null : stringJoin(iterable.iterator(), separator); | |
| 178 | + } | |
| 179 | + | |
| 180 | + private static String stringJoin(Iterator<?> iterator, String separator) { | |
| 181 | + if (iterator == null) { | |
| 182 | + return null; | |
| 183 | + } else if (!iterator.hasNext()) { | |
| 184 | + return ""; | |
| 185 | + } else { | |
| 186 | + Object first = iterator.next(); | |
| 187 | + if (!iterator.hasNext()) { | |
| 188 | + return Objects.toString(first, ""); | |
| 189 | + } else { | |
| 190 | + StringBuilder buf = new StringBuilder(256); | |
| 191 | + if (first != null) { | |
| 192 | + buf.append(first); | |
| 193 | + } | |
| 194 | + | |
| 195 | + while (iterator.hasNext()) { | |
| 196 | + if (separator != null) { | |
| 197 | + buf.append(separator); | |
| 198 | + } | |
| 199 | + | |
| 200 | + Object obj = iterator.next(); | |
| 201 | + if (obj != null) { | |
| 202 | + buf.append(obj); | |
| 203 | + } | |
| 204 | + } | |
| 205 | + return buf.toString(); | |
| 206 | + } | |
| 207 | + } | |
| 208 | + } | |
| 209 | + | |
| 210 | + | |
| 211 | +} | ... | ... |
src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java
| ... | ... | @@ -11,26 +11,23 @@ public class RestResultGeneratorUtil { |
| 11 | 11 | return result; |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | - public static <T> RestResult<T> getSuccessResult() { | |
| 14 | + public static <T> RestResult<T> getSuccessResult() { | |
| 15 | 15 | return getResult(ResultEnum.SUCCESS.getCode(), null, ResultEnum.SUCCESS.getMessage()); |
| 16 | 16 | } |
| 17 | + | |
| 17 | 18 | public static <T> RestResult<T> getSuccessResult(T data) { |
| 18 | 19 | return getResult(ResultEnum.SUCCESS.getCode(), data, ResultEnum.SUCCESS.getMessage()); |
| 19 | 20 | } |
| 20 | 21 | |
| 21 | - public static <T> RestResult<T> getSuccessResult(T data,String message) { | |
| 22 | + public static <T> RestResult<T> getSuccessResult(T data, String message) { | |
| 22 | 23 | return getResult(ResultEnum.SUCCESS.getCode(), data, message); |
| 23 | 24 | } |
| 24 | 25 | |
| 25 | - public static <T> RestResult<T> getSuccessResultMsg(String message) { | |
| 26 | + public static <T> RestResult<T> getSuccessResultMsg(String message) { | |
| 26 | 27 | return getResult(ResultEnum.SUCCESS.getCode(), null, message); |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | |
| 30 | -// public static RestResult<String> getErrorResult(String message) { | |
| 31 | -// return getResult(ResultEnum.ERROR.getCode(), null, message); | |
| 32 | -// } | |
| 33 | - | |
| 34 | 31 | public static <T> RestResult<T> getErrorResult(String message) { |
| 35 | 32 | return getResult(ResultEnum.ERROR.getCode(), null, message); |
| 36 | 33 | } | ... | ... |