DateUtils.java 45 KB

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