DateUtil.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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 = "2050-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. return getNextMonthFirstDate(curDate, 1);
  353. }
  354. public static Date getNextMonthFirstDate(Date curDate, int monthCount) {
  355. Calendar curDateCal = Calendar.getInstance();
  356. curDateCal.setTime(curDate);
  357. curDateCal.set(Calendar.DAY_OF_MONTH, 1);
  358. curDateCal.set(Calendar.HOUR_OF_DAY, 0);
  359. curDateCal.set(Calendar.MINUTE, 0);
  360. curDateCal.set(Calendar.SECOND, 0);
  361. curDateCal.add(Calendar.MONTH, monthCount);
  362. return curDateCal.getTime();
  363. }
  364. public static Date getFirstDate() {
  365. Calendar curDateCal = Calendar.getInstance();
  366. curDateCal.set(Calendar.DAY_OF_MONTH, 1);
  367. curDateCal.set(Calendar.HOUR_OF_DAY, 0);
  368. curDateCal.set(Calendar.MINUTE, 0);
  369. curDateCal.set(Calendar.SECOND, 0);
  370. Date curDate = curDateCal.getTime();
  371. return curDate;
  372. }
  373. public static Date getFirstDate(Date curDate) {
  374. Calendar curDateCal = Calendar.getInstance();
  375. curDateCal.setTime(curDate);
  376. curDateCal.set(Calendar.DAY_OF_MONTH, 1);
  377. curDateCal.set(Calendar.HOUR_OF_DAY, 0);
  378. curDateCal.set(Calendar.MINUTE, 0);
  379. curDateCal.set(Calendar.SECOND, 0);
  380. return curDateCal.getTime();
  381. }
  382. public static Date getFirstDate(String curDate) {
  383. Date date = DateUtil.getDateFromStringB(curDate);
  384. Calendar curDateCal = Calendar.getInstance();
  385. curDateCal.setTime(date);
  386. curDateCal.set(Calendar.DAY_OF_MONTH, 1);
  387. curDateCal.set(Calendar.HOUR_OF_DAY, 0);
  388. curDateCal.set(Calendar.MINUTE, 0);
  389. curDateCal.set(Calendar.SECOND, 0);
  390. return curDateCal.getTime();
  391. }
  392. public static String getNextMonthFirstDay(String fmt) {
  393. String returndate = "";
  394. Date date = getNextMonthFirstDate();
  395. returndate = getFormatTimeString(date, fmt);
  396. return returndate;
  397. }
  398. public static boolean compareDate(String fistDate, String secondDate, String format)
  399. throws ParseException {
  400. boolean flag = false;
  401. Date fist = null;
  402. Date second = null;
  403. fist = getDateFromString(fistDate, format);
  404. second = getDateFromString(secondDate, format);
  405. if (fist.before(second)) {
  406. flag = true;
  407. }
  408. return flag;
  409. }
  410. public static boolean isRightDate(String value, String varValue) {
  411. try {
  412. SimpleDateFormat format = new SimpleDateFormat(varValue);
  413. format.setLenient(false);
  414. format.parse(value);
  415. } catch (ParseException e) {
  416. return false;
  417. }
  418. return true;
  419. }
  420. public static int getCurrentMonthDay() {
  421. Calendar a = Calendar.getInstance();
  422. a.set(Calendar.DATE, 1);
  423. a.roll(Calendar.DATE, -1);
  424. int maxDate = a.get(Calendar.DATE);
  425. return maxDate;
  426. }
  427. public static int getMonthDay(Date date) {
  428. Calendar a = Calendar.getInstance();
  429. a.setTime(date);
  430. return a.getActualMaximum(Calendar.DAY_OF_MONTH);
  431. }
  432. public static void main(String[] args) throws ParseException {
  433. // SimpleDateFormat sf = new SimpleDateFormat(DateUtil.DATE_FORMATE_STRING_A);
  434. // Calendar c = Calendar.getInstance();
  435. // c.setTime(DateUtil.getDateFromString("2021-12-03",DateUtil.DATE_FORMATE_STRING_A));
  436. // c.add(Calendar.DAY_OF_MONTH, 125);
  437. // System.out.println("增加一天后日期:"+sf.format(c.getTime()));
  438. System.out.println("2021-12-07".compareTo(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B)));
  439. }
  440. public static String getAddDayString(Date date, String pattern, int days) {
  441. SimpleDateFormat sf = new SimpleDateFormat(pattern);
  442. Calendar c = Calendar.getInstance();
  443. c.setTime(date);
  444. c.add(Calendar.DAY_OF_MONTH, days);
  445. return sf.format(c.getTime());
  446. }
  447. public static String getAddDayStringB(Date date, int days) {
  448. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_B);
  449. Calendar c = Calendar.getInstance();
  450. c.setTime(date);
  451. c.add(Calendar.DAY_OF_MONTH, days);
  452. return sf.format(c.getTime());
  453. }
  454. public static String getAddDayStringA(Date date, int days) {
  455. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
  456. Calendar c = Calendar.getInstance();
  457. c.setTime(date);
  458. c.add(Calendar.DAY_OF_MONTH, days);
  459. return sf.format(c.getTime());
  460. }
  461. public static String getAddHoursStringA(Date date, int hours) {
  462. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
  463. Calendar c = Calendar.getInstance();
  464. c.setTime(date);
  465. c.add(Calendar.HOUR_OF_DAY, hours);
  466. return sf.format(c.getTime());
  467. }
  468. public static String getAddMonthStringA(Date date, int month) {
  469. SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
  470. Calendar c = Calendar.getInstance();
  471. c.setTime(date);
  472. c.add(Calendar.MONTH, month);
  473. return sf.format(c.getTime());
  474. }
  475. /**
  476. * 在给定的日期加上或减去指定月份后的日期
  477. *
  478. * @param sourceDate 原始时间
  479. * @param month 要调整的月份,向前为负数,向后为正数
  480. * @return
  481. */
  482. public static Date stepMonth(Date sourceDate, int month) {
  483. Calendar c = Calendar.getInstance();
  484. c.setTime(sourceDate);
  485. c.add(Calendar.MONTH, month);
  486. return c.getTime();
  487. }
  488. /**
  489. * 在给定的日期加上或减去指定天数后的日期
  490. *
  491. * @param sourceDate 原始时间
  492. * @param day 要调整的月份,向前为负数,向后为正数
  493. * @return
  494. */
  495. public static Date stepDay(Date sourceDate, int day) {
  496. Calendar c = Calendar.getInstance();
  497. c.setTime(sourceDate);
  498. c.add(Calendar.DATE, day);
  499. return c.getTime();
  500. }
  501. public static String dateTimeToDate(String dateTime) {
  502. String dateStr = "";
  503. try {
  504. Date date = getDateFromString(dateTime, DATE_FORMATE_STRING_A);
  505. dateStr = getFormatTimeString(date, DATE_FORMATE_STRING_B);
  506. } catch (ParseException e) {
  507. dateStr = dateTime;
  508. }
  509. return dateStr;
  510. }
  511. public static int getYear() {
  512. Date date = getCurrentDate();
  513. Calendar calendar = Calendar.getInstance();
  514. calendar.setTime(date);
  515. return calendar.get(Calendar.YEAR);
  516. }
  517. public static int getMonth() {
  518. Date date = getCurrentDate();
  519. Calendar calendar = Calendar.getInstance();
  520. calendar.setTime(date);
  521. return calendar.get(Calendar.MONTH) + 1;
  522. }
  523. /**
  524. * 判断时间是否在时间段内
  525. *
  526. * @param nowTime
  527. * @param beginTime
  528. * @param endTime
  529. * @return
  530. */
  531. public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
  532. Calendar date = Calendar.getInstance();
  533. date.setTime(nowTime);
  534. Calendar begin = Calendar.getInstance();
  535. begin.setTime(beginTime);
  536. Calendar end = Calendar.getInstance();
  537. end.setTime(endTime);
  538. if (date.after(begin) && date.before(end)) {
  539. return true;
  540. } else if (nowTime.compareTo(beginTime) == 0 || nowTime.compareTo(endTime) == 0) {
  541. return true;
  542. } else {
  543. return false;
  544. }
  545. }
  546. //获取两个日期之间的天数
  547. public static int daysBetween(Date now, Date returnDate) {
  548. Calendar cNow = Calendar.getInstance();
  549. Calendar cReturnDate = Calendar.getInstance();
  550. cNow.setTime(now);
  551. cReturnDate.setTime(returnDate);
  552. setTimeToMidnight(cNow);
  553. setTimeToMidnight(cReturnDate);
  554. long todayMs = cNow.getTimeInMillis();
  555. long returnMs = cReturnDate.getTimeInMillis();
  556. long intervalMs = todayMs - returnMs;
  557. return millisecondsToDays(intervalMs);
  558. }
  559. //获取两个日期之间的毫秒数
  560. private static void setTimeToMidnight(Calendar calendar) {
  561. calendar.set(Calendar.HOUR_OF_DAY, 0);
  562. calendar.set(Calendar.MINUTE, 0);
  563. calendar.set(Calendar.SECOND, 0);
  564. }
  565. //获取两个日期之间的分钟数
  566. private static int millisecondsToDays(long intervalMs) {
  567. return (int) (intervalMs / (1000 * 86400));
  568. }
  569. /**
  570. *    *字符串的日期格式的计算
  571. */
  572. public static int daysBetween(String smdate, String bdate) {
  573. long between_days = 0;
  574. try {
  575. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  576. Calendar cal = Calendar.getInstance();
  577. cal.setTime(sdf.parse(smdate));
  578. long time1 = cal.getTimeInMillis();
  579. cal.setTime(sdf.parse(bdate));
  580. long time2 = cal.getTimeInMillis();
  581. between_days = (time2 - time1) / (1000 * 3600 * 24);
  582. } catch (Exception e) {
  583. e.printStackTrace();
  584. }
  585. return Integer.parseInt(String.valueOf(between_days));
  586. }
  587. // public static double dayCompare(Date fromDate, Date toDate) {
  588. // double resMonth = 0.0;
  589. // Calendar from = Calendar.getInstance();
  590. // from.setTime(fromDate);
  591. // Calendar to = Calendar.getInstance();
  592. // to.setTime(toDate);
  593. // //比较月份差 可能有整数 也会负数
  594. // int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
  595. // //比较年差
  596. // int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
  597. //
  598. // //真实 相差月份
  599. // result = result + month;
  600. //
  601. // //开始时间 2021-06-01 2021-08-05 result = 2 2021-08-01
  602. // Calendar newFrom = Calendar.getInstance();
  603. // newFrom.setTime(fromDate);
  604. // newFrom.add(Calendar.MONTH, result);
  605. // //如果加月份后 大于了当前时间 默认加 月份 -1 情况 12-19 21-01-10
  606. // //这个是神的逻辑一定好好理解
  607. // if (newFrom.getTime().getTime() > toDate.getTime()) {
  608. // newFrom.setTime(fromDate);
  609. // result = result - 1;
  610. // newFrom.add(Calendar.MONTH, result);
  611. // }
  612. //
  613. // // t1 2021-08-01 t2 2021-08-05
  614. // long t1 = newFrom.getTime().getTime();
  615. // long t2 = to.getTime().getTime();
  616. // //相差毫秒
  617. // double days = (t2 - t1) * 1.00 / (24 * 60 * 60 * 1000);
  618. // BigDecimal tmpDays = new BigDecimal(days); //相差天数
  619. // BigDecimal monthDay = null;
  620. // Calendar newFromMaxDay = Calendar.getInstance();
  621. // newFromMaxDay.set(newFrom.get(Calendar.YEAR), newFrom.get(Calendar.MONTH), 1, 0, 0, 0);
  622. // newFromMaxDay.add(Calendar.MONTH, 1); //下个月1号
  623. // //在当前月中 这块有问题
  624. // if (toDate.getTime() < newFromMaxDay.getTime().getTime()) {
  625. // monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
  626. // return tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).doubleValue();
  627. // }
  628. // // 上月天数
  629. // days = (newFromMaxDay.getTimeInMillis() - t1) * 1.00 / (24 * 60 * 60 * 1000);
  630. // tmpDays = new BigDecimal(days);
  631. // monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
  632. // BigDecimal preRresMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP);
  633. //
  634. // //下月天数
  635. // days = (t2 - newFromMaxDay.getTimeInMillis()) * 1.00 / (24 * 60 * 60 * 1000);
  636. // tmpDays = new BigDecimal(days);
  637. // monthDay = new BigDecimal(newFromMaxDay.getActualMaximum(Calendar.DAY_OF_MONTH));
  638. // resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).add(preRresMonth).doubleValue();
  639. // return resMonth;
  640. // }
  641. /**
  642. * 通过时间秒毫秒数判断两个时间的间隔
  643. *
  644. * @param date1
  645. * @param date2
  646. * @return
  647. */
  648. public static int differentDaysUp(Date date1, Date date2) {
  649. double days = ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24 * 1.00));
  650. return new Double(Math.ceil(days)).intValue();
  651. }
  652. /**
  653. * 获取两个日期之间的所有月份 (年月)
  654. *
  655. * @param startTime
  656. * @param endTime
  657. * @return:list
  658. */
  659. public static List<String> getMonthBetweenDate(Date startTime, Date endTime) {
  660. return getMonthBetweenDate(DateUtil.getFormatTimeStringA(startTime), DateUtil.getFormatTimeStringA(endTime));
  661. }
  662. /**
  663. * 获取两个日期之间的所有月份 (年月)
  664. *
  665. * @param startTime
  666. * @param endTime
  667. * @return:list
  668. */
  669. public static List<String> getMonthBetweenDate(String startTime, String endTime) {
  670. SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMATE_STRING_Q);
  671. // 声明保存日期集合
  672. List<String> list = new ArrayList<>();
  673. try {
  674. // 转化成日期类型
  675. Date startDate = sdf.parse(startTime);
  676. Date endDate = sdf.parse(endTime);
  677. //用Calendar 进行日期比较判断
  678. Calendar calendar = Calendar.getInstance();
  679. while (startDate.getTime() <= endDate.getTime()) {
  680. // 把日期添加到集合
  681. list.add(sdf.format(startDate));
  682. // 设置日期
  683. calendar.setTime(startDate);
  684. //把月数增加 1
  685. calendar.add(Calendar.MONTH, 1);
  686. // 获取增加后的日期
  687. startDate = calendar.getTime();
  688. }
  689. } catch (Exception e) {
  690. e.printStackTrace();
  691. }
  692. return list;
  693. }
  694. /**
  695. * 除去 小时 分 秒
  696. *
  697. * @param time
  698. * @return
  699. */
  700. public static Date timeToDate(Date time) {
  701. Calendar calendar = Calendar.getInstance();
  702. calendar.setTime(time);
  703. setTimeToMidnight(calendar);
  704. return calendar.getTime();
  705. }
  706. /**
  707. * 除去 小时 分 秒
  708. *
  709. * deadtime 本来就少了一秒
  710. *
  711. * @param time
  712. * @return
  713. */
  714. public static Date deadTimeToDate(Date time) {
  715. Calendar calendar = Calendar.getInstance();
  716. calendar.setTime(time);
  717. calendar.add(Calendar.SECOND,1);
  718. setTimeToMidnight(calendar);
  719. return calendar.getTime();
  720. }
  721. public static boolean sameMonthDay(Date startDate, Date endDate) {
  722. Calendar startCalendar = Calendar.getInstance();
  723. startCalendar.setTime(startDate);
  724. Calendar endCalender = Calendar.getInstance();
  725. endCalender.setTime(endDate);
  726. if (
  727. startCalendar.get(Calendar.DAY_OF_MONTH) == endCalender.get(Calendar.DAY_OF_MONTH)
  728. && startCalendar.get(Calendar.HOUR_OF_DAY) == endCalender.get(Calendar.HOUR_OF_DAY)
  729. ) {
  730. return true;
  731. }
  732. return false;
  733. }
  734. public static double dayCompare(Date fromDate, Date toDate) {
  735. return dayCompare(fromDate, toDate, false);
  736. }
  737. /**
  738. * 计算 fromDate 2023-01-12 toDate 2023-09-15
  739. * 2023-01-12--->2023-01-01 ---> 2023-09-01 ------> 2023-09-15
  740. * fromDate ---> fromDateFirstDate ---> toDateFirstDate ----> toDate
  741. *
  742. * @param fromDate
  743. * @param toDate
  744. * @return
  745. */
  746. public static double dayCompare(Date fromDate, Date toDate, boolean plusOneSec) {
  747. if (plusOneSec) {
  748. Calendar toD = Calendar.getInstance();
  749. toD.setTime(toDate);
  750. toD.add(Calendar.SECOND, 1);
  751. toDate = toD.getTime();
  752. }
  753. //todo 需要计算三端时间 相加即可
  754. Date fromDateFirstDate = fromDate; // 第一个1日
  755. Date toDateFirstDate = toDate; // 最后一个1日
  756. boolean firstDay = true;
  757. //todo 1.0 计算 fromDateFirstDate
  758. Calendar fromDateCal = Calendar.getInstance();
  759. fromDateCal.setTime(fromDate);
  760. fromDateCal.set(Calendar.DAY_OF_MONTH, 1);
  761. fromDateCal.set(Calendar.HOUR_OF_DAY, 0);
  762. fromDateCal.set(Calendar.MINUTE, 0);
  763. if (fromDate.getTime() > fromDateCal.getTime().getTime()) {
  764. fromDateCal.add(Calendar.MONTH, 1);
  765. firstDay = false;
  766. fromDateFirstDate = fromDateCal.getTime();
  767. }
  768. //todo 2.0 计算 toDateFirstDate
  769. Calendar toDateCal = Calendar.getInstance();
  770. toDateCal.setTime(toDate);
  771. toDateCal.set(Calendar.DAY_OF_MONTH, 1);
  772. toDateCal.set(Calendar.HOUR_OF_DAY, 0);
  773. toDateCal.set(Calendar.MINUTE, 0);
  774. if (toDate.getTime() > toDateCal.getTime().getTime()) {
  775. toDateFirstDate = toDateCal.getTime();
  776. }
  777. // todo 3.0 计算整数月 fromDateFirstDate ---> toDateFirstDate
  778. Calendar from = Calendar.getInstance();
  779. from.setTime(fromDateFirstDate);
  780. Calendar to = Calendar.getInstance();
  781. to.setTime(toDateFirstDate);
  782. //比较月份差 可能有整数 也会负数
  783. int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
  784. //比较年差
  785. int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
  786. //真实 相差月份
  787. result = result + month;
  788. //todo 3.1 如果 fromDate 和toDate 是同一天 则直接返回整月,不再计算 4.0 和5.0
  789. if (DateUtil.sameMonthDay(fromDate, toDate)) {
  790. return firstDay ? result : result + 1;
  791. }
  792. // todo 4.0 计算 fromDate ---> fromDateFirstDate 的月份
  793. double days = (fromDateFirstDate.getTime() - fromDate.getTime()) * 1.00 / (24 * 60 * 60 * 1000);
  794. BigDecimal tmpDays = new BigDecimal(days); //相差天数
  795. BigDecimal monthDay = new BigDecimal(DateUtil.getMonthDay(fromDate));
  796. BigDecimal resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result));
  797. // todo 5.0 计算 toDateFirstDate ----> toDate 月份
  798. days = (toDate.getTime() - toDateFirstDate.getTime()) * 1.00 / (24 * 60 * 60 * 1000);
  799. tmpDays = new BigDecimal(days); //相差天数
  800. monthDay = new BigDecimal(DateUtil.getMonthDay(toDate));
  801. resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(resMonth);
  802. return resMonth.doubleValue();
  803. }
  804. public static Date getTargetEndTime(double month, Date startDate) {
  805. Calendar endDate = Calendar.getInstance();
  806. endDate.setTime(startDate);
  807. Double intMonth = Math.floor(month);
  808. endDate.add(Calendar.MONTH, intMonth.intValue());
  809. double doubleMonth = month - intMonth;
  810. if (doubleMonth <= 0) {
  811. return endDate.getTime();
  812. }
  813. int futureDay = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);
  814. Double hour = doubleMonth * futureDay * 24;
  815. endDate.add(Calendar.HOUR_OF_DAY, hour.intValue());
  816. return endDate.getTime();
  817. }
  818. public static String getNextSecTime(String time) {
  819. Date tTime = getDateFromStringA(time);
  820. return getNextSecTime(tTime);
  821. }
  822. public static Date getNextSecDateTime(Date time) {
  823. Calendar calendar = Calendar.getInstance();
  824. calendar.setTime(time);
  825. calendar.add(Calendar.SECOND, 1);
  826. return calendar.getTime();
  827. }
  828. public static String getNextSecTime(Date time) {
  829. Calendar calendar = Calendar.getInstance();
  830. calendar.setTime(time);
  831. calendar.add(Calendar.SECOND, 1);
  832. return getFormatTimeStringA(calendar.getTime());
  833. }
  834. public static String getPreSecTime(String time) {
  835. Date tTime = getDateFromStringA(time);
  836. return getPreSecTime(tTime);
  837. }
  838. public static String getPreSecTime(Date time) {
  839. Calendar calendar = Calendar.getInstance();
  840. calendar.setTime(time);
  841. calendar.add(Calendar.SECOND, -1);
  842. return getFormatTimeStringA(calendar.getTime());
  843. }
  844. }