DateUtils.java 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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 String getDateString(String createTime) throws ParseException {
  492. SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
  493. Date date = sdf1.parse(createTime);
  494. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  495. String str = sdf2.format(date);
  496. return str;
  497. }
  498. public static Map<String, Object> getStartEndTime(String createTime) throws ParseException {
  499. SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  500. Date parse = smf.parse(createTime);
  501. Calendar calendar = Calendar.getInstance();
  502. calendar.setTime(parse);
  503. calendar.set(Calendar.HOUR_OF_DAY, 0);
  504. calendar.set(Calendar.MINUTE, 0);
  505. calendar.set(Calendar.SECOND, 0);
  506. Date start = calendar.getTime();
  507. calendar.add(Calendar.DAY_OF_MONTH, 1);
  508. calendar.add(Calendar.SECOND, -1);
  509. Date end = calendar.getTime();
  510. Map<String, Object> map = new HashMap<>();
  511. map.put("start", smf.format(start));
  512. map.put("end", smf.format(end));
  513. return map;
  514. }
  515. public static String getQuarterStartDate(Date date) {
  516. Calendar calendar = new GregorianCalendar();
  517. calendar.setTime(date);
  518. Integer month = calendar.get(Calendar.MONTH);
  519. Integer compare = month % 3;
  520. calendar.add(Calendar.MONTH, -compare);
  521. calendar.set(Calendar.DAY_OF_MONTH, 1);
  522. Date getDate = calendar.getTime();
  523. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  524. return dateFormat.format(getDate);
  525. }
  526. public static String getQuarterEndDate(Date date) {
  527. Calendar calendar = new GregorianCalendar();
  528. calendar.setTime(date);
  529. Integer month = calendar.get(Calendar.MONTH);
  530. Integer compare = month % 3;
  531. calendar.add(Calendar.MONTH, (3 - compare));
  532. calendar.set(Calendar.DAY_OF_MONTH, 1);
  533. Date getDate = calendar.getTime();
  534. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  535. return dateFormat.format(getDate);
  536. }
  537. /**
  538. * String类型 转换为Date, 如果参数长度为10 转换格式”yyyy-MM-dd“ 如果参数长度为19 转换格式”yyyy-MM-dd
  539. * HH:mm:ss“ * @param text String类型的时间值
  540. */
  541. @Override
  542. public void setAsText(String text) throws IllegalArgumentException {
  543. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  544. SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  545. if (StringUtils.hasText(text)) {
  546. try {
  547. if (text.indexOf(":") == -1 && text.length() == 10) {
  548. setValue(format.parse(text));
  549. } else if (text.indexOf(":") > 0 && text.length() == 19) {
  550. setValue(datetimeFormat.parse(text));
  551. } else {
  552. throw new IllegalArgumentException("Could not parse date, date format is error ");
  553. }
  554. } catch (ParseException ex) {
  555. IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage());
  556. iae.initCause(ex);
  557. throw iae;
  558. }
  559. } else {
  560. setValue(null);
  561. }
  562. }
  563. public static int getYear(Date date) {
  564. GregorianCalendar calendar = new GregorianCalendar();
  565. calendar.setTime(date);
  566. return calendar.get(Calendar.YEAR);
  567. }
  568. public static Date timeStampToDate(Long time) {
  569. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  570. Date date;
  571. try {
  572. date = sdf.parse(sdf.format(time));
  573. return date;
  574. } catch (ParseException e) {
  575. e.printStackTrace();
  576. return null;
  577. }
  578. }
  579. public static List<Map<String, String>> getDateDayRange(String startDate, String endDate) throws ParseException {
  580. List<Map<String, String>> list = new ArrayList<>();
  581. String splitDate = DateUtils.addDay(endDate, -1);
  582. splitDate = DateUtils.addDay(splitDate, 1);
  583. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  584. Map<String, String> map = new HashMap<String, String>();
  585. map.put("startDate", startDate);
  586. map.put("endDate", endDate);
  587. list.add(map);
  588. } else {
  589. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  590. Map<String, String> map = new HashMap<String, String>();
  591. map.put("startDate", splitDate);
  592. map.put("endDate", endDate);
  593. list.add(map);
  594. splitDate = DateUtils.addDay(splitDate, -1);
  595. endDate = DateUtils.addDay(endDate, -1);
  596. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  597. splitDate = startDate;
  598. }
  599. }
  600. }
  601. return list;
  602. }
  603. public static List<Map<String, String>> getDateWeekRange(String startDate, String endDate) throws ParseException {
  604. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  605. String splitDate = DateUtils.addWeek(endDate, -1);
  606. splitDate = DateUtils.addDay(splitDate, 1);
  607. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  608. Map<String, String> map = new HashMap<String, String>();
  609. map.put("startDate", startDate);
  610. map.put("endDate", endDate);
  611. list.add(map);
  612. } else {
  613. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  614. Map<String, String> map = new HashMap<String, String>();
  615. map.put("startDate", splitDate);
  616. map.put("endDate", endDate);
  617. list.add(map);
  618. splitDate = DateUtils.addWeek(splitDate, -1);
  619. endDate = DateUtils.addWeek(endDate, -1);
  620. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  621. splitDate = startDate;
  622. }
  623. }
  624. }
  625. return list;
  626. }
  627. public static List<Map<String, String>> getDateMonthRange(String startDate, String endDate) throws ParseException {
  628. List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  629. String splitDate = DateUtils.addMonth(endDate, -1);
  630. splitDate = DateUtils.addDay(splitDate, 1);
  631. if (DateUtils.compareDate(startDate, splitDate) >= 0) {
  632. Map<String, String> map = new HashMap<String, String>();
  633. map.put("startDate", startDate);
  634. map.put("endDate", endDate);
  635. list.add(map);
  636. } else {
  637. while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) {
  638. Map<String, String> map = new HashMap<String, String>();
  639. map.put("startDate", splitDate);
  640. map.put("endDate", endDate);
  641. list.add(map);
  642. splitDate = DateUtils.addMonth(splitDate, -1);
  643. endDate = DateUtils.addMonth(endDate, -1);
  644. if (DateUtils.compareDate(startDate, splitDate) > 0) {
  645. splitDate = startDate;
  646. }
  647. }
  648. }
  649. return list;
  650. }
  651. public static String addMonth(String dateString, int month) throws ParseException {
  652. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  653. Date date = sdf.parse(dateString);
  654. Calendar calendar = Calendar.getInstance();
  655. calendar.setTime(date);
  656. calendar.add(Calendar.MONTH, month);
  657. return sdf.format(calendar.getTime());
  658. }
  659. public static String addWeek(String dateString, int week) throws ParseException {
  660. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  661. Date date = sdf.parse(dateString);
  662. Calendar calendar = Calendar.getInstance();
  663. calendar.setTime(date);
  664. calendar.add(Calendar.WEEK_OF_YEAR, week);
  665. return sdf.format(calendar.getTime());
  666. }
  667. public static String addDay(String dateString, int day) throws ParseException {
  668. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  669. Date date = sdf.parse(dateString);
  670. Calendar calendar = Calendar.getInstance();
  671. calendar.setTime(date);
  672. calendar.add(Calendar.DAY_OF_YEAR, day);
  673. return sdf.format(calendar.getTime());
  674. }
  675. public static Date addDay(Date date, int day) {
  676. Calendar calendar = Calendar.getInstance();
  677. calendar.setTime(date);
  678. calendar.add(Calendar.DAY_OF_YEAR, day);
  679. return calendar.getTime();
  680. }
  681. public static Date addSecond(Date date, int seconds) {
  682. Calendar calendar = Calendar.getInstance();
  683. calendar.setTime(date);
  684. calendar.add(Calendar.SECOND, seconds);
  685. return calendar.getTime();
  686. }
  687. public static int compareDate(String firstDateString, String secondDateString) throws ParseException {
  688. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  689. Date firstDate = sdf.parse(firstDateString);
  690. Date secondDate = sdf.parse(secondDateString);
  691. long first = firstDate.getTime();
  692. long second = secondDate.getTime();
  693. return first == second ? 0 : (first > second ? 1 : -1);
  694. }
  695. public static String timeStamp2Date(Timestamp timeLong) {
  696. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  697. Date date;
  698. try {
  699. date = sdf.parse(sdf.format(timeLong));
  700. return sdf.format(date);
  701. } catch (ParseException e) {
  702. e.printStackTrace();
  703. return null;
  704. }
  705. }
  706. public static String timeStamp2Date(long currentTimeMillis) {
  707. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  708. Date date;
  709. try {
  710. date = sdf.parse(sdf.format(currentTimeMillis));
  711. return sdf.format(date);
  712. } catch (ParseException e) {
  713. e.printStackTrace();
  714. return null;
  715. }
  716. }
  717. public static Map<String, Object> getBeforeDate() {
  718. Date dNow = new Date();
  719. Date dBefore = new Date();
  720. Calendar calendar = Calendar.getInstance();
  721. calendar.setTime(dNow);
  722. calendar.add(Calendar.DAY_OF_MONTH, -1);
  723. dBefore = calendar.getTime();
  724. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  725. String defaultStartDate = sdf.format(dBefore);
  726. defaultStartDate = defaultStartDate + " 00:00:00";
  727. String defaultEndDate = defaultStartDate.substring(0, 10) + " 23:59:59";
  728. Map<String, Object> map = new HashMap<>();
  729. map.put("startDate", defaultStartDate);
  730. map.put("endDate", defaultEndDate);
  731. return map;
  732. }
  733. /**
  734. * @param format 返回日期格式
  735. * @param date 传入的初始日期
  736. * @param num 天数
  737. * @return
  738. * @throws ParseException
  739. */
  740. public static String getAnotherDay(String format, String date, Integer num) {
  741. SimpleDateFormat sdf = new SimpleDateFormat(format);
  742. Date getDate = null;
  743. try {
  744. getDate = sdf.parse(date);
  745. } catch (ParseException e) {
  746. e.printStackTrace();
  747. }
  748. Calendar calendar = Calendar.getInstance();
  749. calendar.setTime(getDate);
  750. calendar.add(Calendar.DAY_OF_MONTH, num);
  751. Date resultDate = calendar.getTime();
  752. return sdf.format(resultDate);
  753. }
  754. public static String getNowDate(String format) {
  755. SimpleDateFormat sdf = new SimpleDateFormat(format);
  756. Date date = new Date();
  757. return sdf.format(date);
  758. }
  759. public static String getMonthBefore(String format, String nowTime, int amount) throws ParseException {
  760. // 获取当前时间
  761. SimpleDateFormat dateFormat = new SimpleDateFormat(format);
  762. Date date = dateFormat.parse(nowTime);
  763. //得到日历
  764. Calendar calendar = Calendar.getInstance();
  765. //把当前时间赋给日历
  766. calendar.setTime(date);
  767. //设置为前2月,可根据需求进行修改
  768. calendar.add(Calendar.MONTH, amount);
  769. //获取2个月前的时间
  770. date = calendar.getTime();
  771. return dateFormat.format(date);
  772. }
  773. public static Map<String, Object> compareDate(String format, String date1, String date2) {
  774. DateFormat df = new SimpleDateFormat(format);
  775. Map<String, Object> map = new HashMap<>();
  776. try {
  777. Date dt1 = df.parse(date1);
  778. Date dt2 = df.parse(date2);
  779. if (dt1.getTime() > dt2.getTime()) {
  780. map.put("bigDate", date1);
  781. map.put("smallDate", date2);
  782. return map;
  783. } else if (dt1.getTime() < dt2.getTime()) {
  784. map.put("bigDate", date2);
  785. map.put("smallDate", date1);
  786. return map;
  787. }
  788. } catch (Exception exception) {
  789. exception.printStackTrace();
  790. }
  791. return null;
  792. }
  793. /**
  794. * 日期中获取年份
  795. *
  796. * @param format
  797. * @param date
  798. * @return
  799. * @throws ParseException
  800. */
  801. public static String getYear(String format, String date) throws ParseException {
  802. SimpleDateFormat df = new SimpleDateFormat(format);
  803. Date parse = df.parse(date);
  804. return String.format("%tY", parse);
  805. }
  806. /**
  807. * 日期中获取月份
  808. *
  809. * @param format
  810. * @param date
  811. * @return
  812. * @throws ParseException
  813. */
  814. public static String getMonth(String format, String date) throws ParseException {
  815. SimpleDateFormat df = new SimpleDateFormat(format);
  816. Date parse = df.parse(date);
  817. return String.format("%tm", parse);
  818. }
  819. /**
  820. * 日期中获取天
  821. *
  822. * @param format
  823. * @param date
  824. * @return
  825. * @throws ParseException
  826. */
  827. public static String getDay(String format, String date) throws ParseException {
  828. SimpleDateFormat df = new SimpleDateFormat(format);
  829. Date parse = df.parse(date);
  830. return String.format("%td", parse);
  831. }
  832. /**
  833. * 根据时间获取季度
  834. * 1即Q1(1,2,3月),以此类推
  835. *
  836. * @param date
  837. * @return
  838. */
  839. public static int getQuarter(Date date) {
  840. Calendar cal = Calendar.getInstance();
  841. cal.setTime(date);
  842. //获取当前月份
  843. int month = cal.get(Calendar.MONTH) + 1;
  844. if (month <= 3) {
  845. return 1;
  846. } else if (month <= 6) {
  847. return 2;
  848. } else if (month <= 9) {
  849. return 3;
  850. } else if (month <= 12) {
  851. return 4;
  852. }
  853. return 0;
  854. }
  855. /**
  856. * 获取【所在周】周五的时间(周五为一周第一天,周四为一周最后一天)
  857. *
  858. * @param date
  859. */
  860. public static Date getFriday(Date date) {
  861. Calendar calendar = new GregorianCalendar();
  862. calendar.setTime(date);
  863. calendar.setFirstDayOfWeek(Calendar.FRIDAY);
  864. calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  865. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  866. System.out.println(df.format(calendar.getTime()));
  867. return calendar.getTime();
  868. }
  869. /**
  870. * 获取传入时间的年、月份和季度(周五为每周第一天,周四为每周最后一天)
  871. * 获取的是所属年、月、季度,因为很多月份的第一周或最后 一周会包括上一个月或者下一个月的部分日期
  872. */
  873. public static Map<String, Integer> getYearQuarter(Date date) {
  874. Map<String, Integer> dateMap = new HashMap<>();
  875. Calendar calendar = new GregorianCalendar();
  876. calendar.setTime(date);
  877. int originYear = calendar.get(Calendar.YEAR);
  878. int originMonth = calendar.get(Calendar.MONTH);
  879. int originQuarter = DateUtils.getQuarter(date); //本月所在季度(按照日期来的)
  880. Calendar calendar1 = new GregorianCalendar();
  881. calendar1.setTime(date);
  882. calendar1.setFirstDayOfWeek(Calendar.FRIDAY);
  883. //1.判断当天所在周第一天是否跨月
  884. calendar1.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  885. int fridayYear = calendar1.get(Calendar.YEAR);
  886. int fridayMonth = calendar1.get(Calendar.MONTH);
  887. int fridayQuarter = DateUtils.getQuarter(calendar1.getTime());
  888. //2.判断当天所在周的最后一天是否跨月
  889. Calendar calendar2 = new GregorianCalendar();
  890. calendar2.setTime(date);
  891. calendar2.setFirstDayOfWeek(Calendar.FRIDAY);
  892. calendar2.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  893. int thursdayYear = calendar2.get(Calendar.YEAR);
  894. int thursdayMonth = calendar2.get(Calendar.MONTH);
  895. int thursdayQuarter = DateUtils.getQuarter(calendar2.getTime());
  896. //如果本周第一天(周五)和当前天不在一个月内,说明周跨月了
  897. if (fridayMonth == originMonth && thursdayMonth == originMonth) {
  898. dateMap.put("year", originYear);
  899. dateMap.put("month", originMonth + 1); //最后一个月是11,应该+1
  900. dateMap.put("quarter", originQuarter);
  901. return dateMap;
  902. } else if (fridayYear < originYear || (fridayYear == originYear && fridayMonth < originMonth)) {
  903. //判断本周有几天在本月,如果是(5,6,7)则就是本月本季度
  904. //int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  905. calendar1.set(Calendar.DATE, calendar1.getActualMaximum(Calendar.DATE));
  906. int lastDateWeek = calendar1.get(Calendar.DAY_OF_WEEK);
  907. //判断如果本周所在上个月的最后一天是周五、周六、周日的话,说明本周归属于本月(周五是第一天);否则本周归属于上个季度
  908. if (lastDateWeek == 6 || lastDateWeek == 7 || lastDateWeek == 1) {
  909. dateMap.put("year", originYear);
  910. dateMap.put("month", originMonth + 1);
  911. dateMap.put("quarter", originQuarter);
  912. } else {
  913. //判断如果本周属于上个月,判断是否跨年
  914. if (originYear == fridayYear) {
  915. dateMap.put("year", originYear);
  916. dateMap.put("month", originMonth);
  917. //判断是否跨季
  918. if (originQuarter == fridayQuarter) {
  919. dateMap.put("quarter", originQuarter);
  920. } else {
  921. dateMap.put("quarter", fridayQuarter);
  922. }
  923. } else {
  924. //跨年的话说明这周上年的12月(第四季度)
  925. dateMap.put("year", originYear - 1);
  926. dateMap.put("month", 12); //??查看是11,还是12
  927. dateMap.put("quarter", 4);
  928. }
  929. }
  930. return dateMap;
  931. } else if (originYear < thursdayYear || (originYear == thursdayYear && originMonth < thursdayMonth)) {
  932. calendar2.set(Calendar.DATE, 1);
  933. int firstDateWeek = calendar2.get(Calendar.DAY_OF_WEEK);
  934. //如果跨月的第一天是周二周三周四的话说明这周不跨月,否在进入下个月(季度、年需要再判断)
  935. if (firstDateWeek == 3 || firstDateWeek == 4 || firstDateWeek == 5) {
  936. dateMap.put("year", originYear);
  937. dateMap.put("month", originMonth + 1);
  938. dateMap.put("quarter", originQuarter);
  939. } else {
  940. if (originYear == thursdayYear) {
  941. dateMap.put("year", originYear);
  942. dateMap.put("month", originMonth + 1 + 1);
  943. //判断是否跨季
  944. if (originQuarter == thursdayQuarter) {
  945. dateMap.put("quarter", originQuarter);
  946. } else {
  947. dateMap.put("quarter", thursdayQuarter);
  948. }
  949. } else {
  950. //跨年的话说明这周属于明年的1月(第一季度)
  951. dateMap.put("year", originYear + 1);
  952. dateMap.put("month", 1);
  953. dateMap.put("quarter", 1);
  954. }
  955. }
  956. return dateMap;
  957. }
  958. return dateMap;
  959. }
  960. /**
  961. * 计算季度开始、结束时间
  962. * 原则:周五为本周的第一天,下一周的周四为本周的最后一天
  963. *
  964. * @param year
  965. * @param quarter
  966. */
  967. public static Map<String, String> quarterStartEndDate(int year, int quarter) {
  968. Map<String, String> quarterStartEndDateMap = new HashMap<>();
  969. Date startDate = null;
  970. Date endDate = null;
  971. int startMonth = 0;
  972. int endMonth = 0;
  973. if (quarter == 1) {
  974. startMonth = 1;
  975. endMonth = 3;
  976. } else if (quarter == 2) {
  977. startMonth = 4;
  978. endMonth = 6;
  979. } else if (quarter == 3) {
  980. startMonth = 7;
  981. endMonth = 9;
  982. } else {
  983. startMonth = 10;
  984. endMonth = 12;
  985. }
  986. //判断季度第一天的时间(以周为单位,有可能是上个月,也有可能是本月的非1号)
  987. Calendar calendar1 = new GregorianCalendar();
  988. calendar1.set(Calendar.YEAR, year);
  989. calendar1.set(Calendar.MONTH, startMonth - 1);
  990. calendar1.set(Calendar.DATE, 1);
  991. calendar1.setFirstDayOfWeek(Calendar.FRIDAY);
  992. //如果季度第一个月的第一天是周二周三周四,那么这周应该归属于上个季度,否则这周(包括上个月的时间)归属于这个季度
  993. int firstDateWeek = calendar1.get(Calendar.DAY_OF_WEEK);
  994. if (firstDateWeek == 5 || firstDateWeek == 3 || firstDateWeek == 4) {
  995. calendar1.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  996. startDate = DateUtils.addDay(calendar1.getTime(), 1);
  997. } else {
  998. calendar1.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  999. startDate = calendar1.getTime();
  1000. }
  1001. //判断季度最后一天的时间(以周为单位,有可能是下个月,也有可能是本月的非最后一天)
  1002. Calendar calendar2 = new GregorianCalendar();
  1003. calendar2.set(Calendar.YEAR, year);
  1004. calendar2.set(Calendar.MONTH, endMonth - 1);
  1005. calendar2.set(Calendar.DATE, 1);
  1006. calendar2.setFirstDayOfWeek(Calendar.FRIDAY);
  1007. //如果一个季度最后一个月最后一天是周五、周六、周日,则这个月最后一周属于下个季度
  1008. calendar2.set(Calendar.DATE, calendar2.getActualMaximum(Calendar.DATE));
  1009. int lastDateWeek = calendar2.get(Calendar.DAY_OF_WEEK);
  1010. if (lastDateWeek == 1 || lastDateWeek == 7 || lastDateWeek == 6) {
  1011. calendar2.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  1012. endDate = DateUtils.addDay(calendar2.getTime(), -1);
  1013. } else {
  1014. calendar2.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  1015. endDate = calendar2.getTime();
  1016. }
  1017. DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  1018. quarterStartEndDateMap.put("startDate", sdf.format(startDate));
  1019. quarterStartEndDateMap.put("endDate", sdf.format(endDate));
  1020. return quarterStartEndDateMap;
  1021. }
  1022. /**
  1023. * 获取上个季度的年度和季度数
  1024. *
  1025. * @param date
  1026. * @return
  1027. */
  1028. public static Map<String, Integer> lastQuarter(Date date) {
  1029. Map<String, Integer> resultMap = new HashMap<>();
  1030. //获取当天所在
  1031. Map<String, Integer> yearQuarter = getYearQuarter(date);
  1032. Integer year = yearQuarter.get("year");
  1033. Integer quarter = yearQuarter.get("quarter");
  1034. Integer lastyear = 0;
  1035. Integer lastQuarter = 0;
  1036. if (quarter == 4 || quarter == 3 || quarter == 2) {
  1037. lastyear = year;
  1038. lastQuarter = quarter - 1;
  1039. } else {
  1040. lastyear = year - 1;
  1041. lastQuarter = 4;
  1042. }
  1043. resultMap.put("year", lastyear);
  1044. resultMap.put("quarter", lastQuarter);
  1045. return resultMap;
  1046. }
  1047. }