1. import com.demo.xzm.newTool.emnu.EnumDateType;
    2. import com.demo.xzm.newTool.emnu.EnumDateType2;
    3. import org.slf4j.Logger;
    4. import org.slf4j.LoggerFactory;
    5. import java.time.*;
    6. import java.time.format.DateTimeFormatter;
    7. import java.time.temporal.TemporalAdjusters;
    8. import java.util.Arrays;
    9. import java.util.Date;
    10. public class NewDateUtil {
    11. private static final Logger logger = LoggerFactory.getLogger(NewDateUtil.class);
    12. private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    13. private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    14. private static final String ERROR_MSG = "输入内容有误!当前仅支持 yyyy-MM-dd、yyyy/MM/dd 格式的字符串、Date 类型的日期、LocalDate 以及 InStant 类型的日期)";
    15. /**
    16. * 对时间进行任意的加减。eg. 获取 2021-6-9 10:34:05 近24小时
    17. *
    18. * @param t
    19. * @param plusNum
    20. * @param <T>
    21. */
    22. public static <T> String datePlusOrMinus(T t, EnumDateType2 enumDateType2, long plusNum) {
    23. LocalDateTime localDateTime = converOtherType2LDT(t);
    24. if (enumDateType2 == EnumDateType2.HOUR) {// 按 小时
    25. localDateTime = localDateTime.plusHours(plusNum);
    26. } else if (enumDateType2 == EnumDateType2.DAY) {// 按 天
    27. localDateTime = localDateTime.plusDays(plusNum);
    28. } else if (enumDateType2 == EnumDateType2.MONTH) {// 按 月
    29. localDateTime = localDateTime.plusMonths(plusNum);
    30. } else if (enumDateType2 == EnumDateType2.YEAR) {// 按 年
    31. localDateTime = localDateTime.plusYears(plusNum);
    32. }
    33. return localDateTime.format(DATE_TIME_FORMATTER);
    34. }
    35. /**
    36. * 获取指定的时间差:eg. [近 7 小时, 现在]
    37. *
    38. * @param t
    39. * @param <T>
    40. * @return
    41. */
    42. public static <T> String[] getDate(T t, EnumDateType enumDateType) {
    43. return method(t, enumDateType, null);
    44. }
    45. public static <T> String[] getDate(T t, EnumDateType enumDateType, int num) {
    46. return method(t, enumDateType, num);
    47. }
    48. /**
    49. * 判断日期是否在范围内
    50. *
    51. * @param sourceDate 被比较日期
    52. * @param startDate 开始日期
    53. * @param endDate 结束日期
    54. * @return
    55. */
    56. public static <T> boolean checkDateRange(T sourceDate, T startDate, T endDate) {
    57. LocalDate sourceDateNew = converOtherType2LD(sourceDate);
    58. LocalDate startDateNew = converOtherType2LD(startDate).minusDays(1);
    59. LocalDate endDateNew = converOtherType2LD(endDate).plusDays(1);
    60. return sourceDateNew.isBefore(endDateNew) && sourceDateNew.isAfter(startDateNew);
    61. }
    62. /**
    63. * 获取日期的开始和结束时间
    64. *
    65. * @param t
    66. * @param <T>
    67. * @return
    68. */
    69. public static <T> String[] getDayStartAndEndTime(T t) {
    70. String[] result = new String[2];
    71. LocalDate date = converOtherType2LD(t);
    72. result[0] = LocalDateTime.of(date, LocalTime.MIN).format(DATE_TIME_FORMATTER);
    73. result[1] = LocalDateTime.of(date, LocalTime.MAX).format(DATE_TIME_FORMATTER);
    74. Arrays.stream(result).forEach(System.out::println);
    75. return result;
    76. }
    77. /**
    78. * 获取指定日期的周一(Monday)和周日(Sunday)
    79. *
    80. * @param t
    81. * @param <T>
    82. * @return
    83. */
    84. public static <T> String[] getWeekStartAndEndDate(T t) {
    85. String[] result = new String[2];
    86. //LocalDate date = converOtherType2LD(t);
    87. //LocalDate todayOfLastWeek = date.minusDays(7);
    88. //result[0] = convertLocalDate2LDString(todayOfLastWeek.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).plusDays(1));
    89. //result[1] = convertLocalDate2LDString(todayOfLastWeek.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).minusDays(1));
    90. //return result;
    91. LocalDate date = converOtherType2LD(t);
    92. // 所在周开始时间
    93. result[0] = convertLocalDate2LDString(date.with(DayOfWeek.MONDAY));
    94. // 所在周结束时间
    95. result[1] = convertLocalDate2LDString(date.with(DayOfWeek.SUNDAY));
    96. return result;
    97. }
    98. /**
    99. * 获取日期的月初和月末
    100. *
    101. * @param t
    102. * @param <T>
    103. * @return
    104. */
    105. public static <T> String[] getMonthStartAndEndDate(T t) {
    106. String[] result = new String[2];
    107. LocalDate date = converOtherType2LD(t);
    108. result[0] = convertLocalDate2LDString(date.with(TemporalAdjusters.firstDayOfMonth()));
    109. result[1] = convertLocalDate2LDString(date.with(TemporalAdjusters.lastDayOfMonth()));
    110. Arrays.stream(result).forEach(System.out::println);
    111. return result;
    112. }
    113. /**
    114. * 功能:获取每月的天数
    115. *
    116. * @param t 日期(Date)/LocalDate/String 均可
    117. * @param <T>
    118. * @return
    119. */
    120. public static <T> int getMonthCount(T t) {
    121. // 获取每月的天数
    122. return converOtherType2LD(t).lengthOfMonth();
    123. }
    124. /**
    125. * 功能:获取当前日期的周一和周日
    126. */
    127. public static <T> String[] getMondayAndSunday(T t) {
    128. LocalDate date = null;
    129. if (t instanceof String) {
    130. date = LocalDate.parse(parseUnStandardString2LDString((String) t), DATE_FORMATTER);
    131. } else if (t instanceof LocalDate) {
    132. date = (LocalDate) t;
    133. } else if (t instanceof LocalDateTime) {
    134. date = convertLocalDateTime2LD((LocalDateTime) t);
    135. } else if (t instanceof Date) {
    136. date = ((Date) t).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    137. //date = ((Date) t).toInstant().atZone(ZoneOffset.ofHours(0)).toLocalDate();
    138. } else if (t instanceof Instant) {
    139. date = convertInstant2LD((Instant) t);
    140. } else {
    141. throw new RuntimeException(ERROR_MSG);
    142. }
    143. String[] startAndEndDate = new String[2];
    144. if (!date.getDayOfWeek().equals(DayOfWeek.MONDAY)) {
    145. startAndEndDate[0] = date.with(TemporalAdjusters.previous(DayOfWeek.MONDAY)).toString();
    146. } else {
    147. startAndEndDate[0] = date.toString();
    148. }
    149. if (!date.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
    150. startAndEndDate[1] = date.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)).toString();
    151. } else {
    152. startAndEndDate[1] = date.toString();
    153. }
    154. Arrays.stream(startAndEndDate).forEach(System.out::println);
    155. return startAndEndDate;
    156. }
    157. /**
    158. * 功能:解析日期字符串 & 日期格式化 功能二合一
    159. *
    160. * @param t
    161. * @param <T>
    162. * @param <R>
    163. * @return
    164. */
    165. public static <T, R> R dateFormatOrParse(T t, Class<R> clazz) {
    166. if (!(t instanceof String || t instanceof LocalDate || t instanceof LocalDateTime || t instanceof Date || t instanceof Instant)) {
    167. throw new RuntimeException(ERROR_MSG);
    168. }
    169. if ("java.lang.String".equalsIgnoreCase(clazz.getName())) {
    170. return (R) converOtherType2MatchString(t);
    171. } else if ("java.time.LocalDate".equalsIgnoreCase(clazz.getName())) {
    172. return (R) converOtherType2LD(t);
    173. } else if ("java.time.LocalDateTime".equalsIgnoreCase(clazz.getName())) {
    174. return (R) converOtherType2LDT(t);
    175. } else if ("java.util.Date".equalsIgnoreCase(clazz.getName())) {
    176. return (R) converOtherType2Date(t);
    177. } else if ("java.time.Instant".equalsIgnoreCase(clazz.getName())) {
    178. return (R) converOtherType2Instant(t);
    179. }
    180. throw new RuntimeException(ERROR_MSG);
    181. }
    182. //////////////////////////////////////////////////////////////// ↓
    183. /**
    184. * 将 String、Date 类型转成 LocalDate 类型
    185. *
    186. * @param t
    187. * @param <T>
    188. * @return
    189. */
    190. public static <T> LocalDate converOtherType2LD(T t) {
    191. if (t instanceof String) {
    192. return converString2LD((String) t);
    193. } else if (t instanceof Date) {
    194. return convertDate2LD((Date) t);
    195. } else if (t instanceof LocalDate) {
    196. return (LocalDate) t;
    197. } else if (t instanceof LocalDateTime) {
    198. return converOtherType2LD(convertLocalDateTime2LD((LocalDateTime) t));
    199. } else if (t instanceof Instant) {
    200. return convertInstant2LD((Instant) t);
    201. }
    202. throw new RuntimeException("当前日期格式/类型不支持转换");
    203. }
    204. /**
    205. * 功能:将 String、Date、LocalDateTime、Instant 类型日期转成 LocalDateTime
    206. *
    207. * @param t 时间字符串 or Date or LocalDateTime 类型
    208. * @return
    209. */
    210. public static <T> LocalDateTime converOtherType2LDT(T t) {
    211. if (t instanceof String) {
    212. return converString2LDT((String) t);
    213. } else if (t instanceof LocalDate) {
    214. return convertLocalDate2LDT((LocalDate) t);
    215. } else if (t instanceof LocalDateTime) {
    216. return (LocalDateTime) t;
    217. } else if (t instanceof Date) {
    218. return convertDate2LDT((Date) t);
    219. } else if (t instanceof Instant) {
    220. return convertInstant2LDT((Instant) t);
    221. }
    222. throw new RuntimeException("当前日期格式不支持");
    223. }
    224. public static <T> Date converOtherType2Date(T t) {
    225. if (t instanceof String) {
    226. return converString2Date((String) t);
    227. } else if (t instanceof Date) {
    228. return (Date) t;
    229. } else if (t instanceof LocalDate) {
    230. return converOtherType2Date(convertLocalDate2LDT((LocalDate) t));
    231. } else if (t instanceof LocalDateTime) {// 主
    232. return convertLocalDateTime2Date((LocalDateTime) t);
    233. } else if (t instanceof Instant) {
    234. return converOtherType2Date(convertInstant2LDT((Instant) t));
    235. }
    236. throw new RuntimeException("当前日期格式/类型不支持转换");
    237. }
    238. public static <T> Instant converOtherType2Instant(T t) {
    239. if (t instanceof String) {
    240. return converOtherType2Instant(converString2LDT((String) t));
    241. } else if (t instanceof Date) {
    242. return converOtherType2Instant(convertDate2LDT((Date) t));
    243. } else if (t instanceof LocalDate) {
    244. return converOtherType2Instant(convertLocalDate2LDT((LocalDate) t));
    245. } else if (t instanceof LocalDateTime) {// (主)
    246. return convertLocalDateTime2Instant((LocalDateTime) t);
    247. } else if (t instanceof Instant) {
    248. return (Instant) t;
    249. }
    250. throw new RuntimeException("当前日期格式/类型不支持转换");
    251. }
    252. /**
    253. * 将日期/时间转成对应的字符串
    254. *
    255. * @param t
    256. * @param <T>
    257. * @return
    258. */
    259. public static <T> String converOtherType2MatchString(T t) {
    260. if (t instanceof String) {
    261. return parseUnStandardString2MatchString((String) t);
    262. } else if (t instanceof Date) {
    263. return converOtherType2MatchString(convertDate2LDT((Date) t));
    264. } else if (t instanceof LocalDate) {
    265. return convertLocalDate2LDString((LocalDate) t);
    266. } else if (t instanceof LocalDateTime) {// 主
    267. return convertLocalDateTime2LDTString((LocalDateTime) t);
    268. } else if (t instanceof Instant) {
    269. return converOtherType2MatchString(convertInstant2LDT((Instant) t));
    270. }
    271. throw new RuntimeException(ERROR_MSG);
    272. }
    273. //////////////////////////////////////////////////////////////// ↑
    274. private static String convertLocalDateTime2LDTString(LocalDateTime t) {
    275. return t.format(DATE_TIME_FORMATTER);
    276. }
    277. private static Instant convertLocalDateTime2Instant(LocalDateTime t) {
    278. return t.atZone(ZoneOffset.ofHours(0)).toInstant();
    279. }
    280. private static LocalDateTime convertDate2LDT(Date date) {
    281. return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    282. }
    283. private static LocalDateTime convertInstant2LDT(Instant instant) {
    284. return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    285. }
    286. private static String convertInstant2LDTString(Instant instant) {
    287. return convertLocalDateTime2LDTString(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));
    288. }
    289. private static LocalDate convertInstant2LD(Instant instant) {
    290. return convertLocalDateTime2LD(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));
    291. }
    292. private static LocalDate convertDate2LD(Date date) {
    293. return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    294. //return date.toInstant().atZone(ZoneOffset.ofHours(0)).toLocalDate();
    295. }
    296. private static Date convertLocalDateTime2Date(LocalDateTime time) {
    297. return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    298. //return Date.from(time.atZone(ZoneOffset.ofHours(0)).toInstant());
    299. }
    300. private static Date convertLocalDate2Date(LocalDate time) {
    301. return Date.from(time.atStartOfDay(ZoneId.systemDefault()).toInstant());
    302. //return Date.from(time.atStartOfDay(ZoneOffset.ofHours(0)).toInstant());
    303. }
    304. private static String convertLocalDate2LDString(LocalDate localDate) {
    305. return localDate.format(DATE_FORMATTER);
    306. }
    307. private static LocalDate convertLocalDateTime2LD(LocalDateTime t) {
    308. return LocalDate.of(t.getYear(), t.getMonthValue(), t.getDayOfMonth());
    309. }
    310. private static LocalDateTime convertLocalDate2LDT(LocalDate t) {
    311. return LocalDateTime.of(t, LocalTime.of(0, 0, 0));
    312. }
    313. /**
    314. * 将字符串转成 LocalDateTime
    315. *
    316. * @param t
    317. * @return
    318. */
    319. private static LocalDateTime converString2LDT(String t) {
    320. return LocalDateTime.parse(parseUnStandardString2LDTString(t), DATE_TIME_FORMATTER);
    321. }
    322. /**
    323. * 将字符串转成 LocalDate
    324. *
    325. * @param t
    326. * @return
    327. */
    328. private static LocalDate converString2LD(String t) {
    329. return parseUnStandardString2LD(t);
    330. }
    331. /**
    332. * 将 LocalDateTime 转成 Date
    333. *
    334. * @param t
    335. * @return
    336. */
    337. private static Date converString2Date(String t) {
    338. return convertLocalDate2Date(converString2LD(t));
    339. }
    340. /**
    341. * 不规范的日期格式转成标准可解析格式字符串,然后转成 LocalDate
    342. *
    343. * @param t
    344. * @param <T>
    345. * @return
    346. */
    347. private static <T> LocalDate parseUnStandardString2LD(String t) {
    348. LocalDate resultLocalDate;
    349. String temp = t.replaceAll(" ", "");
    350. String[] split = temp.split("-");
    351. if (split.length != 3) {
    352. throw new RuntimeException(ERROR_MSG);
    353. }
    354. if (temp.length() != 10) { // 不规范的日期格式:eg. 2021-6-9、2021-06-9、2021-6-09
    355. resultLocalDate = LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
    356. } else {
    357. resultLocalDate = LocalDate.parse(temp, DATE_FORMATTER);
    358. }
    359. return resultLocalDate;
    360. }
    361. private static String parseUnStandardString2MatchString(String t) {
    362. return t.contains(":") ? parseUnStandardString2LDTString(t) : parseUnStandardString2LDString(t);
    363. }
    364. private static String parseUnStandardString2LDString(String t) {
    365. // eg. 2021-6-10 14:12:27
    366. String temp = t;
    367. String[] split = null;
    368. String splitChar = null;
    369. if (temp.contains("-")) {
    370. split = temp.split("-");
    371. splitChar = "-";
    372. } else if (temp.contains("/")) {
    373. split = temp.split("/");
    374. splitChar = "/";
    375. }
    376. if (split.length != 3) {
    377. throw new RuntimeException(ERROR_MSG);
    378. }
    379. if (temp.length() != 10) { // 不规范的日期格式:eg. 2021-6-9、2021-06-9、2021-6-09、甚至是 2021-6-10 14:12:27
    380. String s1 = split[0];
    381. String s2 = split[1];
    382. String s3 = split[2];
    383. s1 = String.format("%04d", Integer.parseInt(s1));
    384. s2 = String.format("%02d", Integer.parseInt(s2));
    385. if (s3.contains(" ") && s3.contains(":")) {
    386. s3 = s3.split(" ")[0];
    387. }
    388. s3 = String.format("%02d", Integer.parseInt(s3));
    389. //return s1 + "-" + s2 + "-" + s3;
    390. //return s1 + splitChar + s2 + splitChar + s3;
    391. return new StringBuilder().append(s1).append(splitChar).append(s2).append(splitChar).append(s3).toString();
    392. } else {
    393. return t;
    394. }
    395. }
    396. private static String parseUnStandardString2LDTString(String t) {
    397. String temp = t;
    398. String[] split = temp.split("-");
    399. if (split.length != 3) {
    400. throw new RuntimeException(ERROR_MSG);
    401. }
    402. if (temp.length() <= 10) { // 不规范的日期格式:eg. 2021-6-9、2021-06-9、2021-6-09
    403. String s1 = split[0];
    404. String s2 = split[1];
    405. String s3 = split[2];
    406. s1 = String.format("%04d", Integer.parseInt(s1));
    407. s2 = String.format("%02d", Integer.parseInt(s2));
    408. s3 = String.format("%02d", Integer.parseInt(s3));
    409. return s1 + "-" + s2 + "-" + s3 + " 00:00:00";
    410. } else if (temp.length() != 20) { // 不规范的日期格式:eg. 2021-6-9、2021-06-9、2021-6-09
    411. String s1 = split[0];
    412. String s2 = split[1];
    413. String s3 = split[2].split(" ")[0];
    414. String s4 = split[2].split(" ")[1];
    415. s1 = String.format("%04d", Integer.parseInt(s1));
    416. s2 = String.format("%02d", Integer.parseInt(s2));
    417. s3 = String.format("%02d", Integer.parseInt(s3));
    418. return s1 + "-" + s2 + "-" + s3 + " " + s4;
    419. } else {
    420. return t;
    421. }
    422. }
    423. private static <T> String[] method(T t, EnumDateType enumDateType, Integer num) {
    424. LocalDate date = converOtherType2LD(t);
    425. String[] startAndEndTime = new String[2];// 0、1:时间,2、3:日期
    426. if (enumDateType == EnumDateType.HOUR) {// 近 N 小时(默认24小时)
    427. startAndEndTime[0] = LocalDateTime.of(date, LocalTime.now()).minusHours(num != null ? num : 24).format(DATE_TIME_FORMATTER);
    428. startAndEndTime[1] = LocalDateTime.of(date, LocalTime.now()).format(DATE_TIME_FORMATTER);
    429. } else if (enumDateType == EnumDateType.DAY) {// 近 N 天(默认7天)
    430. startAndEndTime[0] = LocalDateTime.of(date.minusDays(num != null ? num : 7), LocalTime.now()).format(DATE_TIME_FORMATTER);
    431. startAndEndTime[1] = LocalDateTime.of(date.minusDays(0), LocalTime.now()).format(DATE_TIME_FORMATTER);
    432. } else if (enumDateType == EnumDateType.MONTH) {// 近 N 月(默认6个月)
    433. startAndEndTime[0] = LocalDateTime.of(date.minusMonths(num != null ? num : 6), LocalTime.now()).format(DATE_TIME_FORMATTER);
    434. startAndEndTime[1] = LocalDateTime.of(date.minusMonths(0), LocalTime.now()).format(DATE_TIME_FORMATTER);
    435. } else if (enumDateType == EnumDateType.TODAY_TIME) {// 今天的开始和结束时间
    436. startAndEndTime[0] = LocalDateTime.of(date, LocalTime.MIN).format(DATE_TIME_FORMATTER);// 今天 00:00:00
    437. startAndEndTime[1] = LocalDateTime.of(date, LocalTime.MAX).format(DATE_TIME_FORMATTER);// 今天 23:59:59
    438. } else if (enumDateType == EnumDateType.LAST_24_HOUR) {// 近 24 小时(含当天)
    439. startAndEndTime[0] = LocalDateTime.of(date.plusDays(-1), LocalTime.now()).format(DATE_TIME_FORMATTER);
    440. startAndEndTime[1] = LocalDateTime.of(date, LocalTime.now()).format(DATE_TIME_FORMATTER);
    441. } else if (enumDateType == EnumDateType.LAST_30_DAY_CONTAIN_THIS) {// 近 30 天(含当天)
    442. startAndEndTime[0] = LocalDateTime.of(date.plusDays(-30), LocalTime.now()).format(DATE_TIME_FORMATTER);
    443. startAndEndTime[1] = LocalDateTime.of(date.plusDays(-0), LocalTime.now()).format(DATE_TIME_FORMATTER);
    444. } else if (enumDateType == EnumDateType.LAST_30_DAY_NOT_CONTAIN_THIS) {// 近 30 天(不含当天)
    445. startAndEndTime[0] = LocalDateTime.of(date.plusDays(-31), LocalTime.now()).format(DATE_TIME_FORMATTER);
    446. startAndEndTime[1] = LocalDateTime.of(date.plusDays(-1), LocalTime.now()).format(DATE_TIME_FORMATTER);
    447. } else if (enumDateType == EnumDateType.LAST_6_MONTH_CONTAIN_THIS) {// 近 6 个月(含当月)
    448. startAndEndTime[0] = date.plusMonths(-6).format(DATE_FORMATTER);
    449. startAndEndTime[1] = date.plusMonths(-0).format(DATE_FORMATTER);
    450. } else if (enumDateType == EnumDateType.LAST_6_MONTH_NOT_CONTAIN_THIS) {// 近 6 个月(不含当月)
    451. startAndEndTime[0] = date.plusMonths(-7).format(DATE_FORMATTER);
    452. startAndEndTime[1] = date.plusMonths(-1).format(DATE_FORMATTER);
    453. } else if (enumDateType == EnumDateType.LAST_12_MONTH_CONTAIN_THIS) {// 近 12 个月(含)
    454. startAndEndTime[0] = date.plusYears(-1).format(DATE_FORMATTER);
    455. startAndEndTime[1] = date.plusYears(-0).format(DATE_FORMATTER);
    456. }
    457. Arrays.stream(startAndEndTime).forEach(System.out::println);
    458. return startAndEndTime;
    459. }
    460. }
    1. public enum EnumDateType {// 枚举类
    2. HOUR(-1, "按小时"),
    3. DAY(-2, "按天"),
    4. MONTH(-3, "按月"),
    5. TODAY_TIME(0, "今天的开始和结束时间"),
    6. LAST_24_HOUR(1, "最近24小时(含当天)"),
    7. LAST_30_DAY_CONTAIN_THIS(2, "近 30 天(含)"),
    8. LAST_30_DAY_NOT_CONTAIN_THIS(3, "近 30 天(不含)"),
    9. LAST_6_MONTH_CONTAIN_THIS(4, "近 6 月(含)"),
    10. LAST_6_MONTH_NOT_CONTAIN_THIS(5, "近 6 月(不含)"),
    11. LAST_12_MONTH_CONTAIN_THIS(6, "近 6 月(不含)");
    12. private Integer code;
    13. private String desc;
    14. EnumDateType(Integer code, String desc) {
    15. this.code = code;
    16. this.desc = desc;
    17. }
    18. public Integer getCode() {
    19. return code;
    20. }
    21. public void setCode(Integer code) {
    22. this.code = code;
    23. }
    24. public String getDesc() {
    25. return desc;
    26. }
    27. public void setDesc(String desc) {
    28. this.desc = desc;
    29. }
    30. }
    1. public enum EnumDateType2 {// 枚举类
    2. HOUR(1, "按小时"),
    3. DAY(2, "按天"),
    4. MONTH(3, "按月"),
    5. YEAR(4, "按年");
    6. private Integer code;
    7. private String desc;
    8. EnumDateType2(Integer code) {
    9. this.code = code;
    10. }
    11. EnumDateType2(Integer code, String desc) {
    12. this.code = code;
    13. this.desc = desc;
    14. }
    15. public Integer getCode() {
    16. return code;
    17. }
    18. public void setCode(Integer code) {
    19. this.code = code;
    20. }
    21. public String getDesc() {
    22. return desc;
    23. }
    24. public void setDesc(String desc) {
    25. this.desc = desc;
    26. }
    27. }