DateUtils.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. package org.jeecg.common.util;
  2. import java.beans.PropertyEditorSupport;
  3. import java.sql.Timestamp;
  4. import java.text.DateFormat;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.*;
  8. import org.springframework.util.StringUtils;
  9. /**
  10. * 类描述:时间操作定义类
  11. *
  12. * @Author: 张代浩
  13. * @Date:2012-12-8 12:15:03
  14. * @Version 1.0
  15. */
  16. public class DateUtils extends PropertyEditorSupport {
  17. /**
  18. * 以毫秒表示的时间
  19. */
  20. private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;
  21. private static final long HOUR_IN_MILLIS = 3600 * 1000;
  22. private static final long MINUTE_IN_MILLIS = 60 * 1000;
  23. private static final long SECOND_IN_MILLIS = 1000;
  24. /**
  25. * 指定模式的时间格式
  26. *
  27. * @param pattern
  28. * @return
  29. */
  30. private static SimpleDateFormat getDateFormat(String pattern) {
  31. return new SimpleDateFormat(pattern);
  32. }
  33. /**
  34. * 当前日历,这里用中国时间表示
  35. *
  36. * @return 以当地时区表示的系统当前日历
  37. */
  38. public static Calendar getCalendar() {
  39. return Calendar.getInstance();
  40. }
  41. /**
  42. * 指定毫秒数表示的日历
  43. *
  44. * @param millis 毫秒数
  45. * @return 指定毫秒数表示的日历
  46. */
  47. public static Calendar getCalendar(long millis) {
  48. Calendar cal = Calendar.getInstance();
  49. // --------------------cal.setTimeInMillis(millis);
  50. cal.setTime(new Date(millis));
  51. return cal;
  52. }
  53. // ////////////////////////////////////////////////////////////////////////////
  54. // getDate
  55. // 各种方式获取的Date
  56. // ////////////////////////////////////////////////////////////////////////////
  57. /**
  58. * 当前日期
  59. *
  60. * @return 系统当前时间
  61. */
  62. public static Date getDate() {
  63. return new Date();
  64. }
  65. /**
  66. * 字符串转换成日期
  67. *
  68. * @param str
  69. * @param sdf
  70. * @return
  71. */
  72. public static Date str2Date(String str, SimpleDateFormat sdf) {
  73. if (null == str || "".equals(str)) {
  74. return null;
  75. }
  76. Date date = null;
  77. try {
  78. date = sdf.parse(str);
  79. return date;
  80. } catch (ParseException e) {
  81. e.printStackTrace();
  82. }
  83. return null;
  84. }
  85. /**
  86. * 日期转换为字符串
  87. * @return 字符串
  88. */
  89. public static String date2Str() {
  90. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  91. Date date = getDate();
  92. if (null == date) {
  93. return null;
  94. }
  95. return format.format(date);
  96. }
  97. /**
  98. * 格式化时间
  99. *
  100. * @param date
  101. * @param format
  102. * @return
  103. */
  104. public static String dateformat(String date, String format) {
  105. SimpleDateFormat sformat = new SimpleDateFormat(format);
  106. Date getDate = null;
  107. try {
  108. getDate = sformat.parse(date);
  109. } catch (ParseException e) {
  110. e.printStackTrace();
  111. }
  112. return sformat.format(getDate);
  113. }
  114. /**
  115. * 日期转换为字符串
  116. * @param date 日期
  117. * @return 字符串
  118. */
  119. public static String date2Str(Date date, SimpleDateFormat dateSdf) {
  120. if (null == date) {
  121. return null;
  122. }
  123. return dateSdf.format(date);
  124. }
  125. /**
  126. * 日期转换为字符串
  127. * @param format 日期格式
  128. * @return 字符串
  129. */
  130. public static String getDate(String format) {
  131. Date date = new Date();
  132. if (null == date) {
  133. return null;
  134. }
  135. SimpleDateFormat sdf = new SimpleDateFormat(format);
  136. return sdf.format(date);
  137. }
  138. /**
  139. * 指定毫秒数的时间戳
  140. *
  141. * @param millis 毫秒数
  142. * @return 指定毫秒数的时间戳
  143. */
  144. public static Timestamp getTimestamp(long millis) {
  145. return new Timestamp(millis);
  146. }
  147. /**
  148. * 以字符形式表示的时间戳
  149. *
  150. * @param time 毫秒数
  151. * @return 以字符形式表示的时间戳
  152. */
  153. public static Timestamp getTimestamp(String time) {
  154. return new Timestamp(Long.parseLong(time));
  155. }
  156. /**
  157. * 系统当前的时间戳
  158. *
  159. * @return 系统当前的时间戳
  160. */
  161. public static Timestamp getTimestamp() {
  162. return new Timestamp(System.currentTimeMillis());
  163. }
  164. /**
  165. * 当前时间,格式 yyyy-MM-dd HH:mm:ss
  166. *
  167. * @return 当前时间的标准形式字符串
  168. */
  169. public static String now() {
  170. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  171. return datetimeFormat.format(getCalendar().getTime());
  172. }
  173. /**
  174. * 指定日期的时间戳
  175. *
  176. * @param date 指定日期
  177. * @return 指定日期的时间戳
  178. */
  179. public static Timestamp getTimestamp(Date date) {
  180. return new Timestamp(date.getTime());
  181. }
  182. /**
  183. * 指定日历的时间戳
  184. *
  185. * @param cal 指定日历
  186. * @return 指定日历的时间戳
  187. */
  188. public static Timestamp getCalendarTimestamp(Calendar cal) {
  189. // ---------------------return new Timestamp(cal.getTimeInMillis());
  190. return new Timestamp(cal.getTime().getTime());
  191. }
  192. public static Timestamp gettimestamp() {
  193. Date dt = new Date();
  194. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  195. String nowTime = df.format(dt);
  196. java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
  197. return buydate;
  198. }
  199. // ////////////////////////////////////////////////////////////////////////////
  200. // getMillis
  201. // 各种方式获取的Millis
  202. // ////////////////////////////////////////////////////////////////////////////
  203. /**
  204. * 系统时间的毫秒数
  205. *
  206. * @return 系统时间的毫秒数
  207. */
  208. public static long getMillis() {
  209. return System.currentTimeMillis();
  210. }
  211. /**
  212. * 指定日历的毫秒数
  213. *
  214. * @param cal 指定日历
  215. * @return 指定日历的毫秒数
  216. */
  217. public static long getMillis(Calendar cal) {
  218. // --------------------return cal.getTimeInMillis();
  219. return cal.getTime().getTime();
  220. }
  221. /**
  222. * 指定日期的毫秒数
  223. *
  224. * @param date 指定日期
  225. * @return 指定日期的毫秒数
  226. */
  227. public static long getMillis(Date date) {
  228. return date.getTime();
  229. }
  230. /**
  231. * 指定时间戳的毫秒数
  232. *
  233. * @param ts 指定时间戳
  234. * @return 指定时间戳的毫秒数
  235. */
  236. public static long getMillis(Timestamp ts) {
  237. return ts.getTime();
  238. }
  239. // ////////////////////////////////////////////////////////////////////////////
  240. // formatDate
  241. // 将日期按照一定的格式转化为字符串
  242. // ////////////////////////////////////////////////////////////////////////////
  243. /**
  244. * 默认方式表示的系统当前日期,具体格式:年-月-日
  245. *
  246. * @return 默认日期按“年-月-日“格式显示
  247. */
  248. public static String formatDate() {
  249. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  250. return format.format(getCalendar().getTime());
  251. }
  252. /**
  253. * 默认方式表示的系统当前日期,具体格式:yyyy-MM-dd HH:mm:ss
  254. *
  255. * @return 默认日期按“yyyy-MM-dd HH:mm:ss“格式显示
  256. */
  257. public static String formatDateTime() {
  258. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  259. return datetimeFormat.format(getCalendar().getTime());
  260. }
  261. /**
  262. * 获取时间字符串
  263. */
  264. public static String getDataString(SimpleDateFormat formatstr) {
  265. return formatstr.format(getCalendar().getTime());
  266. }
  267. /**
  268. * 指定日期的默认显示,具体格式:年-月-日
  269. *
  270. * @param cal 指定的日期
  271. * @return 指定日期按“年-月-日“格式显示
  272. */
  273. public static String formatDate(Calendar cal) {
  274. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  275. return format.format(cal.getTime());
  276. }
  277. /**
  278. * 指定日期的默认显示,具体格式:年-月-日
  279. *
  280. * @param date 指定的日期
  281. * @return 指定日期按“年-月-日“格式显示
  282. */
  283. public static String formatDate(Date date) {
  284. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  285. return format.format(date);
  286. }
  287. /**
  288. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日
  289. *
  290. * @param millis 指定的毫秒数
  291. * @return 指定毫秒数表示日期按“年-月-日“格式显示
  292. */
  293. public static String formatDate(long millis) {
  294. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  295. return format.format(new Date(millis));
  296. }
  297. /**
  298. * 默认日期按指定格式显示
  299. *
  300. * @param pattern 指定的格式
  301. * @return 默认日期按指定格式显示
  302. */
  303. public static String formatDate(String pattern) {
  304. return getDateFormat(pattern).format(getCalendar().getTime());
  305. }
  306. /**
  307. * 指定日期按指定格式显示
  308. *
  309. * @param cal 指定的日期
  310. * @param pattern 指定的格式
  311. * @return 指定日期按指定格式显示
  312. */
  313. public static String formatDate(Calendar cal, String pattern) {
  314. return getDateFormat(pattern).format(cal.getTime());
  315. }
  316. /**
  317. * 指定日期按指定格式显示
  318. *
  319. * @param date 指定的日期
  320. * @param pattern 指定的格式
  321. * @return 指定日期按指定格式显示
  322. */
  323. public static String formatDate(Date date, String pattern) {
  324. return getDateFormat(pattern).format(date);
  325. }
  326. // ////////////////////////////////////////////////////////////////////////////
  327. // formatTime
  328. // 将日期按照一定的格式转化为字符串
  329. // ////////////////////////////////////////////////////////////////////////////
  330. /**
  331. * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分
  332. *
  333. * @return 默认日期按“年-月-日 时:分“格式显示
  334. */
  335. public static String formatTime() {
  336. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  337. return timeSdf.format(getCalendar().getTime());
  338. }
  339. /**
  340. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日 时:分
  341. *
  342. * @param millis 指定的毫秒数
  343. * @return 指定毫秒数表示日期按“年-月-日 时:分“格式显示
  344. */
  345. public static String formatTime(long millis) {
  346. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  347. return timeSdf.format(new Date(millis));
  348. }
  349. /**
  350. * 指定日期的默认显示,具体格式:年-月-日 时:分
  351. *
  352. * @param cal 指定的日期
  353. * @return 指定日期按“年-月-日 时:分“格式显示
  354. */
  355. public static String formatTime(Calendar cal) {
  356. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  357. return timeSdf.format(cal.getTime());
  358. }
  359. /**
  360. * 指定日期的默认显示,具体格式:年-月-日 时:分
  361. *
  362. * @param date 指定的日期
  363. * @return 指定日期按“年-月-日 时:分“格式显示
  364. */
  365. public static String formatTime(Date date) {
  366. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  367. return timeSdf.format(date);
  368. }
  369. // ////////////////////////////////////////////////////////////////////////////
  370. // formatShortTime
  371. // 将日期按照一定的格式转化为字符串
  372. // ////////////////////////////////////////////////////////////////////////////
  373. /**
  374. * 默认方式表示的系统当前日期,具体格式:时:分
  375. *
  376. * @return 默认日期按“时:分“格式显示
  377. */
  378. public static String formatShortTime() {
  379. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  380. return shortTimeSdf.format(getCalendar().getTime());
  381. }
  382. /**
  383. * 指定毫秒数表示日期的默认显示,具体格式:时:分
  384. *
  385. * @param millis 指定的毫秒数
  386. * @return 指定毫秒数表示日期按“时:分“格式显示
  387. */
  388. public static String formatShortTime(long millis) {
  389. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  390. return shortTimeSdf.format(new Date(millis));
  391. }
  392. /**
  393. * 指定日期的默认显示,具体格式:时:分
  394. *
  395. * @param cal 指定的日期
  396. * @return 指定日期按“时:分“格式显示
  397. */
  398. public static String formatShortTime(Calendar cal) {
  399. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  400. return shortTimeSdf.format(cal.getTime());
  401. }
  402. /**
  403. * 指定日期的默认显示,具体格式:时:分
  404. *
  405. * @param date 指定的日期
  406. * @return 指定日期按“时:分“格式显示
  407. */
  408. public static String formatShortTime(Date date) {
  409. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  410. return shortTimeSdf.format(date);
  411. }
  412. // ////////////////////////////////////////////////////////////////////////////
  413. // parseDate
  414. // parseCalendar
  415. // parseTimestamp
  416. // 将字符串按照一定的格式转化为日期或时间
  417. // ////////////////////////////////////////////////////////////////////////////
  418. /**
  419. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  420. *
  421. * @param src 将要转换的原始字符窜
  422. * @param pattern 转换的匹配格式
  423. * @return 如果转换成功则返回转换后的日期
  424. * @throws ParseException
  425. * @throws
  426. */
  427. public static Date parseDate(String src, String pattern) throws ParseException {
  428. return getDateFormat(pattern).parse(src);
  429. }
  430. /**
  431. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  432. *
  433. * @param src 将要转换的原始字符窜
  434. * @param pattern 转换的匹配格式
  435. * @return 如果转换成功则返回转换后的日期
  436. * @throws ParseException
  437. * @throws
  438. */
  439. public static Calendar parseCalendar(String src, String pattern) throws ParseException {
  440. Date date = parseDate(src, pattern);
  441. Calendar cal = Calendar.getInstance();
  442. cal.setTime(date);
  443. return cal;
  444. }
  445. public static String formatAddDate(String src, String pattern, int amount) throws ParseException {
  446. Calendar cal;
  447. cal = parseCalendar(src, pattern);
  448. cal.add(Calendar.DATE, amount);
  449. return formatDate(cal);
  450. }
  451. /**
  452. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  453. *
  454. * @param src 将要转换的原始字符窜
  455. * @param pattern 转换的匹配格式
  456. * @return 如果转换成功则返回转换后的时间戳
  457. * @throws ParseException
  458. * @throws
  459. */
  460. public static Timestamp parseTimestamp(String src, String pattern) throws ParseException {
  461. Date date = parseDate(src, pattern);
  462. return new Timestamp(date.getTime());
  463. }
  464. // ////////////////////////////////////////////////////////////////////////////
  465. // dateDiff
  466. // 计算两个日期之间的差值
  467. // ////////////////////////////////////////////////////////////////////////////
  468. /**
  469. * 计算两个时间之间的差值,根据标志的不同而不同
  470. *
  471. * @param flag 计算标志,表示按照年/月/日/时/分/秒等计算
  472. * @param calSrc 减数
  473. * @param calDes 被减数
  474. * @return 两个日期之间的差值
  475. */
  476. public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) {
  477. long millisDiff = getMillis(calSrc) - getMillis(calDes);
  478. if (flag == 'y') {
  479. return (calSrc.get(Calendar.YEAR) - calDes.get(Calendar.YEAR));
  480. }
  481. if (flag == 'd') {
  482. return (int) (millisDiff / DAY_IN_MILLIS);
  483. }
  484. if (flag == 'h') {
  485. return (int) (millisDiff / HOUR_IN_MILLIS);
  486. }
  487. if (flag == 'm') {
  488. return (int) (millisDiff / MINUTE_IN_MILLIS);
  489. }
  490. if (flag == 's') {
  491. return (int) (millisDiff / SECOND_IN_MILLIS);
  492. }
  493. return 0;
  494. }
  495. /**
  496. * String类型 转换为Date, 如果参数长度为10 转换格式”yyyy-MM-dd“ 如果参数长度为19 转换格式”yyyy-MM-dd
  497. * HH:mm:ss“ * @param text String类型的时间值
  498. */
  499. @Override
  500. public void setAsText(String text) throws IllegalArgumentException {
  501. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  502. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  503. if (StringUtils.hasText(text)) {
  504. try {
  505. if (text.indexOf(":") == -1 && text.length() == 10) {
  506. setValue(format.parse(text));
  507. } else if (text.indexOf(":") > 0 && text.length() == 19) {
  508. setValue(datetimeFormat.parse(text));
  509. } else {
  510. throw new IllegalArgumentException("Could not parse date, date format is error ");
  511. }
  512. } catch (ParseException ex) {
  513. IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
  514. iae.initCause(ex);
  515. throw iae;
  516. }
  517. } else {
  518. setValue(null);
  519. }
  520. }
  521. public static int getYear() {
  522. GregorianCalendar calendar = new GregorianCalendar();
  523. calendar.setTime(getDate());
  524. return calendar.get(Calendar.YEAR);
  525. }
  526. public static Date timeStampToDate(Long time) {
  527. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
  528. Date date;
  529. try {
  530. date = sdf.parse(sdf.format(time));
  531. return date;
  532. } catch (ParseException e) {
  533. e.printStackTrace();
  534. return null;
  535. }
  536. }
  537. public static List<Map<String, String>> getDateDayRange(String startDate, String endDate) throws ParseException {
  538. List<Map<String, String>> list = new ArrayList<>();
  539. String splitDate = DateUtils.addDay(endDate, -1);
  540. splitDate = DateUtils.addDay(splitDate, 1);
  541. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  542. Map<String, String> map = new HashMap<String, String>();
  543. map.put("startDate", startDate);
  544. map.put("endDate", endDate);
  545. list.add(map);
  546. } else {
  547. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  548. Map<String, String> map = new HashMap<String, String>();
  549. map.put("startDate", splitDate);
  550. map.put("endDate", endDate);
  551. list.add(map);
  552. splitDate = DateUtils.addDay(splitDate, -1);
  553. endDate = DateUtils.addDay(endDate, -1);
  554. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  555. splitDate = startDate;
  556. }
  557. }
  558. }
  559. return list;
  560. }
  561. public static List<Map<String, String>> getDateWeekRange(String startDate, String endDate) throws ParseException {
  562. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  563. String splitDate = DateUtils.addWeek(endDate, -1);
  564. splitDate = DateUtils.addDay(splitDate, 1);
  565. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  566. Map<String, String> map = new HashMap<String, String>();
  567. map.put("startDate", startDate);
  568. map.put("endDate", endDate);
  569. list.add(map);
  570. } else {
  571. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  572. Map<String, String> map = new HashMap<String, String>();
  573. map.put("startDate", splitDate);
  574. map.put("endDate", endDate);
  575. list.add(map);
  576. splitDate = DateUtils.addWeek(splitDate, -1);
  577. endDate = DateUtils.addWeek(endDate, -1);
  578. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  579. splitDate = startDate;
  580. }
  581. }
  582. }
  583. return list;
  584. }
  585. public static List<Map<String, String>> getDateMonthRange(String startDate, String endDate) throws ParseException {
  586. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  587. String splitDate = DateUtils.addMonth(endDate, -1);
  588. splitDate = DateUtils.addDay(splitDate, 1);
  589. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  590. Map<String, String> map = new HashMap<String, String>();
  591. map.put("startDate", startDate);
  592. map.put("endDate", endDate);
  593. list.add(map);
  594. } else {
  595. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  596. Map<String, String> map = new HashMap<String, String>();
  597. map.put("startDate", splitDate);
  598. map.put("endDate", endDate);
  599. list.add(map);
  600. splitDate = DateUtils.addMonth(splitDate, -1);
  601. endDate = DateUtils.addMonth(endDate, -1);
  602. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  603. splitDate = startDate;
  604. }
  605. }
  606. }
  607. return list;
  608. }
  609. public static String addMonth(String dateString, int month) throws ParseException {
  610. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  611. Date date = sdf.parse(dateString);
  612. Calendar calendar = Calendar.getInstance();
  613. calendar.setTime(date);
  614. calendar.add(Calendar.MONTH, month);
  615. return sdf.format(calendar.getTime());
  616. }
  617. public static String addWeek(String dateString, int week) throws ParseException {
  618. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  619. Date date = sdf.parse(dateString);
  620. Calendar calendar = Calendar.getInstance();
  621. calendar.setTime(date);
  622. calendar.add(Calendar.WEEK_OF_YEAR, week);
  623. return sdf.format(calendar.getTime());
  624. }
  625. public static String addDay(String dateString, int day) throws ParseException {
  626. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  627. Date date = sdf.parse(dateString);
  628. Calendar calendar = Calendar.getInstance();
  629. calendar.setTime(date);
  630. calendar.add(Calendar.DAY_OF_YEAR, day);
  631. return sdf.format(calendar.getTime());
  632. }
  633. public static Date addDay(Date date, int day) {
  634. Calendar calendar = Calendar.getInstance();
  635. calendar.setTime(date);
  636. calendar.add(Calendar.DAY_OF_YEAR, day);
  637. return calendar.getTime();
  638. }
  639. public static Date addSecond(Date date, int seconds) {
  640. Calendar calendar = Calendar.getInstance();
  641. calendar.setTime(date);
  642. calendar.add(Calendar.SECOND, seconds);
  643. return calendar.getTime();
  644. }
  645. public static int compareDate(String firstDateString, String secondDateString) throws ParseException {
  646. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  647. Date firstDate = sdf.parse(firstDateString);
  648. Date secondDate = sdf.parse(secondDateString);
  649. long first = firstDate.getTime();
  650. long second = secondDate.getTime();
  651. return first == second ? 0 : (first > second ? 1 : -1);
  652. }
  653. public static String timeStamp2Date(Timestamp timeLong) {
  654. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
  655. Date date;
  656. try {
  657. date = sdf.parse(sdf.format(timeLong));
  658. return sdf.format(date);
  659. } catch (ParseException e) {
  660. e.printStackTrace();
  661. return null;
  662. }
  663. }
  664. public static String timeStamp2Date(long currentTimeMillis) {
  665. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
  666. Date date;
  667. try {
  668. date = sdf.parse(sdf.format(currentTimeMillis));
  669. return sdf.format(date);
  670. } catch (ParseException e) {
  671. e.printStackTrace();
  672. return null;
  673. }
  674. }
  675. }