DateUtil.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. package com.java110.utils.util;
  2. import java.math.BigDecimal;
  3. import java.sql.Timestamp;
  4. import java.text.DateFormat;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.*;
  8. /**
  9. * Created by wuxw on 2017/7/24.
  10. */
  11. public class DateUtil {
  12. private static DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
  13. public static final String LAST_TIME = "2038-01-01 00:00:00";
  14. private static Map<String, SimpleDateFormat> formats = new HashMap();
  15. public static final String DATE_FORMATE_STRING_DEFAULT = "yyyyMMddHHmmss";
  16. public static final String DATE_FORMATE_STRING_A = "yyyy-MM-dd HH:mm:ss";
  17. public static final String DATE_FORMATE_STRING_B = "yyyy-MM-dd";
  18. public static final String DATE_FORMATE_STRING_C = "MM/dd/yyyy HH:mm:ss a";
  19. public static final String DATE_FORMATE_STRING_D = "yyyy-MM-dd HH:mm:ss a";
  20. public static final String DATE_FORMATE_STRING_E = "yyyy-MM-dd'T'HH:mm:ss'Z'";
  21. public static final String DATE_FORMATE_STRING_F = "yyyy-MM-dd'T'HH:mm:ssZ";
  22. public static final String DATE_FORMATE_STRING_G = "yyyy-MM-dd'T'HH:mm:ssz";
  23. public static final String DATE_FORMATE_STRING_H = "yyyyMMdd";
  24. public static final String DATE_FORMATE_STRING_I = "yyyy-MM-dd HH:mm:ss.SSS";
  25. public static final String DATE_FORMATE_STRING_J = "yyyyMMddHHmmss.SSS";
  26. public static final String DATE_FORMATE_STRING_K = "yyyyMMddHHmmssSSS";
  27. public static final String DATE_FORMATE_STRING_L = "MMdd";
  28. public static final String DATE_FORMATE_STRING_M = "yyyyMM";
  29. public static final String DATE_FORMATE_STRING_N = "HHmmss";
  30. public static final String DATE_FORMATE_STRING_O = "yyyyMMddHHmm";
  31. public static final String DATE_FORMATE_STRING_Q = "yyyy-MM";
  32. static {
  33. formats.put("yyyyMMddHHmmss", new SimpleDateFormat("yyyyMMddHHmmss"));
  34. formats.put("yyyy-MM-dd HH:mm:ss", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
  35. formats.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
  36. formats.put("MM/dd/yyyy HH:mm:ss a", new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a"));
  37. formats.put("yyyy-MM-dd HH:mm:ss a", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a"));
  38. formats.put("yyyy-MM-dd'T'HH:mm:ss'Z'", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
  39. formats.put("yyyy-MM-dd'T'HH:mm:ssZ", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
  40. formats.put("yyyy-MM-dd'T'HH:mm:ssz", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"));
  41. formats.put("yyyyMMdd", new SimpleDateFormat("yyyyMMdd"));
  42. formats.put("yyyy-MM-dd HH:mm:ss.SSS", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
  43. formats.put("yyyyMMddHHmmss.SSS", new SimpleDateFormat("yyyyMMddHHmmss.SSS"));
  44. formats.put("yyyyMMddHHmmssSSS", new SimpleDateFormat("yyyyMMddHHmmssSSS"));
  45. formats.put("MMdd", new SimpleDateFormat("MMdd"));
  46. formats.put("yyyyMM", new SimpleDateFormat("yyyyMM"));
  47. formats.put("HHmmss", new SimpleDateFormat("HHmmss"));
  48. formats.put("yyyyMMddHHmm", new SimpleDateFormat("yyyyMMddHHmm"));
  49. formats.put("yyyy-MM", new SimpleDateFormat("yyyy-MM"));
  50. }
  51. /**
  52. * 返回 yyyyMMddhhmmss 格式的日期串
  53. *
  54. * @return
  55. */
  56. public static String getyyyyMMddhhmmssDateString() {
  57. return dateFormat.format(new Date());
  58. }
  59. /**
  60. * 获取当前时间
  61. *
  62. * @return
  63. */
  64. public static Date getCurrentDate() {
  65. Calendar calendar = Calendar.getInstance();
  66. return calendar.getTime();
  67. }
  68. /**
  69. * 获取当前月
  70. *
  71. * @return
  72. */
  73. public static int getCurrentMonth() {
  74. Calendar calendar = Calendar.getInstance();
  75. return calendar.get(Calendar.MONTH) + 1;
  76. }
  77. public static Date getLastDate() throws ParseException {
  78. return getDateFromString("2037-12-01", DATE_FORMATE_STRING_B);
  79. }
  80. /**
  81. * 转TimeStamp
  82. *
  83. * @param date
  84. * @return
  85. */
  86. public static Timestamp getTimestamp(Date date) {
  87. Timestamp timestamp = new Timestamp(date.getTime());
  88. return timestamp;
  89. }
  90. /**
  91. * 获取未来时间
  92. *
  93. * @param second 秒
  94. * @return
  95. */
  96. public static Date getFutureDate(int second) {
  97. Calendar calendar = Calendar.getInstance();
  98. calendar.add(Calendar.SECOND, second);
  99. return calendar.getTime();
  100. }
  101. public static String getFormatTimeString(Date date, String pattern) {
  102. SimpleDateFormat sDateFormat = getDateFormat(pattern);
  103. synchronized (sDateFormat) {
  104. return sDateFormat.format(date);
  105. }
  106. }
  107. public static String getFormatTimeStringA(Date date) {
  108. SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_A);
  109. synchronized (sDateFormat) {
  110. return sDateFormat.format(date);
  111. }
  112. }
  113. public static String getFormatTimeStringB(Date date) {
  114. SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_B);
  115. synchronized (sDateFormat) {
  116. return sDateFormat.format(date);
  117. }
  118. }
  119. public static String getDefaultFormateTimeString(Date date) {
  120. return getFormatTimeString(date, "yyyyMMddHHmmss");
  121. }
  122. public static SimpleDateFormat getDateFormat(String pattern) {
  123. SimpleDateFormat sDateFormat = (SimpleDateFormat) formats.get(pattern);
  124. if (sDateFormat == null) {
  125. sDateFormat = new SimpleDateFormat(pattern);
  126. formats.put(pattern, sDateFormat);
  127. }
  128. return sDateFormat;
  129. }
  130. public static Date getDateFromString(String date, String pattern)
  131. throws ParseException {
  132. SimpleDateFormat sDateFormat = getDateFormat(pattern);
  133. synchronized (sDateFormat) {
  134. return sDateFormat.parse(date);
  135. }
  136. }
  137. public static Date getDateFromStringB(String date) {
  138. SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_B);
  139. try {
  140. synchronized (sDateFormat) {
  141. return sDateFormat.parse(date);
  142. }
  143. } catch (Exception e) {
  144. throw new IllegalArgumentException(e);
  145. }
  146. }
  147. public static Date getDateFromStringA(String date) {
  148. SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_A);
  149. try {
  150. synchronized (sDateFormat) {
  151. return sDateFormat.parse(date);
  152. }
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. throw new IllegalArgumentException(e);
  156. }
  157. }
  158. public static Date getDefaultDateFromString(String date)
  159. throws ParseException {
  160. return getDateFromString(date, "yyyyMMddHHmmss");
  161. }
  162. public static String getNowDefault() {
  163. return getNow("yyyyMMddHHmmss");
  164. }
  165. public static String getNow(String pattern) {
  166. return getFormatTimeString(new Date(), pattern);
  167. }
  168. public static String getLastTime() {
  169. return LAST_TIME;
  170. }
  171. public static String getNowII() {
  172. return getFormatTimeString(new Date(), "yyyyMMdd");
  173. }
  174. public static long dateString2Long(String str, String pattern)
  175. throws ParseException {
  176. return getDateFromString(str, pattern).getTime();
  177. }
  178. /**
  179. * 校验字符串是否可以格式化为时间
  180. *
  181. * @param str
  182. * @param pattern
  183. * @return
  184. */
  185. public static boolean judgeDate(String str, String pattern) {
  186. try {
  187. dateString2Long(str, pattern);
  188. } catch (Exception e) {
  189. return false;
  190. }
  191. return true;
  192. }
  193. public static String longToDateStringDefault(long time) {
  194. return getFormatTimeString(new Date(time), "yyyyMMddHHmmss");
  195. }
  196. public static String longToDateString(long time, String pattern) {
  197. return getFormatTimeString(new Date(time), pattern);
  198. }
  199. public static long date2Long(Date date) {
  200. return date.getTime();
  201. }
  202. public static Date longToDate(long time) {
  203. return new Date(time);
  204. }
  205. public static Date getDateFromStringAdaptTwoPattern(String date)
  206. throws ParseException {
  207. try {
  208. return getDateFromString(date, "yyyy-MM-dd HH:mm:ss");
  209. } catch (ParseException e) {
  210. }
  211. return getDateFromString(date, "yyyy-MM-dd");
  212. }
  213. public static String changeNumDateToDate(String numdate, String inFormat, String outFormat)
  214. throws ParseException {
  215. Date date = getDateFromString(numdate, inFormat);
  216. return getFormatTimeString(date, outFormat);
  217. }
  218. public static String getNextMonthFistDay(String nowdate, String inFormat, String outFormat)
  219. throws ParseException {
  220. Date date = getDateFromString(nowdate, inFormat);
  221. Calendar cl = Calendar.getInstance();
  222. cl.setTime(date);
  223. cl.set(2, cl.get(2) + 1);
  224. cl.set(5, 1);
  225. date = cl.getTime();
  226. return getFormatTimeString(date, outFormat);
  227. }
  228. public static boolean isLeapYear(int year) {
  229. if (year % 400 == 0)
  230. return true;
  231. if (year % 4 == 0) {
  232. return (year % 100 != 0);
  233. }
  234. return false;
  235. }
  236. public static String getLastDay(String nowdate, String inFormat, String outFormat)
  237. throws ParseException {
  238. String returndate = "";
  239. Date date = getDateFromString(nowdate, inFormat);
  240. Calendar cl = Calendar.getInstance();
  241. cl.setTime(date);
  242. switch (cl.get(2)) {
  243. case 0:
  244. cl.set(5, 31);
  245. break;
  246. case 1:
  247. int year = cl.get(1);
  248. if (isLeapYear(year))
  249. cl.set(5, 29);
  250. else {
  251. cl.set(5, 28);
  252. }
  253. break;
  254. case 2:
  255. cl.set(5, 31);
  256. break;
  257. case 3:
  258. cl.set(5, 30);
  259. break;
  260. case 4:
  261. cl.set(5, 31);
  262. break;
  263. case 5:
  264. cl.set(5, 30);
  265. break;
  266. case 6:
  267. cl.set(5, 31);
  268. break;
  269. case 7:
  270. cl.set(5, 31);
  271. break;
  272. case 8:
  273. cl.set(5, 30);
  274. break;
  275. case 9:
  276. cl.set(5, 31);
  277. break;
  278. case 10:
  279. cl.set(5, 30);
  280. break;
  281. case 11:
  282. cl.set(5, 31);
  283. }
  284. date = cl.getTime();
  285. returndate = getFormatTimeString(date, outFormat);
  286. return returndate;
  287. }
  288. public static String getMonthLastDay(String fmt) {
  289. String returndate = "";
  290. Date date = null;
  291. Calendar cl = Calendar.getInstance();
  292. switch (cl.get(2)) {
  293. case 0:
  294. cl.set(5, 31);
  295. break;
  296. case 1:
  297. int year = cl.get(1);
  298. if (isLeapYear(year))
  299. cl.set(5, 29);
  300. else {
  301. cl.set(5, 28);
  302. }
  303. break;
  304. case 2:
  305. cl.set(5, 31);
  306. break;
  307. case 3:
  308. cl.set(5, 30);
  309. break;
  310. case 4:
  311. cl.set(5, 31);
  312. break;
  313. case 5:
  314. cl.set(5, 30);
  315. break;
  316. case 6:
  317. cl.set(5, 31);
  318. break;
  319. case 7:
  320. cl.set(5, 31);
  321. break;
  322. case 8:
  323. cl.set(5, 30);
  324. break;
  325. case 9:
  326. cl.set(5, 31);
  327. break;
  328. case 10:
  329. cl.set(5, 30);
  330. break;
  331. case 11:
  332. cl.set(5, 31);
  333. }
  334. date = cl.getTime();
  335. returndate = getFormatTimeString(date, fmt);
  336. return returndate;
  337. }
  338. public static Date getNextMonthFirstDate() {
  339. Calendar calendar = Calendar.getInstance();
  340. calendar.set(Calendar.DAY_OF_MONTH, 1);
  341. calendar.set(Calendar.HOUR_OF_DAY, 0);
  342. calendar.set(Calendar.MINUTE, 0);
  343. calendar.set(Calendar.SECOND, 0);
  344. calendar.add(Calendar.MONTH, 1);
  345. return calendar.getTime();
  346. }
  347. public static Date getNextMonthFirstDate(String curDate) {
  348. Date date = DateUtil.getDateFromStringB(curDate);
  349. return getNextMonthFirstDate(date);
  350. }
  351. public static Date getNextMonthFirstDate(Date curDate) {
  352. Calendar curDateCal = Calendar.getInstance();
  353. curDateCal.setTime(curDate);
  354. curDateCal.set(Calendar.DAY_OF_MONTH, 1);
  355. curDateCal.set(Calendar.HOUR_OF_DAY, 0);
  356. curDateCal.set(Calendar.MINUTE, 0);
  357. curDateCal.set(Calendar.SECOND, 0);
  358. curDateCal.add(Calendar.MONTH, 1);
  359. return curDateCal.getTime();
  360. }
  361. public static Date getFirstDate() {
  362. Calendar curDateCal = Calendar.getInstance();
  363. curDateCal.set(Calendar.DAY_OF_MONTH, 1);
  364. curDateCal.set(Calendar.HOUR_OF_DAY, 0);
  365. curDateCal.set(Calendar.MINUTE, 0);
  366. curDateCal.set(Calendar.SECOND, 0);
  367. Date curDate = curDateCal.getTime();
  368. return curDate;
  369. }
  370. public static Date getFirstDate(Date curDate) {
  371. Calendar curDateCal = Calendar.getInstance();
  372. curDateCal.setTime(curDate);
  373. curDateCal.set(Calendar.DAY_OF_MONTH, 1);
  374. curDateCal.set(Calendar.HOUR_OF_DAY, 0);
  375. curDateCal.set(Calendar.MINUTE, 0);
  376. curDateCal.set(Calendar.SECOND, 0);
  377. return curDateCal.getTime();
  378. }
  379. public static Date getFirstDate(String curDate) {
  380. Date date = DateUtil.getDateFromStringB(curDate);
  381. Calendar curDateCal = Calendar.getInstance();
  382. curDateCal.setTime(date);
  383. curDateCal.set(Calendar.DAY_OF_MONTH, 1);
  384. curDateCal.set(Calendar.HOUR_OF_DAY, 0);
  385. curDateCal.set(Calendar.MINUTE, 0);
  386. curDateCal.set(Calendar.SECOND, 0);
  387. return curDateCal.getTime();
  388. }
  389. public static String getNextMonthFirstDay(String fmt) {
  390. String returndate = "";
  391. Date date = getNextMonthFirstDate();
  392. returndate = getFormatTimeString(date, fmt);
  393. return returndate;
  394. }
  395. public static boolean compareDate(String fistDate, String secondDate, String format)
  396. throws ParseException {
  397. boolean flag = false;
  398. Date fist = null;
  399. Date second = null;
  400. fist = getDateFromString(fistDate, format);
  401. second = getDateFromString(secondDate, format);
  402. if (fist.before(second)) {
  403. flag = true;
  404. }
  405. return flag;
  406. }
  407. public static boolean isRightDate(String value, String varValue) {
  408. try {
  409. SimpleDateFormat format = new SimpleDateFormat(varValue);
  410. format.setLenient(false);
  411. format.parse(value);
  412. } catch (ParseException e) {
  413. return false;
  414. }
  415. return true;
  416. }
  417. public static int getCurrentMonthDay() {
  418. Calendar a = Calendar.getInstance();
  419. a.set(Calendar.DATE, 1);
  420. a.roll(Calendar.DATE, -1);
  421. int maxDate = a.get(Calendar.DATE);
  422. return maxDate;
  423. }
  424. public static int getMonthDay(Date date) {
  425. Calendar a = Calendar.getInstance();
  426. a.setTime(date);
  427. return a.getActualMaximum(Calendar.DAY_OF_MONTH);
  428. }
  429. public static void main(String[] args) throws ParseException {
  430. // SimpleDateFormat sf = new SimpleDateFormat(DateUtil.DATE_FORMATE_STRING_A);
  431. // Calendar c = Calendar.getInstance();
  432. // c.setTime(DateUtil.getDateFromString("2021-12-03",DateUtil.DATE_FORMATE_STRING_A));
  433. // c.add(Calendar.DAY_OF_MONTH, 125);
  434. // System.out.println("增加一天后日期:"+sf.format(c.getTime()));
  435. System.out.println("2021-12-07".compareTo(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B)));
  436. }
  437. public static String getAddDayString(Date date, String pattern, int days) {
  438. SimpleDateFormat sf = new SimpleDateFormat(pattern);
  439. Calendar c = Calendar.getInstance();
  440. c.setTime(date);
  441. c.add(Calendar.DAY_OF_MONTH, days);
  442. return sf.format(c.getTime());
  443. }
  444. public static String getAddDayStringB(Date date, int days) {
  445. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_B);
  446. Calendar c = Calendar.getInstance();
  447. c.setTime(date);
  448. c.add(Calendar.DAY_OF_MONTH, days);
  449. return sf.format(c.getTime());
  450. }
  451. public static String getAddDayStringA(Date date, int days) {
  452. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
  453. Calendar c = Calendar.getInstance();
  454. c.setTime(date);
  455. c.add(Calendar.DAY_OF_MONTH, days);
  456. return sf.format(c.getTime());
  457. }
  458. public static String getAddHoursStringA(Date date, int hours) {
  459. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
  460. Calendar c = Calendar.getInstance();
  461. c.setTime(date);
  462. c.add(Calendar.HOUR_OF_DAY, hours);
  463. return sf.format(c.getTime());
  464. }
  465. public static String getAddMonthStringA(Date date, int month) {
  466. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
  467. Calendar c = Calendar.getInstance();
  468. c.setTime(date);
  469. c.add(Calendar.MONTH, month);
  470. return sf.format(c.getTime());
  471. }
  472. /**
  473. * 在给定的日期加上或减去指定月份后的日期
  474. *
  475. * @param sourceDate 原始时间
  476. * @param month 要调整的月份,向前为负数,向后为正数
  477. * @return
  478. */
  479. public static Date stepMonth(Date sourceDate, int month) {
  480. Calendar c = Calendar.getInstance();
  481. c.setTime(sourceDate);
  482. c.add(Calendar.MONTH, month);
  483. return c.getTime();
  484. }
  485. /**
  486. * 在给定的日期加上或减去指定天数后的日期
  487. *
  488. * @param sourceDate 原始时间
  489. * @param day 要调整的月份,向前为负数,向后为正数
  490. * @return
  491. */
  492. public static Date stepDay(Date sourceDate, int day) {
  493. Calendar c = Calendar.getInstance();
  494. c.setTime(sourceDate);
  495. c.add(Calendar.DATE, day);
  496. return c.getTime();
  497. }
  498. public static String dateTimeToDate(String dateTime) {
  499. String dateStr = "";
  500. try {
  501. Date date = getDateFromString(dateTime, DATE_FORMATE_STRING_A);
  502. dateStr = getFormatTimeString(date, DATE_FORMATE_STRING_B);
  503. } catch (ParseException e) {
  504. dateStr = dateTime;
  505. }
  506. return dateStr;
  507. }
  508. public static int getYear() {
  509. Date date = getCurrentDate();
  510. Calendar calendar = Calendar.getInstance();
  511. calendar.setTime(date);
  512. return calendar.get(Calendar.YEAR);
  513. }
  514. public static int getMonth() {
  515. Date date = getCurrentDate();
  516. Calendar calendar = Calendar.getInstance();
  517. calendar.setTime(date);
  518. return calendar.get(Calendar.MONTH) + 1;
  519. }
  520. /**
  521. * 判断时间是否在时间段内
  522. *
  523. * @param nowTime
  524. * @param beginTime
  525. * @param endTime
  526. * @return
  527. */
  528. public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
  529. Calendar date = Calendar.getInstance();
  530. date.setTime(nowTime);
  531. Calendar begin = Calendar.getInstance();
  532. begin.setTime(beginTime);
  533. Calendar end = Calendar.getInstance();
  534. end.setTime(endTime);
  535. if (date.after(begin) && date.before(end)) {
  536. return true;
  537. } else if (nowTime.compareTo(beginTime) == 0 || nowTime.compareTo(endTime) == 0) {
  538. return true;
  539. } else {
  540. return false;
  541. }
  542. }
  543. //获取两个日期之间的天数
  544. public static int daysBetween(Date now, Date returnDate) {
  545. Calendar cNow = Calendar.getInstance();
  546. Calendar cReturnDate = Calendar.getInstance();
  547. cNow.setTime(now);
  548. cReturnDate.setTime(returnDate);
  549. setTimeToMidnight(cNow);
  550. setTimeToMidnight(cReturnDate);
  551. long todayMs = cNow.getTimeInMillis();
  552. long returnMs = cReturnDate.getTimeInMillis();
  553. long intervalMs = todayMs - returnMs;
  554. return millisecondsToDays(intervalMs);
  555. }
  556. //获取两个日期之间的毫秒数
  557. private static void setTimeToMidnight(Calendar calendar) {
  558. calendar.set(Calendar.HOUR_OF_DAY, 0);
  559. calendar.set(Calendar.MINUTE, 0);
  560. calendar.set(Calendar.SECOND, 0);
  561. }
  562. //获取两个日期之间的分钟数
  563. private static int millisecondsToDays(long intervalMs) {
  564. return (int) (intervalMs / (1000 * 86400));
  565. }
  566. /**
  567. *    *字符串的日期格式的计算
  568. */
  569. public static int daysBetween(String smdate, String bdate) {
  570. long between_days = 0;
  571. try {
  572. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  573. Calendar cal = Calendar.getInstance();
  574. cal.setTime(sdf.parse(smdate));
  575. long time1 = cal.getTimeInMillis();
  576. cal.setTime(sdf.parse(bdate));
  577. long time2 = cal.getTimeInMillis();
  578. between_days = (time2 - time1) / (1000 * 3600 * 24);
  579. } catch (Exception e) {
  580. e.printStackTrace();
  581. }
  582. return Integer.parseInt(String.valueOf(between_days));
  583. }
  584. // public static double dayCompare(Date fromDate, Date toDate) {
  585. // double resMonth = 0.0;
  586. // Calendar from = Calendar.getInstance();
  587. // from.setTime(fromDate);
  588. // Calendar to = Calendar.getInstance();
  589. // to.setTime(toDate);
  590. // //比较月份差 可能有整数 也会负数
  591. // int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
  592. // //比较年差
  593. // int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
  594. //
  595. // //真实 相差月份
  596. // result = result + month;
  597. //
  598. // //开始时间 2021-06-01 2021-08-05 result = 2 2021-08-01
  599. // Calendar newFrom = Calendar.getInstance();
  600. // newFrom.setTime(fromDate);
  601. // newFrom.add(Calendar.MONTH, result);
  602. // //如果加月份后 大于了当前时间 默认加 月份 -1 情况 12-19 21-01-10
  603. // //这个是神的逻辑一定好好理解
  604. // if (newFrom.getTime().getTime() > toDate.getTime()) {
  605. // newFrom.setTime(fromDate);
  606. // result = result - 1;
  607. // newFrom.add(Calendar.MONTH, result);
  608. // }
  609. //
  610. // // t1 2021-08-01 t2 2021-08-05
  611. // long t1 = newFrom.getTime().getTime();
  612. // long t2 = to.getTime().getTime();
  613. // //相差毫秒
  614. // double days = (t2 - t1) * 1.00 / (24 * 60 * 60 * 1000);
  615. // BigDecimal tmpDays = new BigDecimal(days); //相差天数
  616. // BigDecimal monthDay = null;
  617. // Calendar newFromMaxDay = Calendar.getInstance();
  618. // newFromMaxDay.set(newFrom.get(Calendar.YEAR), newFrom.get(Calendar.MONTH), 1, 0, 0, 0);
  619. // newFromMaxDay.add(Calendar.MONTH, 1); //下个月1号
  620. // //在当前月中 这块有问题
  621. // if (toDate.getTime() < newFromMaxDay.getTime().getTime()) {
  622. // monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
  623. // return tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).doubleValue();
  624. // }
  625. // // 上月天数
  626. // days = (newFromMaxDay.getTimeInMillis() - t1) * 1.00 / (24 * 60 * 60 * 1000);
  627. // tmpDays = new BigDecimal(days);
  628. // monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
  629. // BigDecimal preRresMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP);
  630. //
  631. // //下月天数
  632. // days = (t2 - newFromMaxDay.getTimeInMillis()) * 1.00 / (24 * 60 * 60 * 1000);
  633. // tmpDays = new BigDecimal(days);
  634. // monthDay = new BigDecimal(newFromMaxDay.getActualMaximum(Calendar.DAY_OF_MONTH));
  635. // resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).add(preRresMonth).doubleValue();
  636. // return resMonth;
  637. // }
  638. /**
  639. * 通过时间秒毫秒数判断两个时间的间隔
  640. *
  641. * @param date1
  642. * @param date2
  643. * @return
  644. */
  645. public static int differentDaysUp(Date date1, Date date2) {
  646. double days = ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24 * 1.00));
  647. return new Double(Math.ceil(days)).intValue();
  648. }
  649. /**
  650. * 获取两个日期之间的所有月份 (年月)
  651. *
  652. * @param startTime
  653. * @param endTime
  654. * @return:list
  655. */
  656. public static List<String> getMonthBetweenDate(Date startTime, Date endTime) {
  657. return getMonthBetweenDate(DateUtil.getFormatTimeStringA(startTime), DateUtil.getFormatTimeStringA(endTime));
  658. }
  659. /**
  660. * 获取两个日期之间的所有月份 (年月)
  661. *
  662. * @param startTime
  663. * @param endTime
  664. * @return:list
  665. */
  666. public static List<String> getMonthBetweenDate(String startTime, String endTime) {
  667. SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMATE_STRING_Q);
  668. // 声明保存日期集合
  669. List<String> list = new ArrayList<>();
  670. try {
  671. // 转化成日期类型
  672. Date startDate = sdf.parse(startTime);
  673. Date endDate = sdf.parse(endTime);
  674. //用Calendar 进行日期比较判断
  675. Calendar calendar = Calendar.getInstance();
  676. while (startDate.getTime() <= endDate.getTime()) {
  677. // 把日期添加到集合
  678. list.add(sdf.format(startDate));
  679. // 设置日期
  680. calendar.setTime(startDate);
  681. //把月数增加 1
  682. calendar.add(Calendar.MONTH, 1);
  683. // 获取增加后的日期
  684. startDate = calendar.getTime();
  685. }
  686. } catch (Exception e) {
  687. e.printStackTrace();
  688. }
  689. return list;
  690. }
  691. /**
  692. * 除去 小时 分 秒
  693. *
  694. * @param time
  695. * @return
  696. */
  697. public static Date timeToDate(Date time) {
  698. Calendar calendar = Calendar.getInstance();
  699. calendar.setTime(time);
  700. setTimeToMidnight(calendar);
  701. return calendar.getTime();
  702. }
  703. public static boolean sameMonthDay(Date startDate, Date endDate) {
  704. Calendar startCalendar = Calendar.getInstance();
  705. startCalendar.setTime(startDate);
  706. Calendar endCalender = Calendar.getInstance();
  707. endCalender.setTime(endDate);
  708. if (startCalendar.get(Calendar.DAY_OF_MONTH) == endCalender.get(Calendar.DAY_OF_MONTH)) {
  709. return true;
  710. }
  711. return false;
  712. }
  713. /**
  714. * 计算 fromDate 2023-01-12 toDate 2023-09-15
  715. * 2023-01-12--->2023-01-01 ---> 2023-09-01 ------> 2023-09-15
  716. * fromDate ---> fromDateFirstDate ---> toDateFirstDate ----> toDate
  717. *
  718. * @param fromDate
  719. * @param toDate
  720. * @return
  721. */
  722. public static double dayCompare(Date fromDate, Date toDate) {
  723. //todo 需要计算三端时间 相加即可
  724. Date fromDateFirstDate = fromDate; // 第一个1日
  725. Date toDateFirstDate = toDate; // 最后一个1日
  726. boolean firstDay = true;
  727. //todo 1.0 计算 fromDateFirstDate
  728. Calendar fromDateCal = Calendar.getInstance();
  729. fromDateCal.setTime(fromDate);
  730. fromDateCal.set(Calendar.DAY_OF_MONTH, 1);
  731. if (fromDate.getTime() > fromDateCal.getTime().getTime()) {
  732. fromDateCal.add(Calendar.MONTH, 1);
  733. firstDay = false;
  734. fromDateFirstDate = fromDateCal.getTime();
  735. }
  736. //todo 2.0 计算 toDateFirstDate
  737. Calendar toDateCal = Calendar.getInstance();
  738. toDateCal.setTime(toDate);
  739. toDateCal.set(Calendar.DAY_OF_MONTH, 1);
  740. if (toDate.getTime() > toDateCal.getTime().getTime()) {
  741. toDateFirstDate = toDateCal.getTime();
  742. }
  743. // todo 3.0 计算整数月 fromDateFirstDate ---> toDateFirstDate
  744. Calendar from = Calendar.getInstance();
  745. from.setTime(fromDateFirstDate);
  746. Calendar to = Calendar.getInstance();
  747. to.setTime(toDateFirstDate);
  748. //比较月份差 可能有整数 也会负数
  749. int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
  750. //比较年差
  751. int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
  752. //真实 相差月份
  753. result = result + month;
  754. //todo 3.1 如果 fromDate 和toDate 是同一天 则直接返回整月,不再计算 4.0 和5.0
  755. if (DateUtil.sameMonthDay(fromDate, toDate)) {
  756. return firstDay ? result : result + 1;
  757. }
  758. // todo 4.0 计算 fromDate ---> fromDateFirstDate 的月份
  759. double days = (fromDateFirstDate.getTime() - fromDate.getTime()) * 1.00 / (24 * 60 * 60 * 1000);
  760. BigDecimal tmpDays = new BigDecimal(days); //相差天数
  761. BigDecimal monthDay = new BigDecimal(DateUtil.getMonthDay(fromDate));
  762. BigDecimal resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result));
  763. // todo 5.0 计算 toDateFirstDate ----> toDate 月份
  764. days = (toDate.getTime() - toDateFirstDate.getTime()) * 1.00 / (24 * 60 * 60 * 1000);
  765. tmpDays = new BigDecimal(days); //相差天数
  766. monthDay = new BigDecimal(DateUtil.getMonthDay(toDate));
  767. resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(resMonth);
  768. return resMonth.doubleValue();
  769. }
  770. public static Date getTargetEndTime(double month, Date startDate) {
  771. Calendar endDate = Calendar.getInstance();
  772. endDate.setTime(startDate);
  773. Double intMonth = Math.floor(month);
  774. endDate.add(Calendar.MONTH, intMonth.intValue());
  775. double doubleMonth = month - intMonth;
  776. if (doubleMonth <= 0) {
  777. return endDate.getTime();
  778. }
  779. int futureDay = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);
  780. Double hour = doubleMonth * futureDay * 24;
  781. endDate.add(Calendar.HOUR_OF_DAY, hour.intValue());
  782. return endDate.getTime();
  783. }
  784. }