DateUtils.java 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. public static String getDateByDateStr(String dateStr) throws ParseException {
  25. SimpleDateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd");
  26. Date parse = dsdf.parse(dateStr);
  27. String day = dsdf.format(parse);
  28. return day;
  29. }
  30. public static Integer getHour(String dateStr) throws ParseException {
  31. DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  32. Calendar cal = Calendar.getInstance();
  33. Date date = sdf.parse(dateStr);
  34. cal.setTime(date);
  35. Integer hour = cal.get(Calendar.HOUR_OF_DAY);
  36. return hour;
  37. }
  38. /**
  39. * 指定模式的时间格式
  40. *
  41. * @param pattern
  42. * @return
  43. */
  44. private static SimpleDateFormat getDateFormat(String pattern) {
  45. return new SimpleDateFormat(pattern);
  46. }
  47. /**
  48. * 当前日历,这里用中国时间表示
  49. *
  50. * @return 以当地时区表示的系统当前日历
  51. */
  52. public static Calendar getCalendar() {
  53. return Calendar.getInstance();
  54. }
  55. /**
  56. * 指定毫秒数表示的日历
  57. *
  58. * @param millis 毫秒数
  59. * @return 指定毫秒数表示的日历
  60. */
  61. public static Calendar getCalendar(long millis) {
  62. Calendar cal = Calendar.getInstance();
  63. cal.setTime(new Date(millis));
  64. return cal;
  65. }
  66. // ////////////////////////////////////////////////////////////////////////////
  67. // getDate
  68. // 各种方式获取的Date
  69. // ////////////////////////////////////////////////////////////////////////////
  70. /**
  71. * 当前日期
  72. *
  73. * @return 系统当前时间
  74. */
  75. public static Date getDate() {
  76. return new Date();
  77. }
  78. /**
  79. * 字符串转换成日期
  80. *
  81. * @param str
  82. * @param sdf
  83. * @return
  84. */
  85. public static Date str2Date(String str, SimpleDateFormat sdf) {
  86. if (null == str || "".equals(str)) {
  87. return null;
  88. }
  89. Date date = null;
  90. try {
  91. date = sdf.parse(str);
  92. return date;
  93. } catch (ParseException e) {
  94. e.printStackTrace();
  95. }
  96. return null;
  97. }
  98. /**
  99. * 日期转换为字符串
  100. *
  101. * @return 字符串
  102. */
  103. public static String date2Str() {
  104. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  105. Date date = getDate();
  106. if (null == date) {
  107. return null;
  108. }
  109. return format.format(date);
  110. }
  111. /**
  112. * 格式化时间
  113. *
  114. * @param date
  115. * @param format
  116. * @return
  117. */
  118. public static String dateformat(String date, String format) {
  119. SimpleDateFormat sformat = new SimpleDateFormat(format);
  120. Date getDate = null;
  121. try {
  122. getDate = sformat.parse(date);
  123. } catch (ParseException e) {
  124. e.printStackTrace();
  125. }
  126. return sformat.format(getDate);
  127. }
  128. /**
  129. * 日期转换为字符串
  130. *
  131. * @param format 日期格式
  132. * @return 字符串
  133. */
  134. public static String getDate(String format) {
  135. Date date = new Date();
  136. if (null == date) {
  137. return null;
  138. }
  139. SimpleDateFormat sdf = new SimpleDateFormat(format);
  140. return sdf.format(date);
  141. }
  142. /**
  143. * 指定毫秒数的时间戳
  144. *
  145. * @param millis 毫秒数
  146. * @return 指定毫秒数的时间戳
  147. */
  148. public static Timestamp getTimestamp(long millis) {
  149. return new Timestamp(millis);
  150. }
  151. /**
  152. * 以字符形式表示的时间戳
  153. *
  154. * @param time 毫秒数
  155. * @return 以字符形式表示的时间戳
  156. */
  157. public static Timestamp getTimestamp(String time) {
  158. return new Timestamp(Long.parseLong(time));
  159. }
  160. /**
  161. * 系统当前的时间戳
  162. *
  163. * @return 系统当前的时间戳
  164. */
  165. public static Timestamp getTimestamp() {
  166. return new Timestamp(System.currentTimeMillis());
  167. }
  168. /**
  169. * 当前时间,格式 yyyy-MM-dd HH:mm:ss
  170. *
  171. * @return 当前时间的标准形式字符串
  172. */
  173. public static String now() {
  174. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  175. return datetimeFormat.format(getCalendar().getTime());
  176. }
  177. /**
  178. * 指定日期的时间戳
  179. *
  180. * @param date 指定日期
  181. * @return 指定日期的时间戳
  182. */
  183. public static Timestamp getTimestamp(Date date) {
  184. return new Timestamp(date.getTime());
  185. }
  186. /**
  187. * 指定日历的时间戳
  188. *
  189. * @param cal 指定日历
  190. * @return 指定日历的时间戳
  191. */
  192. public static Timestamp getCalendarTimestamp(Calendar cal) {
  193. return new Timestamp(cal.getTime().getTime());
  194. }
  195. public static Timestamp gettimestamp() {
  196. Date dt = new Date();
  197. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  198. String nowTime = df.format(dt);
  199. java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime);
  200. return buydate;
  201. }
  202. // ////////////////////////////////////////////////////////////////////////////
  203. // getMillis
  204. // 各种方式获取的Millis
  205. // ////////////////////////////////////////////////////////////////////////////
  206. /**
  207. * 系统时间的毫秒数
  208. *
  209. * @return 系统时间的毫秒数
  210. */
  211. public static long getMillis() {
  212. return System.currentTimeMillis();
  213. }
  214. /**
  215. * 指定日历的毫秒数
  216. *
  217. * @param cal 指定日历
  218. * @return 指定日历的毫秒数
  219. */
  220. public static long getMillis(Calendar cal) {
  221. return cal.getTime().getTime();
  222. }
  223. /**
  224. * 指定日期的毫秒数
  225. *
  226. * @param date 指定日期
  227. * @return 指定日期的毫秒数
  228. */
  229. public static long getMillis(Date date) {
  230. return date.getTime();
  231. }
  232. /**
  233. * 指定时间戳的毫秒数
  234. *
  235. * @param ts 指定时间戳
  236. * @return 指定时间戳的毫秒数
  237. */
  238. public static long getMillis(Timestamp ts) {
  239. return ts.getTime();
  240. }
  241. // ////////////////////////////////////////////////////////////////////////////
  242. // formatDate
  243. // 将日期按照一定的格式转化为字符串
  244. // ////////////////////////////////////////////////////////////////////////////
  245. /**
  246. * 默认方式表示的系统当前日期,具体格式:年-月-日
  247. *
  248. * @return 默认日期按“年-月-日“格式显示
  249. */
  250. public static String formatDate() {
  251. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  252. return format.format(getCalendar().getTime());
  253. }
  254. /**
  255. * 默认方式表示的系统当前日期,具体格式:yyyy-MM-dd HH:mm:ss
  256. *
  257. * @return 默认日期按“yyyy-MM-dd HH:mm:ss“格式显示
  258. */
  259. public static String formatDateTime() {
  260. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  261. return datetimeFormat.format(getCalendar().getTime());
  262. }
  263. /**
  264. * 指定日期的默认显示,具体格式:年-月-日
  265. *
  266. * @param cal 指定的日期
  267. * @return 指定日期按“年-月-日“格式显示
  268. */
  269. public static String formatDate(Calendar cal) {
  270. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  271. return format.format(cal.getTime());
  272. }
  273. /**
  274. * 指定日期的默认显示,具体格式:年-月-日
  275. *
  276. * @param date 指定的日期
  277. * @return 指定日期按“年-月-日“格式显示
  278. */
  279. public static String formatDate(Date date) {
  280. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  281. return format.format(date);
  282. }
  283. /**
  284. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日
  285. *
  286. * @param millis 指定的毫秒数
  287. * @return 指定毫秒数表示日期按“年-月-日“格式显示
  288. */
  289. public static String formatDate(long millis) {
  290. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  291. return format.format(new Date(millis));
  292. }
  293. /**
  294. * 默认日期按指定格式显示
  295. *
  296. * @param pattern 指定的格式
  297. * @return 默认日期按指定格式显示
  298. */
  299. public static String formatDate(String pattern) {
  300. return getDateFormat(pattern).format(getCalendar().getTime());
  301. }
  302. /**
  303. * 指定日期按指定格式显示
  304. *
  305. * @param cal 指定的日期
  306. * @param pattern 指定的格式
  307. * @return 指定日期按指定格式显示
  308. */
  309. public static String formatDate(Calendar cal, String pattern) {
  310. return getDateFormat(pattern).format(cal.getTime());
  311. }
  312. /**
  313. * 指定日期按指定格式显示
  314. *
  315. * @param date 指定的日期
  316. * @param pattern 指定的格式
  317. * @return 指定日期按指定格式显示
  318. */
  319. public static String formatDate(Date date, String pattern) {
  320. return getDateFormat(pattern).format(date);
  321. }
  322. // ////////////////////////////////////////////////////////////////////////////
  323. // formatTime
  324. // 将日期按照一定的格式转化为字符串
  325. // ////////////////////////////////////////////////////////////////////////////
  326. /**
  327. * 默认方式表示的系统当前日期,具体格式:年-月-日 时:分
  328. *
  329. * @return 默认日期按“年-月-日 时:分“格式显示
  330. */
  331. public static String formatTime() {
  332. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  333. return timeSdf.format(getCalendar().getTime());
  334. }
  335. /**
  336. * 指定毫秒数表示日期的默认显示,具体格式:年-月-日 时:分
  337. *
  338. * @param millis 指定的毫秒数
  339. * @return 指定毫秒数表示日期按“年-月-日 时:分“格式显示
  340. */
  341. public static String formatTime(long millis) {
  342. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  343. return timeSdf.format(new Date(millis));
  344. }
  345. /**
  346. * 指定日期的默认显示,具体格式:年-月-日 时:分
  347. *
  348. * @param cal 指定的日期
  349. * @return 指定日期按“年-月-日 时:分“格式显示
  350. */
  351. public static String formatTime(Calendar cal) {
  352. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  353. return timeSdf.format(cal.getTime());
  354. }
  355. /**
  356. * 指定日期的默认显示,具体格式:年-月-日 时:分
  357. *
  358. * @param date 指定的日期
  359. * @return 指定日期按“年-月-日 时:分“格式显示
  360. */
  361. public static String formatTime(Date date) {
  362. SimpleDateFormat timeSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  363. return timeSdf.format(date);
  364. }
  365. // ////////////////////////////////////////////////////////////////////////////
  366. // formatShortTime
  367. // 将日期按照一定的格式转化为字符串
  368. // ////////////////////////////////////////////////////////////////////////////
  369. /**
  370. * 默认方式表示的系统当前日期,具体格式:时:分
  371. *
  372. * @return 默认日期按“时:分“格式显示
  373. */
  374. public static String formatShortTime() {
  375. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  376. return shortTimeSdf.format(getCalendar().getTime());
  377. }
  378. /**
  379. * 指定毫秒数表示日期的默认显示,具体格式:时:分
  380. *
  381. * @param millis 指定的毫秒数
  382. * @return 指定毫秒数表示日期按“时:分“格式显示
  383. */
  384. public static String formatShortTime(long millis) {
  385. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  386. return shortTimeSdf.format(new Date(millis));
  387. }
  388. /**
  389. * 指定日期的默认显示,具体格式:时:分
  390. *
  391. * @param cal 指定的日期
  392. * @return 指定日期按“时:分“格式显示
  393. */
  394. public static String formatShortTime(Calendar cal) {
  395. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  396. return shortTimeSdf.format(cal.getTime());
  397. }
  398. /**
  399. * 指定日期的默认显示,具体格式:时:分
  400. *
  401. * @param date 指定的日期
  402. * @return 指定日期按“时:分“格式显示
  403. */
  404. public static String formatShortTime(Date date) {
  405. SimpleDateFormat shortTimeSdf = new SimpleDateFormat("HH:mm");
  406. return shortTimeSdf.format(date);
  407. }
  408. // ////////////////////////////////////////////////////////////////////////////
  409. // parseDate
  410. // parseCalendar
  411. // parseTimestamp
  412. // 将字符串按照一定的格式转化为日期或时间
  413. // ////////////////////////////////////////////////////////////////////////////
  414. /**
  415. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  416. *
  417. * @param src 将要转换的原始字符窜
  418. * @param pattern 转换的匹配格式
  419. * @return 如果转换成功则返回转换后的日期
  420. * @throws ParseException
  421. * @throws
  422. */
  423. public static Date parseDate(String src, String pattern) throws ParseException {
  424. return getDateFormat(pattern).parse(src);
  425. }
  426. /**
  427. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  428. *
  429. * @param src 将要转换的原始字符窜
  430. * @param pattern 转换的匹配格式
  431. * @return 如果转换成功则返回转换后的日期
  432. * @throws ParseException
  433. * @throws
  434. */
  435. public static Calendar parseCalendar(String src, String pattern) throws ParseException {
  436. Date date = parseDate(src, pattern);
  437. Calendar cal = Calendar.getInstance();
  438. cal.setTime(date);
  439. return cal;
  440. }
  441. public static String formatAddDate(String src, String pattern, int amount) throws ParseException {
  442. Calendar cal;
  443. cal = parseCalendar(src, pattern);
  444. cal.add(Calendar.DATE, amount);
  445. return formatDate(cal);
  446. }
  447. /**
  448. * 根据指定的格式将字符串转换成Date 如输入:2003-11-19 11:20:20将按照这个转成时间
  449. *
  450. * @param src 将要转换的原始字符窜
  451. * @param pattern 转换的匹配格式
  452. * @return 如果转换成功则返回转换后的时间戳
  453. * @throws ParseException
  454. * @throws
  455. */
  456. public static Timestamp parseTimestamp(String src, String pattern) throws ParseException {
  457. Date date = parseDate(src, pattern);
  458. return new Timestamp(date.getTime());
  459. }
  460. // ////////////////////////////////////////////////////////////////////////////
  461. // dateDiff
  462. // 计算两个日期之间的差值
  463. // ////////////////////////////////////////////////////////////////////////////
  464. /**
  465. * 计算两个时间之间的差值,根据标志的不同而不同
  466. *
  467. * @param flag 计算标志,表示按照年/月/日/时/分/秒等计算
  468. * @param calSrc 减数
  469. * @param calDes 被减数
  470. * @return 两个日期之间的差值
  471. */
  472. public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) {
  473. long millisDiff = getMillis(calSrc) - getMillis(calDes);
  474. if (flag == 'y') {
  475. return (calSrc.get(Calendar.YEAR) - calDes.get(Calendar.YEAR));
  476. }
  477. if (flag == 'd') {
  478. return (int) (millisDiff / DAY_IN_MILLIS);
  479. }
  480. if (flag == 'h') {
  481. return (int) (millisDiff / HOUR_IN_MILLIS);
  482. }
  483. if (flag == 'm') {
  484. return (int) (millisDiff / MINUTE_IN_MILLIS);
  485. }
  486. if (flag == 's') {
  487. return (int) (millisDiff / SECOND_IN_MILLIS);
  488. }
  489. return 0;
  490. }
  491. public static Map<String, Object> getStartEndTime(String createTime) throws ParseException {
  492. SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  493. Date parse = smf.parse(createTime);
  494. Calendar calendar = Calendar.getInstance();
  495. calendar.setTime(parse);
  496. calendar.set(Calendar.HOUR_OF_DAY, 0);
  497. calendar.set(Calendar.MINUTE, 0);
  498. calendar.set(Calendar.SECOND, 0);
  499. Date start = calendar.getTime();
  500. calendar.add(Calendar.DAY_OF_MONTH, 1);
  501. calendar.add(Calendar.SECOND, -1);
  502. Date end = calendar.getTime();
  503. Map<String, Object> map = new HashMap<>();
  504. map.put("start", smf.format(start));
  505. map.put("end", smf.format(end));
  506. return map;
  507. }
  508. public static String getQuarterStartDate(Date date) {
  509. Calendar calendar = new GregorianCalendar();
  510. calendar.setTime(date);
  511. Integer month = calendar.get(Calendar.MONTH);
  512. Integer compare = month % 3;
  513. calendar.add(Calendar.MONTH, -compare);
  514. calendar.set(Calendar.DAY_OF_MONTH, 1);
  515. Date getDate = calendar.getTime();
  516. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  517. return dateFormat.format(getDate);
  518. }
  519. public static String getQuarterEndDate(Date date) {
  520. Calendar calendar = new GregorianCalendar();
  521. calendar.setTime(date);
  522. Integer month = calendar.get(Calendar.MONTH);
  523. Integer compare = month % 3;
  524. calendar.add(Calendar.MONTH, (3 - compare));
  525. calendar.set(Calendar.DAY_OF_MONTH, 1);
  526. Date getDate = calendar.getTime();
  527. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  528. return dateFormat.format(getDate);
  529. }
  530. /**
  531. * String类型 转换为Date, 如果参数长度为10 转换格式”yyyy-MM-dd“ 如果参数长度为19 转换格式”yyyy-MM-dd
  532. * HH:mm:ss“ * @param text String类型的时间值
  533. */
  534. @Override
  535. public void setAsText(String text) throws IllegalArgumentException {
  536. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  537. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  538. if (StringUtils.hasText(text)) {
  539. try {
  540. if (text.indexOf(":") == -1 && text.length() == 10) {
  541. setValue(format.parse(text));
  542. } else if (text.indexOf(":") > 0 && text.length() == 19) {
  543. setValue(datetimeFormat.parse(text));
  544. } else {
  545. throw new IllegalArgumentException("Could not parse date, date format is error ");
  546. }
  547. } catch (ParseException ex) {
  548. IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
  549. iae.initCause(ex);
  550. throw iae;
  551. }
  552. } else {
  553. setValue(null);
  554. }
  555. }
  556. public static int getYear(Date date) {
  557. GregorianCalendar calendar = new GregorianCalendar();
  558. calendar.setTime(date);
  559. return calendar.get(Calendar.YEAR);
  560. }
  561. public static Date timeStampToDate(Long time) {
  562. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  563. Date date;
  564. try {
  565. date = sdf.parse(sdf.format(time));
  566. return date;
  567. } catch (ParseException e) {
  568. e.printStackTrace();
  569. return null;
  570. }
  571. }
  572. public static List<Map<String, String>> getDateDayRange(String startDate, String endDate) throws ParseException {
  573. List<Map<String, String>> list = new ArrayList<>();
  574. String splitDate = DateUtils.addDay(endDate, -1);
  575. splitDate = DateUtils.addDay(splitDate, 1);
  576. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  577. Map<String, String> map = new HashMap<String, String>();
  578. map.put("startDate", startDate);
  579. map.put("endDate", endDate);
  580. list.add(map);
  581. } else {
  582. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  583. Map<String, String> map = new HashMap<String, String>();
  584. map.put("startDate", splitDate);
  585. map.put("endDate", endDate);
  586. list.add(map);
  587. splitDate = DateUtils.addDay(splitDate, -1);
  588. endDate = DateUtils.addDay(endDate, -1);
  589. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  590. splitDate = startDate;
  591. }
  592. }
  593. }
  594. return list;
  595. }
  596. public static List<Map<String, String>> getDateWeekRange(String startDate, String endDate) throws ParseException {
  597. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  598. String splitDate = DateUtils.addWeek(endDate, -1);
  599. splitDate = DateUtils.addDay(splitDate, 1);
  600. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  601. Map<String, String> map = new HashMap<String, String>();
  602. map.put("startDate", startDate);
  603. map.put("endDate", endDate);
  604. list.add(map);
  605. } else {
  606. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  607. Map<String, String> map = new HashMap<String, String>();
  608. map.put("startDate", splitDate);
  609. map.put("endDate", endDate);
  610. list.add(map);
  611. splitDate = DateUtils.addWeek(splitDate, -1);
  612. endDate = DateUtils.addWeek(endDate, -1);
  613. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  614. splitDate = startDate;
  615. }
  616. }
  617. }
  618. return list;
  619. }
  620. public static List<Map<String, String>> getDateMonthRange(String startDate, String endDate) throws ParseException {
  621. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  622. String splitDate = DateUtils.addMonth(endDate, -1);
  623. splitDate = DateUtils.addDay(splitDate, 1);
  624. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  625. Map<String, String> map = new HashMap<String, String>();
  626. map.put("startDate", startDate);
  627. map.put("endDate", endDate);
  628. list.add(map);
  629. } else {
  630. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  631. Map<String, String> map = new HashMap<String, String>();
  632. map.put("startDate", splitDate);
  633. map.put("endDate", endDate);
  634. list.add(map);
  635. splitDate = DateUtils.addMonth(splitDate, -1);
  636. endDate = DateUtils.addMonth(endDate, -1);
  637. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  638. splitDate = startDate;
  639. }
  640. }
  641. }
  642. return list;
  643. }
  644. public static String addMonth(String dateString, int month) throws ParseException {
  645. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  646. Date date = sdf.parse(dateString);
  647. Calendar calendar = Calendar.getInstance();
  648. calendar.setTime(date);
  649. calendar.add(Calendar.MONTH, month);
  650. return sdf.format(calendar.getTime());
  651. }
  652. public static String addWeek(String dateString, int week) throws ParseException {
  653. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  654. Date date = sdf.parse(dateString);
  655. Calendar calendar = Calendar.getInstance();
  656. calendar.setTime(date);
  657. calendar.add(Calendar.WEEK_OF_YEAR, week);
  658. return sdf.format(calendar.getTime());
  659. }
  660. public static String addDay(String dateString, int day) throws ParseException {
  661. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  662. Date date = sdf.parse(dateString);
  663. Calendar calendar = Calendar.getInstance();
  664. calendar.setTime(date);
  665. calendar.add(Calendar.DAY_OF_YEAR, day);
  666. return sdf.format(calendar.getTime());
  667. }
  668. public static Date addDay(Date date, int day) {
  669. Calendar calendar = Calendar.getInstance();
  670. calendar.setTime(date);
  671. calendar.add(Calendar.DAY_OF_YEAR, day);
  672. return calendar.getTime();
  673. }
  674. public static Date addSecond(Date date, int seconds) {
  675. Calendar calendar = Calendar.getInstance();
  676. calendar.setTime(date);
  677. calendar.add(Calendar.SECOND, seconds);
  678. return calendar.getTime();
  679. }
  680. public static int compareDate(String firstDateString, String secondDateString) throws ParseException {
  681. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  682. Date firstDate = sdf.parse(firstDateString);
  683. Date secondDate = sdf.parse(secondDateString);
  684. long first = firstDate.getTime();
  685. long second = secondDate.getTime();
  686. return first == second ? 0 : (first > second ? 1 : -1);
  687. }
  688. public static String timeStamp2Date(Timestamp timeLong) {
  689. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  690. Date date;
  691. try {
  692. date = sdf.parse(sdf.format(timeLong));
  693. return sdf.format(date);
  694. } catch (ParseException e) {
  695. e.printStackTrace();
  696. return null;
  697. }
  698. }
  699. public static String timeStamp2Date(long currentTimeMillis) {
  700. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  701. Date date;
  702. try {
  703. date = sdf.parse(sdf.format(currentTimeMillis));
  704. return sdf.format(date);
  705. } catch (ParseException e) {
  706. e.printStackTrace();
  707. return null;
  708. }
  709. }
  710. public static Map<String, Object> getBeforeDate() {
  711. Date dNow = new Date();
  712. Date dBefore = new Date();
  713. Calendar calendar = Calendar.getInstance();
  714. calendar.setTime(dNow);
  715. calendar.add(Calendar.DAY_OF_MONTH, -1);
  716. dBefore = calendar.getTime();
  717. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  718. String defaultStartDate = sdf.format(dBefore);
  719. defaultStartDate = defaultStartDate + " 00:00:00";
  720. String defaultEndDate = defaultStartDate.substring(0, 10) + " 23:59:59";
  721. Map<String, Object> map = new HashMap<>();
  722. map.put("startDate", defaultStartDate);
  723. map.put("endDate", defaultEndDate);
  724. return map;
  725. }
  726. /**
  727. * @param format 返回日期格式
  728. * @param date 传入的初始日期
  729. * @param num 天数
  730. * @return
  731. * @throws ParseException
  732. */
  733. public static String getAnotherDay(String format, String date, Integer num) throws ParseException {
  734. SimpleDateFormat sdf = new SimpleDateFormat(format);
  735. Date getDate = sdf.parse(date);
  736. Calendar calendar = Calendar.getInstance();
  737. calendar.setTime(getDate);
  738. calendar.add(Calendar.DAY_OF_MONTH, num);
  739. Date resultDate = calendar.getTime();
  740. return sdf.format(resultDate);
  741. }
  742. public static String getNowDate(String format) {
  743. SimpleDateFormat sdf = new SimpleDateFormat(format);
  744. Date date = new Date();
  745. return sdf.format(date);
  746. }
  747. public static String getMonthBefore(String format, String nowTime, int amount) throws ParseException {
  748. // 获取当前时间
  749. SimpleDateFormat dateFormat = new SimpleDateFormat(format);
  750. Date date = dateFormat.parse(nowTime);
  751. //得到日历
  752. Calendar calendar = Calendar.getInstance();
  753. //把当前时间赋给日历
  754. calendar.setTime(date);
  755. //设置为前2月,可根据需求进行修改
  756. calendar.add(Calendar.MONTH, amount);
  757. //获取2个月前的时间
  758. date = calendar.getTime();
  759. return dateFormat.format(date);
  760. }
  761. public static Map<String, Object> compareDate(String format, String date1, String date2) {
  762. DateFormat df = new SimpleDateFormat(format);
  763. Map<String, Object> map = new HashMap<>();
  764. try {
  765. Date dt1 = df.parse(date1);
  766. Date dt2 = df.parse(date2);
  767. if (dt1.getTime() > dt2.getTime()) {
  768. map.put("bigDate", date1);
  769. map.put("smallDate", date2);
  770. return map;
  771. } else if (dt1.getTime() < dt2.getTime()) {
  772. map.put("bigDate", date2);
  773. map.put("smallDate", date1);
  774. return map;
  775. }
  776. } catch (Exception exception) {
  777. exception.printStackTrace();
  778. }
  779. return null;
  780. }
  781. /**
  782. * 日期中获取年份
  783. *
  784. * @param format
  785. * @param date
  786. * @return
  787. * @throws ParseException
  788. */
  789. public static String getYear(String format, String date) throws ParseException {
  790. SimpleDateFormat df = new SimpleDateFormat(format);
  791. Date parse = df.parse(date);
  792. return String.format("%tY", parse);
  793. }
  794. /**
  795. * 日期中获取月份
  796. *
  797. * @param format
  798. * @param date
  799. * @return
  800. * @throws ParseException
  801. */
  802. public static String getMonth(String format, String date) throws ParseException {
  803. SimpleDateFormat df = new SimpleDateFormat(format);
  804. Date parse = df.parse(date);
  805. return String.format("%tm", parse);
  806. }
  807. /**
  808. * 日期中获取天
  809. *
  810. * @param format
  811. * @param date
  812. * @return
  813. * @throws ParseException
  814. */
  815. public static String getDay(String format, String date) throws ParseException {
  816. SimpleDateFormat df = new SimpleDateFormat(format);
  817. Date parse = df.parse(date);
  818. return String.format("%td", parse);
  819. }
  820. /**
  821. * 根据时间获取季度
  822. * 1即Q1(1,2,3月),以此类推
  823. * @param date
  824. * @return
  825. */
  826. public static int getQuarter(Date date) {
  827. Calendar cal = Calendar.getInstance();
  828. cal.setTime(date);
  829. //获取当前月份
  830. int month = cal.get(Calendar.MONTH) + 1;
  831. if (month <= 3) {
  832. return 1;
  833. } else if (month <= 6) {
  834. return 2;
  835. } else if (month <= 9) {
  836. return 3;
  837. } else if (month <= 12) {
  838. return 4;
  839. }
  840. return 0;
  841. }
  842. /**
  843. * 获取【所在周】周五的时间(周五为一周第一天,周四为一周最后一天)
  844. * @param date
  845. */
  846. public static Date getFriday(Date date) {
  847. Calendar calendar = new GregorianCalendar();
  848. calendar.setTime(date);
  849. calendar.setFirstDayOfWeek(Calendar.FRIDAY);
  850. calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  851. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  852. System.out.println(df.format(calendar.getTime()));
  853. return calendar.getTime();
  854. }
  855. /**
  856. * 获取传入时间的年、月份和季度(周五为每周第一天,周四为每周最后一天)
  857. * 获取的是所属年、月、季度,因为很多月份的第一周或最后 一周会包括上一个月或者下一个月的部分日期
  858. */
  859. public static Map<String, Integer> getYearQuarter(Date date) {
  860. Map<String, Integer> dateMap = new HashMap<>();
  861. Calendar calendar = new GregorianCalendar();
  862. calendar.setTime(date);
  863. int originYear = calendar.get(Calendar.YEAR);
  864. int originMonth = calendar.get(Calendar.MONTH);
  865. int originQuarter = DateUtils.getQuarter(date); //本月所在季度(按照日期来的)
  866. Calendar calendar1 = new GregorianCalendar();
  867. calendar1.setTime(date);
  868. calendar1.setFirstDayOfWeek(Calendar.FRIDAY);
  869. //1.判断当天所在周第一天是否跨月
  870. calendar1.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  871. int fridayYear = calendar1.get(Calendar.YEAR);
  872. int fridayMonth = calendar1.get(Calendar.MONTH);
  873. int fridayQuarter = DateUtils.getQuarter(calendar1.getTime());
  874. //2.判断当天所在周的最后一天是否跨月
  875. Calendar calendar2 = new GregorianCalendar();
  876. calendar2.setTime(date);
  877. calendar2.setFirstDayOfWeek(Calendar.FRIDAY);
  878. calendar2.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  879. int thursdayYear = calendar2.get(Calendar.YEAR);
  880. int thursdayMonth = calendar2.get(Calendar.MONTH);
  881. int thursdayQuarter = DateUtils.getQuarter(calendar2.getTime());
  882. //如果本周第一天(周五)和当前天不在一个月内,说明周跨月了
  883. if (fridayMonth == originMonth && thursdayMonth == originMonth) {
  884. dateMap.put("year", originYear);
  885. dateMap.put("month", originMonth + 1); //最后一个月是11,应该+1
  886. dateMap.put("quarter", originQuarter);
  887. return dateMap;
  888. } else if (fridayYear < originYear || (fridayYear == originYear && fridayMonth < originMonth)) {
  889. //判断本周有几天在本月,如果是(5,6,7)则就是本月本季度
  890. //int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  891. calendar1.set(Calendar.DATE, calendar1.getActualMaximum(Calendar.DATE));
  892. int lastDateWeek = calendar1.get(Calendar.DAY_OF_WEEK);
  893. //判断如果本周所在上个月的最后一天是周五、周六、周日的话,说明本周归属于本月(周五是第一天);否则本周归属于上个季度
  894. if (lastDateWeek == 6 || lastDateWeek == 7 || lastDateWeek == 1) {
  895. dateMap.put("year", originYear);
  896. dateMap.put("month", originMonth + 1);
  897. dateMap.put("quarter", originQuarter);
  898. } else {
  899. //判断如果本周属于上个月,判断是否跨年
  900. if (originYear == fridayYear) {
  901. dateMap.put("year", originYear);
  902. dateMap.put("month", originMonth);
  903. //判断是否跨季
  904. if (originQuarter == fridayQuarter) {
  905. dateMap.put("quarter", originQuarter);
  906. } else {
  907. dateMap.put("quarter", fridayQuarter);
  908. }
  909. } else {
  910. //跨年的话说明这周上年的12月(第四季度)
  911. dateMap.put("year", originYear - 1);
  912. dateMap.put("month", 12); //??查看是11,还是12
  913. dateMap.put("quarter", 4);
  914. }
  915. }
  916. return dateMap;
  917. } else if (originYear < thursdayYear || (originYear == thursdayYear && originMonth < thursdayMonth)) {
  918. calendar2.set(Calendar.DATE, 1);
  919. int firstDateWeek = calendar2.get(Calendar.DAY_OF_WEEK);
  920. //如果跨月的第一天是周二周三周四的话说明这周不跨月,否在进入下个月(季度、年需要再判断)
  921. if (firstDateWeek == 3 || firstDateWeek == 4 || firstDateWeek == 5) {
  922. dateMap.put("year", originYear);
  923. dateMap.put("month", originMonth + 1);
  924. dateMap.put("quarter", originQuarter);
  925. } else {
  926. if (originYear == thursdayYear) {
  927. dateMap.put("year", originYear);
  928. dateMap.put("month", originMonth + 1 + 1);
  929. //判断是否跨季
  930. if (originQuarter == thursdayQuarter) {
  931. dateMap.put("quarter", originQuarter);
  932. } else {
  933. dateMap.put("quarter", thursdayQuarter);
  934. }
  935. } else {
  936. //跨年的话说明这周属于明年的1月(第一季度)
  937. dateMap.put("year", originYear + 1);
  938. dateMap.put("month", 1);
  939. dateMap.put("quarter", 1);
  940. }
  941. }
  942. return dateMap;
  943. }
  944. return dateMap;
  945. }
  946. /**
  947. * 计算季度开始、结束时间
  948. * 原则:周五为本周的第一天,下一周的周四为本周的最后一天
  949. * @param year
  950. * @param quarter
  951. */
  952. public static Map<String, String> quarterStartEndDate(int year, int quarter) {
  953. Map<String, String> quarterStartEndDateMap = new HashMap<>();
  954. Date startDate = null;
  955. Date endDate = null;
  956. int startMonth = 0;
  957. int endMonth = 0;
  958. if (quarter == 1) {
  959. startMonth = 1;
  960. endMonth = 3;
  961. } else if (quarter == 2) {
  962. startMonth = 4;
  963. endMonth = 6;
  964. } else if (quarter == 3) {
  965. startMonth = 7;
  966. endMonth = 9;
  967. } else {
  968. startMonth = 10;
  969. endMonth = 12;
  970. }
  971. //判断季度第一天的时间(以周为单位,有可能是上个月,也有可能是本月的非1号)
  972. Calendar calendar1 = new GregorianCalendar();
  973. calendar1.set(Calendar.YEAR, year);
  974. calendar1.set(Calendar.MONTH, startMonth - 1);
  975. calendar1.set(Calendar.DATE, 1);
  976. calendar1.setFirstDayOfWeek(Calendar.FRIDAY);
  977. //如果季度第一个月的第一天是周二周三周四,那么这周应该归属于上个季度,否则这周(包括上个月的时间)归属于这个季度
  978. int firstDateWeek = calendar1.get(Calendar.DAY_OF_WEEK);
  979. if (firstDateWeek == 5 || firstDateWeek == 3 || firstDateWeek == 4) {
  980. calendar1.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  981. startDate = DateUtils.addDay(calendar1.getTime(), 1);
  982. } else {
  983. calendar1.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  984. startDate = calendar1.getTime();
  985. }
  986. //判断季度最后一天的时间(以周为单位,有可能是下个月,也有可能是本月的非最后一天)
  987. Calendar calendar2 = new GregorianCalendar();
  988. calendar2.set(Calendar.YEAR, year);
  989. calendar2.set(Calendar.MONTH, endMonth - 1);
  990. calendar2.set(Calendar.DATE, 1);
  991. calendar2.setFirstDayOfWeek(Calendar.FRIDAY);
  992. //如果一个季度最后一个月最后一天是周五、周六、周日,则这个月最后一周属于下个季度
  993. calendar2.set(Calendar.DATE, calendar2.getActualMaximum(Calendar.DATE));
  994. int lastDateWeek = calendar2.get(Calendar.DAY_OF_WEEK);
  995. if (lastDateWeek == 1 || lastDateWeek == 7 || lastDateWeek == 6) {
  996. calendar2.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  997. endDate = DateUtils.addDay(calendar2.getTime(), -1);
  998. } else {
  999. calendar2.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  1000. endDate = calendar2.getTime();
  1001. }
  1002. DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  1003. quarterStartEndDateMap.put("startDate", sdf.format(startDate));
  1004. quarterStartEndDateMap.put("endDate", sdf.format(endDate));
  1005. return quarterStartEndDateMap;
  1006. }
  1007. /**
  1008. * 获取上个季度的年度和季度数
  1009. *
  1010. * @param date
  1011. * @return
  1012. */
  1013. public static Map<String, Integer> lastQuarter(Date date) {
  1014. Map<String, Integer> resultMap = new HashMap<>();
  1015. //获取当天所在
  1016. Map<String, Integer> yearQuarter = getYearQuarter(date);
  1017. Integer year = yearQuarter.get("year");
  1018. Integer quarter = yearQuarter.get("quarter");
  1019. Integer lastyear = 0;
  1020. Integer lastQuarter = 0;
  1021. if (quarter == 4 || quarter == 3 || quarter == 2) {
  1022. lastyear = year;
  1023. lastQuarter = quarter - 1;
  1024. } else {
  1025. lastyear = year - 1;
  1026. lastQuarter = 4;
  1027. }
  1028. resultMap.put("year", lastyear);
  1029. resultMap.put("quarter", lastQuarter);
  1030. return resultMap;
  1031. }
  1032. }