DateUtils.java 30 KB

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