DateUtils.java 44 KB

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