DateUtils.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. public static Map<String, Object> getStartEndTime(String createTime) throws ParseException {
  478. SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  479. Date parse = smf.parse(createTime);
  480. Calendar calendar = Calendar.getInstance();
  481. calendar.setTime(parse);
  482. calendar.set(Calendar.HOUR_OF_DAY, 0);
  483. calendar.set(Calendar.MINUTE, 0);
  484. calendar.set(Calendar.SECOND, 0);
  485. Date start = calendar.getTime();
  486. calendar.add(Calendar.DAY_OF_MONTH, 1);
  487. calendar.add(Calendar.SECOND, -1);
  488. Date end = calendar.getTime();
  489. Map<String, Object> map = new HashMap<>();
  490. map.put("start", smf.format(start));
  491. map.put("end", smf.format(end));
  492. return map;
  493. }
  494. public static String getQuarterStartDate(Date date) {
  495. Calendar calendar = new GregorianCalendar();
  496. calendar.setTime(date);
  497. Integer month = calendar.get(Calendar.MONTH);
  498. Integer compare = month % 3;
  499. calendar.add(Calendar.MONTH, -compare);
  500. calendar.set(Calendar.DAY_OF_MONTH, 1);
  501. Date getDate = calendar.getTime();
  502. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  503. return dateFormat.format(getDate);
  504. }
  505. public static String getQuarterEndDate(Date date) {
  506. Calendar calendar = new GregorianCalendar();
  507. calendar.setTime(date);
  508. Integer month = calendar.get(Calendar.MONTH);
  509. Integer compare = month % 3;
  510. calendar.add(Calendar.MONTH, (3 - compare));
  511. calendar.set(Calendar.DAY_OF_MONTH, 1);
  512. Date getDate = calendar.getTime();
  513. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  514. return dateFormat.format(getDate);
  515. }
  516. /**
  517. * String类型 转换为Date, 如果参数长度为10 转换格式”yyyy-MM-dd“ 如果参数长度为19 转换格式”yyyy-MM-dd
  518. * HH:mm:ss“ * @param text String类型的时间值
  519. */
  520. @Override
  521. public void setAsText(String text) throws IllegalArgumentException {
  522. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  523. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  524. if (StringUtils.hasText(text)) {
  525. try {
  526. if (text.indexOf(":") == -1 && text.length() == 10) {
  527. setValue(format.parse(text));
  528. } else if (text.indexOf(":") > 0 && text.length() == 19) {
  529. setValue(datetimeFormat.parse(text));
  530. } else {
  531. throw new IllegalArgumentException("Could not parse date, date format is error ");
  532. }
  533. } catch (ParseException ex) {
  534. IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
  535. iae.initCause(ex);
  536. throw iae;
  537. }
  538. } else {
  539. setValue(null);
  540. }
  541. }
  542. public static int getYear(Date date) {
  543. GregorianCalendar calendar = new GregorianCalendar();
  544. calendar.setTime(date);
  545. return calendar.get(Calendar.YEAR);
  546. }
  547. public static Date timeStampToDate(Long time) {
  548. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  549. Date date;
  550. try {
  551. date = sdf.parse(sdf.format(time));
  552. return date;
  553. } catch (ParseException e) {
  554. e.printStackTrace();
  555. return null;
  556. }
  557. }
  558. public static List<Map<String, String>> getDateDayRange(String startDate, String endDate) throws ParseException {
  559. List<Map<String, String>> list = new ArrayList<>();
  560. String splitDate = DateUtils.addDay(endDate, -1);
  561. splitDate = DateUtils.addDay(splitDate, 1);
  562. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  563. Map<String, String> map = new HashMap<String, String>();
  564. map.put("startDate", startDate);
  565. map.put("endDate", endDate);
  566. list.add(map);
  567. } else {
  568. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  569. Map<String, String> map = new HashMap<String, String>();
  570. map.put("startDate", splitDate);
  571. map.put("endDate", endDate);
  572. list.add(map);
  573. splitDate = DateUtils.addDay(splitDate, -1);
  574. endDate = DateUtils.addDay(endDate, -1);
  575. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  576. splitDate = startDate;
  577. }
  578. }
  579. }
  580. return list;
  581. }
  582. public static List<Map<String, String>> getDateWeekRange(String startDate, String endDate) throws ParseException {
  583. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  584. String splitDate = DateUtils.addWeek(endDate, -1);
  585. splitDate = DateUtils.addDay(splitDate, 1);
  586. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  587. Map<String, String> map = new HashMap<String, String>();
  588. map.put("startDate", startDate);
  589. map.put("endDate", endDate);
  590. list.add(map);
  591. } else {
  592. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  593. Map<String, String> map = new HashMap<String, String>();
  594. map.put("startDate", splitDate);
  595. map.put("endDate", endDate);
  596. list.add(map);
  597. splitDate = DateUtils.addWeek(splitDate, -1);
  598. endDate = DateUtils.addWeek(endDate, -1);
  599. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  600. splitDate = startDate;
  601. }
  602. }
  603. }
  604. return list;
  605. }
  606. public static List<Map<String, String>> getDateMonthRange(String startDate, String endDate) throws ParseException {
  607. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  608. String splitDate = DateUtils.addMonth(endDate, -1);
  609. splitDate = DateUtils.addDay(splitDate, 1);
  610. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  611. Map<String, String> map = new HashMap<String, String>();
  612. map.put("startDate", startDate);
  613. map.put("endDate", endDate);
  614. list.add(map);
  615. } else {
  616. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  617. Map<String, String> map = new HashMap<String, String>();
  618. map.put("startDate", splitDate);
  619. map.put("endDate", endDate);
  620. list.add(map);
  621. splitDate = DateUtils.addMonth(splitDate, -1);
  622. endDate = DateUtils.addMonth(endDate, -1);
  623. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  624. splitDate = startDate;
  625. }
  626. }
  627. }
  628. return list;
  629. }
  630. public static String addMonth(String dateString, int month) throws ParseException {
  631. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  632. Date date = sdf.parse(dateString);
  633. Calendar calendar = Calendar.getInstance();
  634. calendar.setTime(date);
  635. calendar.add(Calendar.MONTH, month);
  636. return sdf.format(calendar.getTime());
  637. }
  638. public static String addWeek(String dateString, int week) throws ParseException {
  639. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  640. Date date = sdf.parse(dateString);
  641. Calendar calendar = Calendar.getInstance();
  642. calendar.setTime(date);
  643. calendar.add(Calendar.WEEK_OF_YEAR, week);
  644. return sdf.format(calendar.getTime());
  645. }
  646. public static String addDay(String dateString, int day) throws ParseException {
  647. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  648. Date date = sdf.parse(dateString);
  649. Calendar calendar = Calendar.getInstance();
  650. calendar.setTime(date);
  651. calendar.add(Calendar.DAY_OF_YEAR, day);
  652. return sdf.format(calendar.getTime());
  653. }
  654. public static Date addDay(Date date, int day) {
  655. Calendar calendar = Calendar.getInstance();
  656. calendar.setTime(date);
  657. calendar.add(Calendar.DAY_OF_YEAR, day);
  658. return calendar.getTime();
  659. }
  660. public static Date addSecond(Date date, int seconds) {
  661. Calendar calendar = Calendar.getInstance();
  662. calendar.setTime(date);
  663. calendar.add(Calendar.SECOND, seconds);
  664. return calendar.getTime();
  665. }
  666. public static int compareDate(String firstDateString, String secondDateString) throws ParseException {
  667. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  668. Date firstDate = sdf.parse(firstDateString);
  669. Date secondDate = sdf.parse(secondDateString);
  670. long first = firstDate.getTime();
  671. long second = secondDate.getTime();
  672. return first == second ? 0 : (first > second ? 1 : -1);
  673. }
  674. public static String timeStamp2Date(Timestamp timeLong) {
  675. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  676. Date date;
  677. try {
  678. date = sdf.parse(sdf.format(timeLong));
  679. return sdf.format(date);
  680. } catch (ParseException e) {
  681. e.printStackTrace();
  682. return null;
  683. }
  684. }
  685. public static String timeStamp2Date(long currentTimeMillis) {
  686. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  687. Date date;
  688. try {
  689. date = sdf.parse(sdf.format(currentTimeMillis));
  690. return sdf.format(date);
  691. } catch (ParseException e) {
  692. e.printStackTrace();
  693. return null;
  694. }
  695. }
  696. public static Map<String, Object> getBeforeDate() {
  697. Date dNow = new Date();
  698. Date dBefore = new Date();
  699. Calendar calendar = Calendar.getInstance();
  700. calendar.setTime(dNow);
  701. calendar.add(Calendar.DAY_OF_MONTH, -1);
  702. dBefore = calendar.getTime();
  703. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  704. String defaultStartDate = sdf.format(dBefore);
  705. defaultStartDate = defaultStartDate + " 00:00:00";
  706. String defaultEndDate = defaultStartDate.substring(0, 10) + " 23:59:59";
  707. Map<String, Object> map = new HashMap<>();
  708. map.put("startDate", defaultStartDate);
  709. map.put("endDate", defaultEndDate);
  710. return map;
  711. }
  712. /**
  713. * @param format 返回日期格式
  714. * @param date 传入的初始日期
  715. * @param num 天数
  716. * @return
  717. * @throws ParseException
  718. */
  719. public static String getAnotherDay(String format, String date, Integer num) throws ParseException {
  720. SimpleDateFormat sdf = new SimpleDateFormat(format);
  721. Date getDate = sdf.parse(date);
  722. Calendar calendar = Calendar.getInstance();
  723. calendar.setTime(getDate);
  724. calendar.add(Calendar.DAY_OF_MONTH, num);
  725. Date resultDate = calendar.getTime();
  726. return sdf.format(resultDate);
  727. }
  728. public static String getNowDate(String format) {
  729. SimpleDateFormat sdf = new SimpleDateFormat(format);
  730. Date date = new Date();
  731. return sdf.format(date);
  732. }
  733. public static String getMonthBefore(String format, String nowTime, int amount) throws ParseException {
  734. // 获取当前时间
  735. SimpleDateFormat dateFormat = new SimpleDateFormat(format);
  736. Date date = dateFormat.parse(nowTime);
  737. //得到日历
  738. Calendar calendar = Calendar.getInstance();
  739. //把当前时间赋给日历
  740. calendar.setTime(date);
  741. //设置为前2月,可根据需求进行修改
  742. calendar.add(Calendar.MONTH, amount);
  743. //获取2个月前的时间
  744. date = calendar.getTime();
  745. return dateFormat.format(date);
  746. }
  747. public static Map<String, Object> compareDate(String format, String date1, String date2) {
  748. DateFormat df = new SimpleDateFormat(format);
  749. Map<String, Object> map = new HashMap<>();
  750. try {
  751. Date dt1 = df.parse(date1);
  752. Date dt2 = df.parse(date2);
  753. if (dt1.getTime() > dt2.getTime()) {
  754. map.put("bigDate", date1);
  755. map.put("smallDate", date2);
  756. return map;
  757. } else if (dt1.getTime() < dt2.getTime()) {
  758. map.put("bigDate", date2);
  759. map.put("smallDate", date1);
  760. return map;
  761. }
  762. } catch (Exception exception) {
  763. exception.printStackTrace();
  764. }
  765. return null;
  766. }
  767. /**
  768. * 日期中获取年份
  769. *
  770. * @param format
  771. * @param date
  772. * @return
  773. * @throws ParseException
  774. */
  775. public static String getYear(String format, String date) throws ParseException {
  776. SimpleDateFormat df = new SimpleDateFormat(format);
  777. Date parse = df.parse(date);
  778. return String.format("%tY", parse);
  779. }
  780. /**
  781. * 日期中获取月份
  782. *
  783. * @param format
  784. * @param date
  785. * @return
  786. * @throws ParseException
  787. */
  788. public static String getMonth(String format, String date) throws ParseException {
  789. SimpleDateFormat df = new SimpleDateFormat(format);
  790. Date parse = df.parse(date);
  791. return String.format("%tm", parse);
  792. }
  793. /**
  794. * 日期中获取天
  795. *
  796. * @param format
  797. * @param date
  798. * @return
  799. * @throws ParseException
  800. */
  801. public static String getDay(String format, String date) throws ParseException {
  802. SimpleDateFormat df = new SimpleDateFormat(format);
  803. Date parse = df.parse(date);
  804. return String.format("%td", parse);
  805. }
  806. /**
  807. * 根据时间获取季度
  808. * 1即Q1(1,2,3月),以此类推
  809. * @param date
  810. * @return
  811. */
  812. public static int getQuarter(Date date){
  813. Calendar cal = Calendar.getInstance();
  814. cal.setTime(date);
  815. //获取当前月份
  816. int month = cal.get(Calendar.MONDAY+1);
  817. if(month<=3){
  818. return 1;
  819. }else if(month <=6){
  820. return 2;
  821. }else if(month<=9){
  822. return 3;
  823. }else if(month<=12){
  824. return 4;
  825. }
  826. return 0;
  827. }
  828. }