DateUtils.java 44 KB

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