DateUtils.java 24 KB

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