DateUtil.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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.Calendar;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. /**
  12. * Created by wuxw on 2017/7/24.
  13. */
  14. public class DateUtil {
  15. private static DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
  16. public static final String LAST_TIME = "2038-01-01 00:00:00";
  17. private static Map<String, SimpleDateFormat> formats = new HashMap();
  18. public static final String DATE_FORMATE_STRING_DEFAULT = "yyyyMMddHHmmss";
  19. public static final String DATE_FORMATE_STRING_A = "yyyy-MM-dd HH:mm:ss";
  20. public static final String DATE_FORMATE_STRING_B = "yyyy-MM-dd";
  21. public static final String DATE_FORMATE_STRING_C = "MM/dd/yyyy HH:mm:ss a";
  22. public static final String DATE_FORMATE_STRING_D = "yyyy-MM-dd HH:mm:ss a";
  23. public static final String DATE_FORMATE_STRING_E = "yyyy-MM-dd'T'HH:mm:ss'Z'";
  24. public static final String DATE_FORMATE_STRING_F = "yyyy-MM-dd'T'HH:mm:ssZ";
  25. public static final String DATE_FORMATE_STRING_G = "yyyy-MM-dd'T'HH:mm:ssz";
  26. public static final String DATE_FORMATE_STRING_H = "yyyyMMdd";
  27. public static final String DATE_FORMATE_STRING_I = "yyyy-MM-dd HH:mm:ss.SSS";
  28. public static final String DATE_FORMATE_STRING_J = "yyyyMMddHHmmss.SSS";
  29. public static final String DATE_FORMATE_STRING_K = "yyyyMMddHHmmssSSS";
  30. public static final String DATE_FORMATE_STRING_L = "MMdd";
  31. public static final String DATE_FORMATE_STRING_M = "yyyyMM";
  32. public static final String DATE_FORMATE_STRING_N = "HHmmss";
  33. public static final String DATE_FORMATE_STRING_O = "yyyyMMddHHmm";
  34. static {
  35. formats.put("yyyyMMddHHmmss", new SimpleDateFormat("yyyyMMddHHmmss"));
  36. formats.put("yyyy-MM-dd HH:mm:ss", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
  37. formats.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
  38. formats.put("MM/dd/yyyy HH:mm:ss a", new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a"));
  39. formats.put("yyyy-MM-dd HH:mm:ss a", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a"));
  40. formats.put("yyyy-MM-dd'T'HH:mm:ss'Z'", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
  41. formats.put("yyyy-MM-dd'T'HH:mm:ssZ", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
  42. formats.put("yyyy-MM-dd'T'HH:mm:ssz", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"));
  43. formats.put("yyyyMMdd", new SimpleDateFormat("yyyyMMdd"));
  44. formats.put("yyyy-MM-dd HH:mm:ss.SSS", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
  45. formats.put("yyyyMMddHHmmss.SSS", new SimpleDateFormat("yyyyMMddHHmmss.SSS"));
  46. formats.put("yyyyMMddHHmmssSSS", new SimpleDateFormat("yyyyMMddHHmmssSSS"));
  47. formats.put("MMdd", new SimpleDateFormat("MMdd"));
  48. formats.put("yyyyMM", new SimpleDateFormat("yyyyMM"));
  49. formats.put("HHmmss", new SimpleDateFormat("HHmmss"));
  50. formats.put("yyyyMMddHHmm", new SimpleDateFormat("yyyyMMddHHmm"));
  51. }
  52. /**
  53. * 返回 yyyyMMddhhmmss 格式的日期串
  54. *
  55. * @return
  56. */
  57. public static String getyyyyMMddhhmmssDateString() {
  58. return dateFormat.format(new Date());
  59. }
  60. /**
  61. * 获取当前时间
  62. *
  63. * @return
  64. */
  65. public static Date getCurrentDate() {
  66. Calendar calendar = Calendar.getInstance();
  67. return calendar.getTime();
  68. }
  69. /**
  70. * 获取当前月
  71. *
  72. * @return
  73. */
  74. public static int getCurrentMonth() {
  75. Calendar calendar = Calendar.getInstance();
  76. return calendar.get(Calendar.MONTH) + 1;
  77. }
  78. public static Date getLastDate() throws ParseException {
  79. return getDateFromString("2037-12-01", DATE_FORMATE_STRING_B);
  80. }
  81. /**
  82. * 转TimeStamp
  83. *
  84. * @param date
  85. * @return
  86. */
  87. public static Timestamp getTimestamp(Date date) {
  88. Timestamp timestamp = new Timestamp(date.getTime());
  89. return timestamp;
  90. }
  91. /**
  92. * 获取未来时间
  93. *
  94. * @param second 秒
  95. * @return
  96. */
  97. public static Date getFutureDate(int second) {
  98. Calendar calendar = Calendar.getInstance();
  99. calendar.add(Calendar.SECOND, second);
  100. return calendar.getTime();
  101. }
  102. public static String getFormatTimeString(Date date, String pattern) {
  103. SimpleDateFormat sDateFormat = getDateFormat(pattern);
  104. synchronized (sDateFormat) {
  105. return sDateFormat.format(date);
  106. }
  107. }
  108. public static String getDefaultFormateTimeString(Date date) {
  109. return getFormatTimeString(date, "yyyyMMddHHmmss");
  110. }
  111. public static SimpleDateFormat getDateFormat(String pattern) {
  112. SimpleDateFormat sDateFormat = (SimpleDateFormat) formats.get(pattern);
  113. if (sDateFormat == null) {
  114. sDateFormat = new SimpleDateFormat(pattern);
  115. formats.put(pattern, sDateFormat);
  116. }
  117. return sDateFormat;
  118. }
  119. public static Date getDateFromString(String date, String pattern)
  120. throws ParseException {
  121. SimpleDateFormat sDateFormat = getDateFormat(pattern);
  122. synchronized (sDateFormat) {
  123. return sDateFormat.parse(date);
  124. }
  125. }
  126. public static Date getDateFromStringB(String date) {
  127. SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_B);
  128. try {
  129. synchronized (sDateFormat) {
  130. return sDateFormat.parse(date);
  131. }
  132. } catch (Exception e) {
  133. throw new IllegalArgumentException(e);
  134. }
  135. }
  136. public static Date getDateFromStringA(String date) {
  137. SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_A);
  138. try {
  139. synchronized (sDateFormat) {
  140. return sDateFormat.parse(date);
  141. }
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. throw new IllegalArgumentException(e);
  145. }
  146. }
  147. public static Date getDefaultDateFromString(String date)
  148. throws ParseException {
  149. return getDateFromString(date, "yyyyMMddHHmmss");
  150. }
  151. public static String getNowDefault() {
  152. return getNow("yyyyMMddHHmmss");
  153. }
  154. public static String getNow(String pattern) {
  155. return getFormatTimeString(new Date(), pattern);
  156. }
  157. public static String getLastTime() {
  158. return LAST_TIME;
  159. }
  160. public static String getNowII() {
  161. return getFormatTimeString(new Date(), "yyyyMMdd");
  162. }
  163. public static long dateString2Long(String str, String pattern)
  164. throws ParseException {
  165. return getDateFromString(str, pattern).getTime();
  166. }
  167. /**
  168. * 校验字符串是否可以格式化为时间
  169. *
  170. * @param str
  171. * @param pattern
  172. * @return
  173. */
  174. public static boolean judgeDate(String str, String pattern) {
  175. try {
  176. dateString2Long(str, pattern);
  177. } catch (Exception e) {
  178. return false;
  179. }
  180. return true;
  181. }
  182. public static String longToDateStringDefault(long time) {
  183. return getFormatTimeString(new Date(time), "yyyyMMddHHmmss");
  184. }
  185. public static String longToDateString(long time, String pattern) {
  186. return getFormatTimeString(new Date(time), pattern);
  187. }
  188. public static long date2Long(Date date) {
  189. return date.getTime();
  190. }
  191. public static Date longToDate(long time) {
  192. return new Date(time);
  193. }
  194. public static Date getDateFromStringAdaptTwoPattern(String date)
  195. throws ParseException {
  196. try {
  197. return getDateFromString(date, "yyyy-MM-dd HH:mm:ss");
  198. } catch (ParseException e) {
  199. }
  200. return getDateFromString(date, "yyyy-MM-dd");
  201. }
  202. public static String changeNumDateToDate(String numdate, String inFormat, String outFormat)
  203. throws ParseException {
  204. Date date = getDateFromString(numdate, inFormat);
  205. return getFormatTimeString(date, outFormat);
  206. }
  207. public static String getNextMonthFistDay(String nowdate, String inFormat, String outFormat)
  208. throws ParseException {
  209. Date date = getDateFromString(nowdate, inFormat);
  210. Calendar cl = Calendar.getInstance();
  211. cl.setTime(date);
  212. cl.set(2, cl.get(2) + 1);
  213. cl.set(5, 1);
  214. date = cl.getTime();
  215. return getFormatTimeString(date, outFormat);
  216. }
  217. public static boolean isLeapYear(int year) {
  218. if (year % 400 == 0)
  219. return true;
  220. if (year % 4 == 0) {
  221. return (year % 100 != 0);
  222. }
  223. return false;
  224. }
  225. public static String getLastDay(String nowdate, String inFormat, String outFormat)
  226. throws ParseException {
  227. String returndate = "";
  228. Date date = getDateFromString(nowdate, inFormat);
  229. Calendar cl = Calendar.getInstance();
  230. cl.setTime(date);
  231. switch (cl.get(2)) {
  232. case 0:
  233. cl.set(5, 31);
  234. break;
  235. case 1:
  236. int year = cl.get(1);
  237. if (isLeapYear(year))
  238. cl.set(5, 29);
  239. else {
  240. cl.set(5, 28);
  241. }
  242. break;
  243. case 2:
  244. cl.set(5, 31);
  245. break;
  246. case 3:
  247. cl.set(5, 30);
  248. break;
  249. case 4:
  250. cl.set(5, 31);
  251. break;
  252. case 5:
  253. cl.set(5, 30);
  254. break;
  255. case 6:
  256. cl.set(5, 31);
  257. break;
  258. case 7:
  259. cl.set(5, 31);
  260. break;
  261. case 8:
  262. cl.set(5, 30);
  263. break;
  264. case 9:
  265. cl.set(5, 31);
  266. break;
  267. case 10:
  268. cl.set(5, 30);
  269. break;
  270. case 11:
  271. cl.set(5, 31);
  272. }
  273. date = cl.getTime();
  274. returndate = getFormatTimeString(date, outFormat);
  275. return returndate;
  276. }
  277. public static String getMonthLastDay(String fmt) {
  278. String returndate = "";
  279. Date date = null;
  280. Calendar cl = Calendar.getInstance();
  281. switch (cl.get(2)) {
  282. case 0:
  283. cl.set(5, 31);
  284. break;
  285. case 1:
  286. int year = cl.get(1);
  287. if (isLeapYear(year))
  288. cl.set(5, 29);
  289. else {
  290. cl.set(5, 28);
  291. }
  292. break;
  293. case 2:
  294. cl.set(5, 31);
  295. break;
  296. case 3:
  297. cl.set(5, 30);
  298. break;
  299. case 4:
  300. cl.set(5, 31);
  301. break;
  302. case 5:
  303. cl.set(5, 30);
  304. break;
  305. case 6:
  306. cl.set(5, 31);
  307. break;
  308. case 7:
  309. cl.set(5, 31);
  310. break;
  311. case 8:
  312. cl.set(5, 30);
  313. break;
  314. case 9:
  315. cl.set(5, 31);
  316. break;
  317. case 10:
  318. cl.set(5, 30);
  319. break;
  320. case 11:
  321. cl.set(5, 31);
  322. }
  323. date = cl.getTime();
  324. returndate = getFormatTimeString(date, fmt);
  325. return returndate;
  326. }
  327. public static Date getNextMonthFirstDate() {
  328. Calendar calendar = Calendar.getInstance();
  329. calendar.set(Calendar.DAY_OF_MONTH, 1);
  330. calendar.set(Calendar.HOUR_OF_DAY, 0);
  331. calendar.set(Calendar.MINUTE, 0);
  332. calendar.set(Calendar.SECOND, 0);
  333. calendar.add(Calendar.MONTH, 1);
  334. return calendar.getTime();
  335. }
  336. public static Date getFirstDate() {
  337. Calendar curDateCal = Calendar.getInstance();
  338. curDateCal.set(Calendar.DAY_OF_MONTH, 1);
  339. curDateCal.set(Calendar.HOUR_OF_DAY, 0);
  340. curDateCal.set(Calendar.MINUTE, 0);
  341. curDateCal.set(Calendar.SECOND, 0);
  342. Date curDate = curDateCal.getTime();
  343. return curDate;
  344. }
  345. public static String getNextMonthFirstDay(String fmt) {
  346. String returndate = "";
  347. Date date = getNextMonthFirstDate();
  348. returndate = getFormatTimeString(date, fmt);
  349. return returndate;
  350. }
  351. public static boolean compareDate(String fistDate, String secondDate, String format)
  352. throws ParseException {
  353. boolean flag = false;
  354. Date fist = null;
  355. Date second = null;
  356. fist = getDateFromString(fistDate, format);
  357. second = getDateFromString(secondDate, format);
  358. if (fist.before(second)) {
  359. flag = true;
  360. }
  361. return flag;
  362. }
  363. public static boolean isRightDate(String value, String varValue) {
  364. try {
  365. SimpleDateFormat format = new SimpleDateFormat(varValue);
  366. format.setLenient(false);
  367. format.parse(value);
  368. } catch (ParseException e) {
  369. return false;
  370. }
  371. return true;
  372. }
  373. public static int getCurrentMonthDay() {
  374. Calendar a = Calendar.getInstance();
  375. a.set(Calendar.DATE, 1);
  376. a.roll(Calendar.DATE, -1);
  377. int maxDate = a.get(Calendar.DATE);
  378. return maxDate;
  379. }
  380. public static void main(String[] args) throws ParseException {
  381. // SimpleDateFormat sf = new SimpleDateFormat(DateUtil.DATE_FORMATE_STRING_A);
  382. // Calendar c = Calendar.getInstance();
  383. // c.setTime(DateUtil.getDateFromString("2021-12-03",DateUtil.DATE_FORMATE_STRING_A));
  384. // c.add(Calendar.DAY_OF_MONTH, 125);
  385. // System.out.println("增加一天后日期:"+sf.format(c.getTime()));
  386. System.out.println("2021-12-07".compareTo(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B)));
  387. }
  388. public static String getAddDayString(Date date, String pattern, int days) {
  389. SimpleDateFormat sf = new SimpleDateFormat(pattern);
  390. Calendar c = Calendar.getInstance();
  391. c.setTime(date);
  392. c.add(Calendar.DAY_OF_MONTH, days);
  393. return sf.format(c.getTime());
  394. }
  395. public static String getAddDayStringB(Date date, int days) {
  396. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_B);
  397. Calendar c = Calendar.getInstance();
  398. c.setTime(date);
  399. c.add(Calendar.DAY_OF_MONTH, days);
  400. return sf.format(c.getTime());
  401. }
  402. public static String getAddDayStringA(Date date, int days) {
  403. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
  404. Calendar c = Calendar.getInstance();
  405. c.setTime(date);
  406. c.add(Calendar.DAY_OF_MONTH, days);
  407. return sf.format(c.getTime());
  408. }
  409. public static String getAddHoursStringA(Date date, int hours) {
  410. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
  411. Calendar c = Calendar.getInstance();
  412. c.setTime(date);
  413. c.add(Calendar.HOUR_OF_DAY, hours);
  414. return sf.format(c.getTime());
  415. }
  416. public static String getAddMonthStringA(Date date, int month) {
  417. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
  418. Calendar c = Calendar.getInstance();
  419. c.setTime(date);
  420. c.add(Calendar.MONTH, month);
  421. return sf.format(c.getTime());
  422. }
  423. /**
  424. * 在给定的日期加上或减去指定月份后的日期
  425. *
  426. * @param sourceDate 原始时间
  427. * @param month 要调整的月份,向前为负数,向后为正数
  428. * @return
  429. */
  430. public static Date stepMonth(Date sourceDate, int month) {
  431. Calendar c = Calendar.getInstance();
  432. c.setTime(sourceDate);
  433. c.add(Calendar.MONTH, month);
  434. return c.getTime();
  435. }
  436. /**
  437. * 在给定的日期加上或减去指定天数后的日期
  438. *
  439. * @param sourceDate 原始时间
  440. * @param day 要调整的月份,向前为负数,向后为正数
  441. * @return
  442. */
  443. public static Date stepDay(Date sourceDate, int day) {
  444. Calendar c = Calendar.getInstance();
  445. c.setTime(sourceDate);
  446. c.add(Calendar.DATE, day);
  447. return c.getTime();
  448. }
  449. public static String dateTimeToDate(String dateTime) {
  450. String dateStr = "";
  451. try {
  452. Date date = getDateFromString(dateTime, DATE_FORMATE_STRING_A);
  453. dateStr = getFormatTimeString(date, DATE_FORMATE_STRING_B);
  454. } catch (ParseException e) {
  455. dateStr = dateTime;
  456. }
  457. return dateStr;
  458. }
  459. public static int getYear() {
  460. Date date = getCurrentDate();
  461. Calendar calendar = Calendar.getInstance();
  462. calendar.setTime(date);
  463. return calendar.get(Calendar.YEAR);
  464. }
  465. public static int getMonth() {
  466. Date date = getCurrentDate();
  467. Calendar calendar = Calendar.getInstance();
  468. calendar.setTime(date);
  469. return calendar.get(Calendar.MONTH) + 1;
  470. }
  471. /**
  472. * 判断时间是否在时间段内
  473. *
  474. * @param nowTime
  475. * @param beginTime
  476. * @param endTime
  477. * @return
  478. */
  479. public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
  480. Calendar date = Calendar.getInstance();
  481. date.setTime(nowTime);
  482. Calendar begin = Calendar.getInstance();
  483. begin.setTime(beginTime);
  484. Calendar end = Calendar.getInstance();
  485. end.setTime(endTime);
  486. if (date.after(begin) && date.before(end)) {
  487. return true;
  488. } else if (nowTime.compareTo(beginTime) == 0 || nowTime.compareTo(endTime) == 0) {
  489. return true;
  490. } else {
  491. return false;
  492. }
  493. }
  494. //获取两个日期之间的天数
  495. public static int daysBetween(Date now, Date returnDate) {
  496. Calendar cNow = Calendar.getInstance();
  497. Calendar cReturnDate = Calendar.getInstance();
  498. cNow.setTime(now);
  499. cReturnDate.setTime(returnDate);
  500. setTimeToMidnight(cNow);
  501. setTimeToMidnight(cReturnDate);
  502. long todayMs = cNow.getTimeInMillis();
  503. long returnMs = cReturnDate.getTimeInMillis();
  504. long intervalMs = todayMs - returnMs;
  505. return millisecondsToDays(intervalMs);
  506. }
  507. //获取两个日期之间的毫秒数
  508. private static void setTimeToMidnight(Calendar calendar) {
  509. calendar.set(Calendar.HOUR_OF_DAY, 0);
  510. calendar.set(Calendar.MINUTE, 0);
  511. calendar.set(Calendar.SECOND, 0);
  512. }
  513. //获取两个日期之间的分钟数
  514. private static int millisecondsToDays(long intervalMs) {
  515. return (int) (intervalMs / (1000 * 86400));
  516. }
  517. /**
  518. *    *字符串的日期格式的计算
  519. *
  520. */
  521. public static int daysBetween(String smdate, String bdate) {
  522. long between_days = 0;
  523. try {
  524. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  525. Calendar cal = Calendar.getInstance();
  526. cal.setTime(sdf.parse(smdate));
  527. long time1 = cal.getTimeInMillis();
  528. cal.setTime(sdf.parse(bdate));
  529. long time2 = cal.getTimeInMillis();
  530. between_days = (time2 - time1) / (1000 * 3600 * 24);
  531. } catch (Exception e) {
  532. e.printStackTrace();
  533. }
  534. return Integer.parseInt(String.valueOf(between_days));
  535. }
  536. public static double dayCompare(Date fromDate, Date toDate) {
  537. double resMonth = 0.0;
  538. Calendar from = Calendar.getInstance();
  539. from.setTime(fromDate);
  540. Calendar to = Calendar.getInstance();
  541. to.setTime(toDate);
  542. //比较月份差 可能有整数 也会负数
  543. int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
  544. //比较年差
  545. int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
  546. //真实 相差月份
  547. result = result + month;
  548. //开始时间 2021-06-01 2021-08-05 result = 2 2021-08-01
  549. Calendar newFrom = Calendar.getInstance();
  550. newFrom.setTime(fromDate);
  551. newFrom.add(Calendar.MONTH, result);
  552. //如果加月份后 大于了当前时间 默认加 月份 -1 情况 12-19 21-01-10
  553. //这个是神的逻辑一定好好理解
  554. if (newFrom.getTime().getTime() > toDate.getTime()) {
  555. newFrom.setTime(fromDate);
  556. result = result - 1;
  557. newFrom.add(Calendar.MONTH, result);
  558. }
  559. // t1 2021-08-01 t2 2021-08-05
  560. long t1 = newFrom.getTime().getTime();
  561. long t2 = to.getTime().getTime();
  562. //相差毫秒
  563. double days = (t2 - t1) * 1.00 / (24 * 60 * 60 * 1000);
  564. BigDecimal tmpDays = new BigDecimal(days); //相差天数
  565. BigDecimal monthDay = null;
  566. Calendar newFromMaxDay = Calendar.getInstance();
  567. newFromMaxDay.set(newFrom.get(Calendar.YEAR), newFrom.get(Calendar.MONTH), 1, 0, 0, 0);
  568. newFromMaxDay.add(Calendar.MONTH, 1); //下个月1号
  569. //在当前月中 这块有问题
  570. if (toDate.getTime() < newFromMaxDay.getTime().getTime()) {
  571. monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
  572. return tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).doubleValue();
  573. }
  574. // 上月天数
  575. days = (newFromMaxDay.getTimeInMillis() - t1) * 1.00 / (24 * 60 * 60 * 1000);
  576. tmpDays = new BigDecimal(days);
  577. monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
  578. BigDecimal preRresMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP);
  579. //下月天数
  580. days = (t2 - newFromMaxDay.getTimeInMillis()) * 1.00 / (24 * 60 * 60 * 1000);
  581. tmpDays = new BigDecimal(days);
  582. monthDay = new BigDecimal(newFromMaxDay.getActualMaximum(Calendar.DAY_OF_MONTH));
  583. resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).add(preRresMonth).doubleValue();
  584. return resMonth;
  585. }
  586. /**
  587. * 通过时间秒毫秒数判断两个时间的间隔
  588. * @param date1
  589. * @param date2
  590. * @return
  591. */
  592. public static int differentDaysUp(Date date1,Date date2)
  593. {
  594. double days = ((date2.getTime() - date1.getTime()) / (1000*3600*24*1.00));
  595. return new Double(Math.ceil(days)).intValue();
  596. }
  597. }