DateUtils.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. package org.jeecg.common.util;
  2. import org.springframework.util.StringUtils;
  3. import java.beans.PropertyEditorSupport;
  4. import java.sql.Timestamp;
  5. import java.text.DateFormat;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.*;
  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.setTime(new Date(millis));
  50. return cal;
  51. }
  52. // ////////////////////////////////////////////////////////////////////////////
  53. // getDate
  54. // 各种方式获取的Date
  55. // ////////////////////////////////////////////////////////////////////////////
  56. /**
  57. * 当前日期
  58. *
  59. * @return 系统当前时间
  60. */
  61. public static Date getDate() {
  62. return new Date();
  63. }
  64. /**
  65. * 字符串转换成日期
  66. *
  67. * @param str
  68. * @param sdf
  69. * @return
  70. */
  71. public static Date str2Date(String str, SimpleDateFormat sdf) {
  72. if (null == str || "".equals(str)) {
  73. return null;
  74. }
  75. Date date = null;
  76. try {
  77. date = sdf.parse(str);
  78. return date;
  79. } catch (ParseException e) {
  80. e.printStackTrace();
  81. }
  82. return null;
  83. }
  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. *
  117. * @param format 日期格式
  118. * @return 字符串
  119. */
  120. public static String getDate(String format) {
  121. Date date = new Date();
  122. if (null == date) {
  123. return null;
  124. }
  125. SimpleDateFormat sdf = new SimpleDateFormat(format);
  126. return sdf.format(date);
  127. }
  128. /**
  129. * 指定毫秒数的时间戳
  130. *
  131. * @param millis 毫秒数
  132. * @return 指定毫秒数的时间戳
  133. */
  134. public static Timestamp getTimestamp(long millis) {
  135. return new Timestamp(millis);
  136. }
  137. /**
  138. * 以字符形式表示的时间戳
  139. *
  140. * @param time 毫秒数
  141. * @return 以字符形式表示的时间戳
  142. */
  143. public static Timestamp getTimestamp(String time) {
  144. return new Timestamp(Long.parseLong(time));
  145. }
  146. /**
  147. * 系统当前的时间戳
  148. *
  149. * @return 系统当前的时间戳
  150. */
  151. public static Timestamp getTimestamp() {
  152. return new Timestamp(System.currentTimeMillis());
  153. }
  154. /**
  155. * 当前时间,格式 yyyy-MM-dd HH:mm:ss
  156. *
  157. * @return 当前时间的标准形式字符串
  158. */
  159. public static String now() {
  160. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  161. return datetimeFormat.format(getCalendar().getTime());
  162. }
  163. /**
  164. * 指定日期的时间戳
  165. *
  166. * @param date 指定日期
  167. * @return 指定日期的时间戳
  168. */
  169. public static Timestamp getTimestamp(Date date) {
  170. return new Timestamp(date.getTime());
  171. }
  172. /**
  173. * 指定日历的时间戳
  174. *
  175. * @param cal 指定日历
  176. * @return 指定日历的时间戳
  177. */
  178. public static Timestamp getCalendarTimestamp(Calendar cal) {
  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.getTime().getTime();
  208. }
  209. /**
  210. * 指定日期的毫秒数
  211. *
  212. * @param date 指定日期
  213. * @return 指定日期的毫秒数
  214. */
  215. public static long getMillis(Date date) {
  216. return date.getTime();
  217. }
  218. /**
  219. * 指定时间戳的毫秒数
  220. *
  221. * @param ts 指定时间戳
  222. * @return 指定时间戳的毫秒数
  223. */
  224. public static long getMillis(Timestamp ts) {
  225. return ts.getTime();
  226. }
  227. // ////////////////////////////////////////////////////////////////////////////
  228. // formatDate
  229. // 将日期按照一定的格式转化为字符串
  230. // ////////////////////////////////////////////////////////////////////////////
  231. /**
  232. * 默认方式表示的系统当前日期,具体格式:年-月-日
  233. *
  234. * @return 默认日期按“年-月-日“格式显示
  235. */
  236. public static String formatDate() {
  237. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  238. return format.format(getCalendar().getTime());
  239. }
  240. /**
  241. * 默认方式表示的系统当前日期,具体格式:yyyy-MM-dd HH:mm:ss
  242. *
  243. * @return 默认日期按“yyyy-MM-dd HH:mm:ss“格式显示
  244. */
  245. public static String formatDateTime() {
  246. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  247. return datetimeFormat.format(getCalendar().getTime());
  248. }
  249. /**
  250. * 指定日期的默认显示,具体格式:年-月-日
  251. *
  252. * @param cal 指定的日期
  253. * @return 指定日期按“年-月-日“格式显示
  254. */
  255. public static String formatDate(Calendar cal) {
  256. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  257. return format.format(cal.getTime());
  258. }
  259. /**
  260. * 指定日期的默认显示,具体格式:年-月-日
  261. *
  262. * @param date 指定的日期
  263. * @return 指定日期按“年-月-日“格式显示
  264. */
  265. public static String formatDate(Date date) {
  266. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  267. return format.format(date);
  268. }
  269. /**
  270. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日
  271. *
  272. * @param millis 指定的毫秒数
  273. * @return 指定毫秒数表示日期按“年-月-日“格式显示
  274. */
  275. public static String formatDate(long millis) {
  276. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  277. return format.format(new Date(millis));
  278. }
  279. /**
  280. * 默认日期按指定格式显示
  281. *
  282. * @param pattern 指定的格式
  283. * @return 默认日期按指定格式显示
  284. */
  285. public static String formatDate(String pattern) {
  286. return getDateFormat(pattern).format(getCalendar().getTime());
  287. }
  288. /**
  289. * 指定日期按指定格式显示
  290. *
  291. * @param cal 指定的日期
  292. * @param pattern 指定的格式
  293. * @return 指定日期按指定格式显示
  294. */
  295. public static String formatDate(Calendar cal, String pattern) {
  296. return getDateFormat(pattern).format(cal.getTime());
  297. }
  298. /**
  299. * 指定日期按指定格式显示
  300. *
  301. * @param date 指定的日期
  302. * @param pattern 指定的格式
  303. * @return 指定日期按指定格式显示
  304. */
  305. public static String formatDate(Date date, String pattern) {
  306. return getDateFormat(pattern).format(date);
  307. }
  308. // ////////////////////////////////////////////////////////////////////////////
  309. // formatTime
  310. // 将日期按照一定的格式转化为字符串
  311. // ////////////////////////////////////////////////////////////////////////////
  312. /**
  313. * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分
  314. *
  315. * @return 默认日期按“年-月-日 时:分“格式显示
  316. */
  317. public static String formatTime() {
  318. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  319. return timeSdf.format(getCalendar().getTime());
  320. }
  321. /**
  322. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日 时:分
  323. *
  324. * @param millis 指定的毫秒数
  325. * @return 指定毫秒数表示日期按“年-月-日 时:分“格式显示
  326. */
  327. public static String formatTime(long millis) {
  328. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  329. return timeSdf.format(new Date(millis));
  330. }
  331. /**
  332. * 指定日期的默认显示,具体格式:年-月-日 时:分
  333. *
  334. * @param cal 指定的日期
  335. * @return 指定日期按“年-月-日 时:分“格式显示
  336. */
  337. public static String formatTime(Calendar cal) {
  338. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  339. return timeSdf.format(cal.getTime());
  340. }
  341. /**
  342. * 指定日期的默认显示,具体格式:年-月-日 时:分
  343. *
  344. * @param date 指定的日期
  345. * @return 指定日期按“年-月-日 时:分“格式显示
  346. */
  347. public static String formatTime(Date date) {
  348. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  349. return timeSdf.format(date);
  350. }
  351. // ////////////////////////////////////////////////////////////////////////////
  352. // formatShortTime
  353. // 将日期按照一定的格式转化为字符串
  354. // ////////////////////////////////////////////////////////////////////////////
  355. /**
  356. * 默认方式表示的系统当前日期,具体格式:时:分
  357. *
  358. * @return 默认日期按“时:分“格式显示
  359. */
  360. public static String formatShortTime() {
  361. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  362. return shortTimeSdf.format(getCalendar().getTime());
  363. }
  364. /**
  365. * 指定毫秒数表示日期的默认显示,具体格式:时:分
  366. *
  367. * @param millis 指定的毫秒数
  368. * @return 指定毫秒数表示日期按“时:分“格式显示
  369. */
  370. public static String formatShortTime(long millis) {
  371. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  372. return shortTimeSdf.format(new Date(millis));
  373. }
  374. /**
  375. * 指定日期的默认显示,具体格式:时:分
  376. *
  377. * @param cal 指定的日期
  378. * @return 指定日期按“时:分“格式显示
  379. */
  380. public static String formatShortTime(Calendar cal) {
  381. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  382. return shortTimeSdf.format(cal.getTime());
  383. }
  384. /**
  385. * 指定日期的默认显示,具体格式:时:分
  386. *
  387. * @param date 指定的日期
  388. * @return 指定日期按“时:分“格式显示
  389. */
  390. public static String formatShortTime(Date date) {
  391. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  392. return shortTimeSdf.format(date);
  393. }
  394. // ////////////////////////////////////////////////////////////////////////////
  395. // parseDate
  396. // parseCalendar
  397. // parseTimestamp
  398. // 将字符串按照一定的格式转化为日期或时间
  399. // ////////////////////////////////////////////////////////////////////////////
  400. /**
  401. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  402. *
  403. * @param src 将要转换的原始字符窜
  404. * @param pattern 转换的匹配格式
  405. * @return 如果转换成功则返回转换后的日期
  406. * @throws ParseException
  407. * @throws
  408. */
  409. public static Date parseDate(String src, String pattern) throws ParseException {
  410. return getDateFormat(pattern).parse(src);
  411. }
  412. /**
  413. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  414. *
  415. * @param src 将要转换的原始字符窜
  416. * @param pattern 转换的匹配格式
  417. * @return 如果转换成功则返回转换后的日期
  418. * @throws ParseException
  419. * @throws
  420. */
  421. public static Calendar parseCalendar(String src, String pattern) throws ParseException {
  422. Date date = parseDate(src, pattern);
  423. Calendar cal = Calendar.getInstance();
  424. cal.setTime(date);
  425. return cal;
  426. }
  427. public static String formatAddDate(String src, String pattern, int amount) throws ParseException {
  428. Calendar cal;
  429. cal = parseCalendar(src, pattern);
  430. cal.add(Calendar.DATE, amount);
  431. return formatDate(cal);
  432. }
  433. /**
  434. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  435. *
  436. * @param src 将要转换的原始字符窜
  437. * @param pattern 转换的匹配格式
  438. * @return 如果转换成功则返回转换后的时间戳
  439. * @throws ParseException
  440. * @throws
  441. */
  442. public static Timestamp parseTimestamp(String src, String pattern) throws ParseException {
  443. Date date = parseDate(src, pattern);
  444. return new Timestamp(date.getTime());
  445. }
  446. // ////////////////////////////////////////////////////////////////////////////
  447. // dateDiff
  448. // 计算两个日期之间的差值
  449. // ////////////////////////////////////////////////////////////////////////////
  450. /**
  451. * 计算两个时间之间的差值,根据标志的不同而不同
  452. *
  453. * @param flag 计算标志,表示按照年/月/日/时/分/秒等计算
  454. * @param calSrc 减数
  455. * @param calDes 被减数
  456. * @return 两个日期之间的差值
  457. */
  458. public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) {
  459. long millisDiff = getMillis(calSrc) - getMillis(calDes);
  460. if (flag == 'y') {
  461. return (calSrc.get(Calendar.YEAR) - calDes.get(Calendar.YEAR));
  462. }
  463. if (flag == 'd') {
  464. return (int) (millisDiff / DAY_IN_MILLIS);
  465. }
  466. if (flag == 'h') {
  467. return (int) (millisDiff / HOUR_IN_MILLIS);
  468. }
  469. if (flag == 'm') {
  470. return (int) (millisDiff / MINUTE_IN_MILLIS);
  471. }
  472. if (flag == 's') {
  473. return (int) (millisDiff / SECOND_IN_MILLIS);
  474. }
  475. return 0;
  476. }
  477. /**
  478. * String类型 转换为Date, 如果参数长度为10 转换格式”yyyy-MM-dd“ 如果参数长度为19 转换格式”yyyy-MM-dd
  479. * HH:mm:ss“ * @param text String类型的时间值
  480. */
  481. @Override
  482. public void setAsText(String text) throws IllegalArgumentException {
  483. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  484. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  485. if (StringUtils.hasText(text)) {
  486. try {
  487. if (text.indexOf(":") == -1 && text.length() == 10) {
  488. setValue(format.parse(text));
  489. } else if (text.indexOf(":") > 0 && text.length() == 19) {
  490. setValue(datetimeFormat.parse(text));
  491. } else {
  492. throw new IllegalArgumentException("Could not parse date, date format is error ");
  493. }
  494. } catch (ParseException ex) {
  495. IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
  496. iae.initCause(ex);
  497. throw iae;
  498. }
  499. } else {
  500. setValue(null);
  501. }
  502. }
  503. public static int getYear() {
  504. GregorianCalendar calendar = new GregorianCalendar();
  505. calendar.setTime(getDate());
  506. return calendar.get(Calendar.YEAR);
  507. }
  508. public static Date timeStampToDate(Long time) {
  509. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  510. Date date;
  511. try {
  512. date = sdf.parse(sdf.format(time));
  513. return date;
  514. } catch (ParseException e) {
  515. e.printStackTrace();
  516. return null;
  517. }
  518. }
  519. public static List<Map<String, String>> getDateDayRange(String startDate, String endDate) throws ParseException {
  520. List<Map<String, String>> list = new ArrayList<>();
  521. String splitDate = DateUtils.addDay(endDate, -1);
  522. splitDate = DateUtils.addDay(splitDate, 1);
  523. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  524. Map<String, String> map = new HashMap<String, String>();
  525. map.put("startDate", startDate);
  526. map.put("endDate", endDate);
  527. list.add(map);
  528. } else {
  529. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  530. Map<String, String> map = new HashMap<String, String>();
  531. map.put("startDate", splitDate);
  532. map.put("endDate", endDate);
  533. list.add(map);
  534. splitDate = DateUtils.addDay(splitDate, -1);
  535. endDate = DateUtils.addDay(endDate, -1);
  536. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  537. splitDate = startDate;
  538. }
  539. }
  540. }
  541. return list;
  542. }
  543. public static List<Map<String, String>> getDateWeekRange(String startDate, String endDate) throws ParseException {
  544. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  545. String splitDate = DateUtils.addWeek(endDate, -1);
  546. splitDate = DateUtils.addDay(splitDate, 1);
  547. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  548. Map<String, String> map = new HashMap<String, String>();
  549. map.put("startDate", startDate);
  550. map.put("endDate", endDate);
  551. list.add(map);
  552. } else {
  553. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  554. Map<String, String> map = new HashMap<String, String>();
  555. map.put("startDate", splitDate);
  556. map.put("endDate", endDate);
  557. list.add(map);
  558. splitDate = DateUtils.addWeek(splitDate, -1);
  559. endDate = DateUtils.addWeek(endDate, -1);
  560. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  561. splitDate = startDate;
  562. }
  563. }
  564. }
  565. return list;
  566. }
  567. public static List<Map<String, String>> getDateMonthRange(String startDate, String endDate) throws ParseException {
  568. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  569. String splitDate = DateUtils.addMonth(endDate, -1);
  570. splitDate = DateUtils.addDay(splitDate, 1);
  571. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  572. Map<String, String> map = new HashMap<String, String>();
  573. map.put("startDate", startDate);
  574. map.put("endDate", endDate);
  575. list.add(map);
  576. } else {
  577. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  578. Map<String, String> map = new HashMap<String, String>();
  579. map.put("startDate", splitDate);
  580. map.put("endDate", endDate);
  581. list.add(map);
  582. splitDate = DateUtils.addMonth(splitDate, -1);
  583. endDate = DateUtils.addMonth(endDate, -1);
  584. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  585. splitDate = startDate;
  586. }
  587. }
  588. }
  589. return list;
  590. }
  591. public static String addMonth(String dateString, int month) throws ParseException {
  592. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  593. Date date = sdf.parse(dateString);
  594. Calendar calendar = Calendar.getInstance();
  595. calendar.setTime(date);
  596. calendar.add(Calendar.MONTH, month);
  597. return sdf.format(calendar.getTime());
  598. }
  599. public static String addWeek(String dateString, int week) throws ParseException {
  600. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  601. Date date = sdf.parse(dateString);
  602. Calendar calendar = Calendar.getInstance();
  603. calendar.setTime(date);
  604. calendar.add(Calendar.WEEK_OF_YEAR, week);
  605. return sdf.format(calendar.getTime());
  606. }
  607. public static String addDay(String dateString, int day) throws ParseException {
  608. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  609. Date date = sdf.parse(dateString);
  610. Calendar calendar = Calendar.getInstance();
  611. calendar.setTime(date);
  612. calendar.add(Calendar.DAY_OF_YEAR, day);
  613. return sdf.format(calendar.getTime());
  614. }
  615. public static Date addDay(Date date, int day) {
  616. Calendar calendar = Calendar.getInstance();
  617. calendar.setTime(date);
  618. calendar.add(Calendar.DAY_OF_YEAR, day);
  619. return calendar.getTime();
  620. }
  621. public static Date addSecond(Date date, int seconds) {
  622. Calendar calendar = Calendar.getInstance();
  623. calendar.setTime(date);
  624. calendar.add(Calendar.SECOND, seconds);
  625. return calendar.getTime();
  626. }
  627. public static int compareDate(String firstDateString, String secondDateString) throws ParseException {
  628. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  629. Date firstDate = sdf.parse(firstDateString);
  630. Date secondDate = sdf.parse(secondDateString);
  631. long first = firstDate.getTime();
  632. long second = secondDate.getTime();
  633. return first == second ? 0 : (first > second ? 1 : -1);
  634. }
  635. public static String timeStamp2Date(Timestamp timeLong) {
  636. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  637. Date date;
  638. try {
  639. date = sdf.parse(sdf.format(timeLong));
  640. return sdf.format(date);
  641. } catch (ParseException e) {
  642. e.printStackTrace();
  643. return null;
  644. }
  645. }
  646. public static String timeStamp2Date(long currentTimeMillis) {
  647. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  648. Date date;
  649. try {
  650. date = sdf.parse(sdf.format(currentTimeMillis));
  651. return sdf.format(date);
  652. } catch (ParseException e) {
  653. e.printStackTrace();
  654. return null;
  655. }
  656. }
  657. public static Map<String, Object> getBeforeDate() {
  658. Date dNow = new Date();
  659. Date dBefore = new Date();
  660. Calendar calendar = Calendar.getInstance();
  661. calendar.setTime(dNow);
  662. calendar.add(Calendar.DAY_OF_MONTH, -1);
  663. dBefore = calendar.getTime();
  664. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  665. String defaultStartDate = sdf.format(dBefore);
  666. defaultStartDate = defaultStartDate + " 00:00:00";
  667. String defaultEndDate = defaultStartDate.substring(0, 10) + " 23:59:59";
  668. Map<String, Object> map = new HashMap<>();
  669. map.put("startDate", defaultStartDate);
  670. map.put("endDate", defaultEndDate);
  671. return map;
  672. }
  673. /**
  674. * @param format 返回日期格式
  675. * @param date 传入的初始日期
  676. * @param num 天数
  677. * @return
  678. * @throws ParseException
  679. */
  680. public static String getAnotherDay(String format, String date, Integer num) throws ParseException {
  681. SimpleDateFormat sdf = new SimpleDateFormat(format);
  682. Date date_ = sdf.parse(date);
  683. Calendar calendar = Calendar.getInstance();
  684. calendar.setTime(date_);
  685. calendar.add(Calendar.DAY_OF_MONTH, num);
  686. Date resultDate = calendar.getTime();
  687. return sdf.format(resultDate);
  688. }
  689. public static String getNowDate(String format) {
  690. SimpleDateFormat sdf = new SimpleDateFormat(format);
  691. Date date = new Date();
  692. return sdf.format(date);
  693. }
  694. public static String getMonthBefore(String format, String nowTime, int amount) throws ParseException {
  695. // 获取当前时间
  696. SimpleDateFormat dateFormat = new SimpleDateFormat(format);
  697. Date date = dateFormat.parse(nowTime);
  698. //得到日历
  699. Calendar calendar = Calendar.getInstance();
  700. //把当前时间赋给日历
  701. calendar.setTime(date);
  702. //设置为前2月,可根据需求进行修改
  703. calendar.add(Calendar.MONTH, amount);
  704. //获取2个月前的时间
  705. date = calendar.getTime();
  706. return dateFormat.format(date);
  707. }
  708. public static Map<String, Object> compare_date(String format, String DATE1, String DATE2) {
  709. DateFormat df = new SimpleDateFormat(format);
  710. Map<String, Object> map = new HashMap<>();
  711. try {
  712. Date dt1 = df.parse(DATE1);
  713. Date dt2 = df.parse(DATE2);
  714. if (dt1.getTime() > dt2.getTime()) {
  715. map.put("bigDate", DATE1);
  716. map.put("smallDate", DATE2);
  717. return map;
  718. } else if (dt1.getTime() < dt2.getTime()) {
  719. map.put("bigDate", DATE2);
  720. map.put("smallDate", DATE1);
  721. return map;
  722. }
  723. } catch (Exception exception) {
  724. exception.printStackTrace();
  725. }
  726. return null;
  727. }
  728. /**
  729. * 日期中获取年份
  730. *
  731. * @param format
  732. * @param date
  733. * @return
  734. * @throws ParseException
  735. */
  736. public static String getYear(String format, String date) throws ParseException {
  737. SimpleDateFormat df = new SimpleDateFormat(format);
  738. Date parse = df.parse(date);
  739. return String.format("%tY", parse);
  740. }
  741. /**
  742. * 日期中获取月份
  743. *
  744. * @param format
  745. * @param date
  746. * @return
  747. * @throws ParseException
  748. */
  749. public static String getMonth(String format, String date) throws ParseException {
  750. SimpleDateFormat df = new SimpleDateFormat(format);
  751. Date parse = df.parse(date);
  752. return String.format("%tm", parse);
  753. }
  754. /**
  755. * 日期中获取天
  756. *
  757. * @param format
  758. * @param date
  759. * @return
  760. * @throws ParseException
  761. */
  762. public static String getDay(String format, String date) throws ParseException {
  763. SimpleDateFormat df = new SimpleDateFormat(format);
  764. Date parse = df.parse(date);
  765. return String.format("%td", parse);
  766. }
  767. }