DateUtils.java 25 KB

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