DateHelper.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. package com.persagy.dmp.common.utils;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.persagy.dmp.common.constant.DateStyle;
  4. import lombok.extern.slf4j.Slf4j;
  5. import java.math.BigDecimal;
  6. import java.text.ParseException;
  7. import java.text.ParsePosition;
  8. import java.text.SimpleDateFormat;
  9. import java.util.*;
  10. /**
  11. * 日期助手
  12. * @author Charlie Yu
  13. * @date 2021-09-02
  14. */
  15. @Slf4j
  16. public class DateHelper {
  17. private static final ThreadLocal<SimpleDateFormat> THREAD_LOCAL = new ThreadLocal<>();
  18. private static final Object INSTANCE = new Object();
  19. /**
  20. * 获取SimpleDateFormat
  21. *
  22. * @param pattern 日期格式
  23. * @return SimpleDateFormat对象
  24. * @throws RuntimeException 异常:非法日期格式
  25. */
  26. private static SimpleDateFormat getDateFormat(String pattern) throws RuntimeException {
  27. SimpleDateFormat dateFormat = THREAD_LOCAL.get();
  28. if (dateFormat == null) {
  29. synchronized (INSTANCE) {
  30. if (dateFormat == null) {
  31. dateFormat = new SimpleDateFormat(pattern);
  32. dateFormat.setLenient(false);
  33. THREAD_LOCAL.set(dateFormat);
  34. }
  35. }
  36. }
  37. dateFormat.applyPattern(pattern);
  38. return dateFormat;
  39. }
  40. /**
  41. * 获取日期中的某数值。如获取月份
  42. *
  43. * @param date 日期
  44. * @param dateType 日期格式
  45. * @return 数值
  46. */
  47. private static int getInteger(Date date, int dateType) {
  48. int num = 0;
  49. Calendar calendar = Calendar.getInstance();
  50. if (date != null) {
  51. calendar.setTime(date);
  52. num = calendar.get(dateType);
  53. }
  54. return num;
  55. }
  56. /**
  57. * 增加日期中某类型的某数值。如增加日期
  58. *
  59. * @param date 日期字符串
  60. * @param dateType 类型
  61. * @param amount 数值
  62. * @return 计算后日期字符串
  63. */
  64. private static String addInteger(String date, int dateType, int amount) {
  65. String dateString = null;
  66. DateStyle dateStyle = getDateStyle(date);
  67. if (dateStyle != null) {
  68. Date myDate = stringToDate(date, dateStyle);
  69. myDate = addInteger(myDate, dateType, amount);
  70. dateString = dateToString(myDate, dateStyle);
  71. }
  72. return dateString;
  73. }
  74. /**
  75. * 增加日期中某类型的某数值。如增加日期
  76. *
  77. * @param date 日期
  78. * @param dateType 类型
  79. * @param amount 数值
  80. * @return 计算后日期
  81. */
  82. private static Date addInteger(Date date, int dateType, int amount) {
  83. Date myDate = null;
  84. if (date != null) {
  85. Calendar calendar = Calendar.getInstance();
  86. calendar.setTime(date);
  87. calendar.add(dateType, amount);
  88. myDate = calendar.getTime();
  89. }
  90. return myDate;
  91. }
  92. /**
  93. * 将时间戳转换为时间
  94. */
  95. public static String stampToDate(String s){
  96. if (StrUtil.isEmpty(s)) {
  97. return null;
  98. }
  99. String res;
  100. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  101. long lt = new Long(s);
  102. Date date = new Date(lt);
  103. res = simpleDateFormat.format(date);
  104. return res;
  105. }
  106. /**
  107. * 获取精确的日期
  108. *
  109. * @param timestamps 时间long集合
  110. * @return 日期
  111. */
  112. private static Date getAccurateDate(List<Long> timestamps) {
  113. Date date = null;
  114. long timestamp = 0;
  115. Map<Long, long[]> map = new HashMap<>(16);
  116. List<Long> absoluteValues = new ArrayList<Long>();
  117. if (timestamps != null && timestamps.size() > 0) {
  118. if (timestamps.size() > 1) {
  119. for (int i = 0; i < timestamps.size(); i++) {
  120. for (int j = i + 1; j < timestamps.size(); j++) {
  121. long absoluteValue = Math.abs(timestamps.get(i) - timestamps.get(j));
  122. absoluteValues.add(absoluteValue);
  123. long[] timestampTmp = { timestamps.get(i), timestamps.get(j) };
  124. map.put(absoluteValue, timestampTmp);
  125. }
  126. }
  127. // 有可能有相等的情况。如2012-11和2012-11-01。时间戳是相等的。此时minAbsoluteValue为0
  128. // 因此不能将minAbsoluteValue取默认值0
  129. long minAbsoluteValue = -1;
  130. if (!absoluteValues.isEmpty()) {
  131. minAbsoluteValue = absoluteValues.get(0);
  132. for (int i = 1; i < absoluteValues.size(); i++) {
  133. if (minAbsoluteValue > absoluteValues.get(i)) {
  134. minAbsoluteValue = absoluteValues.get(i);
  135. }
  136. }
  137. }
  138. if (minAbsoluteValue != -1) {
  139. long[] timestampsLastTmp = map.get(minAbsoluteValue);
  140. long dateOne = timestampsLastTmp[0];
  141. long dateTwo = timestampsLastTmp[1];
  142. if (absoluteValues.size() > 1) {
  143. timestamp = Math.abs(dateOne) > Math.abs(dateTwo) ? dateOne : dateTwo;
  144. }
  145. }
  146. } else {
  147. timestamp = timestamps.get(0);
  148. }
  149. }
  150. if (timestamp != 0) {
  151. date = new Date(timestamp);
  152. }
  153. return date;
  154. }
  155. /**
  156. * 判断字符串是否为日期字符串
  157. *
  158. * @param date 日期字符串
  159. * @return true or false
  160. */
  161. public static boolean isDate(String date) {
  162. boolean isDate = false;
  163. if (date != null) {
  164. if (getDateStyle(date) != null) {
  165. isDate = true;
  166. }
  167. }
  168. return isDate;
  169. }
  170. /**
  171. * 获取日期字符串的日期风格。失敗返回null。
  172. *
  173. * @param date 日期字符串
  174. * @return 日期风格
  175. */
  176. public static DateStyle getDateStyle(String date) {
  177. DateStyle dateStyle = null;
  178. Map<Long, DateStyle> map = new HashMap<>(16);
  179. List<Long> timestamps = new ArrayList<>();
  180. for (DateStyle style : DateStyle.values()) {
  181. if (style.isShowOnly()) {
  182. continue;
  183. }
  184. Date dateTmp = null;
  185. if (date != null) {
  186. try {
  187. ParsePosition pos = new ParsePosition(0);
  188. dateTmp = getDateFormat(style.getValue()).parse(date, pos);
  189. if (pos.getIndex() != date.length()) {
  190. dateTmp = null;
  191. }
  192. } catch (Exception e) {
  193. }
  194. }
  195. if (dateTmp != null) {
  196. timestamps.add(dateTmp.getTime());
  197. map.put(dateTmp.getTime(), style);
  198. }
  199. }
  200. Date accurateDate = getAccurateDate(timestamps);
  201. if (accurateDate != null) {
  202. dateStyle = map.get(accurateDate.getTime());
  203. }
  204. return dateStyle;
  205. }
  206. /**
  207. * 将日期字符串转化为日期。失败返回null。
  208. *
  209. * @param date 日期字符串
  210. * @return 日期
  211. */
  212. public static Date stringToDate(String date) {
  213. DateStyle dateStyle = getDateStyle(date);
  214. return stringToDate(date, dateStyle);
  215. }
  216. /**
  217. * 将日期字符串转化为日期。失败返回null。
  218. *
  219. * @param date 日期字符串
  220. * @param pattern 日期格式
  221. * @return 日期
  222. */
  223. public static Date stringToDate(String date, String pattern) {
  224. Date myDate = null;
  225. if (date != null) {
  226. try {
  227. myDate = getDateFormat(pattern).parse(date);
  228. } catch (Exception e) {
  229. }
  230. }
  231. return myDate;
  232. }
  233. /**
  234. * 将日期字符串转化为日期。失败返回null。
  235. *
  236. * @param date 日期字符串
  237. * @param dateStyle 日期风格
  238. * @return 日期
  239. */
  240. public static Date stringToDate(String date, DateStyle dateStyle) {
  241. Date myDate = null;
  242. if (dateStyle != null) {
  243. myDate = stringToDate(date, dateStyle.getValue());
  244. }
  245. return myDate;
  246. }
  247. /**
  248. * 将日期转化为日期字符串。失败返回null。
  249. *
  250. * @param date 日期
  251. * @param pattern 日期格式
  252. * @return 日期字符串
  253. */
  254. public static String dateToString(Date date, String pattern) {
  255. String dateString = null;
  256. if (date != null) {
  257. try {
  258. dateString = getDateFormat(pattern).format(date);
  259. } catch (Exception e) {
  260. }
  261. }
  262. return dateString;
  263. }
  264. /**
  265. * 将日期转化为日期字符串。失败返回null。
  266. *
  267. * @param date 日期
  268. * @param dateStyle 日期风格
  269. * @return 日期字符串
  270. */
  271. public static String dateToString(Date date, DateStyle dateStyle) {
  272. String dateString = null;
  273. if (dateStyle != null) {
  274. dateString = dateToString(date, dateStyle.getValue());
  275. }
  276. return dateString;
  277. }
  278. /**
  279. * 将日期字符串转化为另一日期字符串。失败返回null。
  280. *
  281. * @param date 旧日期字符串
  282. * @param newPattern 新日期格式
  283. * @return 新日期字符串
  284. */
  285. public static String stringToString(String date, String newPattern) {
  286. DateStyle oldDateStyle = getDateStyle(date);
  287. return stringToString(date, oldDateStyle, newPattern);
  288. }
  289. /**
  290. * 将日期字符串转化为另一日期字符串。失败返回null。
  291. *
  292. * @param date 旧日期字符串
  293. * @param newDateStyle 新日期风格
  294. * @return 新日期字符串
  295. */
  296. public static String stringToString(String date, DateStyle newDateStyle) {
  297. DateStyle oldDateStyle = getDateStyle(date);
  298. return stringToString(date, oldDateStyle, newDateStyle);
  299. }
  300. /**
  301. * 将日期字符串转化为另一日期字符串。失败返回null。
  302. *
  303. * @param date 旧日期字符串
  304. * @param olddPattern 旧日期格式
  305. * @param newPattern 新日期格式
  306. * @return 新日期字符串
  307. */
  308. public static String stringToString(String date, String olddPattern, String newPattern) {
  309. return dateToString(stringToDate(date, olddPattern), newPattern);
  310. }
  311. /**
  312. * 将日期字符串转化为另一日期字符串。失败返回null。
  313. *
  314. * @param date 旧日期字符串
  315. * @param olddDteStyle 旧日期风格
  316. * @param newParttern 新日期格式
  317. * @return 新日期字符串
  318. */
  319. public static String stringToString(String date, DateStyle olddDteStyle, String newParttern) {
  320. String dateString = null;
  321. if (olddDteStyle != null) {
  322. dateString = stringToString(date, olddDteStyle.getValue(), newParttern);
  323. }
  324. return dateString;
  325. }
  326. /**
  327. * 将日期字符串转化为另一日期字符串。失败返回null。
  328. *
  329. * @param date 旧日期字符串
  330. * @param olddPattern 旧日期格式
  331. * @param newDateStyle 新日期风格
  332. * @return 新日期字符串
  333. */
  334. public static String stringToString(String date, String olddPattern, DateStyle newDateStyle) {
  335. String dateString = null;
  336. if (newDateStyle != null) {
  337. dateString = stringToString(date, olddPattern, newDateStyle.getValue());
  338. }
  339. return dateString;
  340. }
  341. /**
  342. * 将日期字符串转化为另一日期字符串。失败返回null。
  343. *
  344. * @param date 旧日期字符串
  345. * @param olddDteStyle 旧日期风格
  346. * @param newDateStyle 新日期风格
  347. * @return 新日期字符串
  348. */
  349. public static String stringToString(String date, DateStyle olddDteStyle, DateStyle newDateStyle) {
  350. String dateString = null;
  351. if (olddDteStyle != null && newDateStyle != null) {
  352. dateString = stringToString(date, olddDteStyle.getValue(), newDateStyle.getValue());
  353. }
  354. return dateString;
  355. }
  356. /**
  357. * 增加日期的年份。失败返回null。
  358. *
  359. * @param date 日期
  360. * @param yearAmount 增加数量。可为负数
  361. * @return 增加年份后的日期字符串
  362. */
  363. public static String addYear(String date, int yearAmount) {
  364. return addInteger(date, Calendar.YEAR, yearAmount);
  365. }
  366. /**
  367. * 增加日期的年份。失败返回null。
  368. *
  369. * @param date 日期
  370. * @param yearAmount 增加数量。可为负数
  371. * @return 增加年份后的日期
  372. */
  373. public static Date addYear(Date date, int yearAmount) {
  374. return addInteger(date, Calendar.YEAR, yearAmount);
  375. }
  376. /**
  377. * 增加日期的月份。失败返回null。
  378. *
  379. * @param date 日期
  380. * @param monthAmount 增加数量。可为负数
  381. * @return 增加月份后的日期字符串
  382. */
  383. public static String addMonth(String date, int monthAmount) {
  384. return addInteger(date, Calendar.MONTH, monthAmount);
  385. }
  386. /**
  387. * 增加日期的月份。失败返回null。
  388. *
  389. * @param date 日期
  390. * @param monthAmount 增加数量。可为负数
  391. * @return 增加月份后的日期
  392. */
  393. public static Date addMonth(Date date, int monthAmount) {
  394. return addInteger(date, Calendar.MONTH, monthAmount);
  395. }
  396. /**
  397. * 增加日期的天数。失败返回null。
  398. *
  399. * @param date 日期字符串
  400. * @param dayAmount 增加数量。可为负数
  401. * @return 增加天数后的日期字符串
  402. */
  403. public static String addDay(String date, int dayAmount) {
  404. return addInteger(date, Calendar.DATE, dayAmount);
  405. }
  406. /**
  407. * 增加日期的天数。失败返回null。
  408. *
  409. * @param date 日期
  410. * @param dayAmount 增加数量。可为负数
  411. * @return 增加天数后的日期
  412. */
  413. public static Date addDay(Date date, int dayAmount) {
  414. return addInteger(date, Calendar.DATE, dayAmount);
  415. }
  416. /**
  417. * 增加日期的小时。失败返回null。
  418. *
  419. * @param date 日期字符串
  420. * @param hourAmount 增加数量。可为负数
  421. * @return 增加小时后的日期字符串
  422. */
  423. public static String addHour(String date, int hourAmount) {
  424. return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
  425. }
  426. /**
  427. * 增加日期的小时。失败返回null。
  428. *
  429. * @param date 日期
  430. * @param hourAmount 增加数量。可为负数
  431. * @return 增加小时后的日期
  432. */
  433. public static Date addHour(Date date, int hourAmount) {
  434. return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
  435. }
  436. /**
  437. * 增加日期的分钟。失败返回null。
  438. *
  439. * @param date 日期字符串
  440. * @param minuteAmount 增加数量。可为负数
  441. * @return 增加分钟后的日期字符串
  442. */
  443. public static String addMinute(String date, int minuteAmount) {
  444. return addInteger(date, Calendar.MINUTE, minuteAmount);
  445. }
  446. /**
  447. * 增加日期的分钟。失败返回null。
  448. *
  449. * @param date 日期
  450. * @param minuteAmount 增加数量。可为负数
  451. * @return 增加分钟后的日期
  452. */
  453. public static Date addMinute(Date date, int minuteAmount) {
  454. return addInteger(date, Calendar.MINUTE, minuteAmount);
  455. }
  456. /**
  457. * 增加日期的秒钟。失败返回null。
  458. *
  459. * @param date 日期字符串
  460. * @param secondAmount 增加数量。可为负数
  461. * @return 增加秒钟后的日期字符串
  462. */
  463. public static String addSecond(String date, int secondAmount) {
  464. return addInteger(date, Calendar.SECOND, secondAmount);
  465. }
  466. /**
  467. * 增加日期的秒钟。失败返回null。
  468. *
  469. * @param date 日期
  470. * @param secondAmount 增加数量。可为负数
  471. * @return 增加秒钟后的日期
  472. */
  473. public static Date addSecond(Date date, int secondAmount) {
  474. return addInteger(date, Calendar.SECOND, secondAmount);
  475. }
  476. /**
  477. * 获取日期的年份。失败返回0。
  478. *
  479. * @param date 日期字符串
  480. * @return 年份
  481. */
  482. public static int getYear(String date) {
  483. return getYear(stringToDate(date));
  484. }
  485. /**
  486. * 获取日期的年份。失败返回0。
  487. *
  488. * @param date 日期
  489. * @return 年份
  490. */
  491. public static int getYear(Date date) {
  492. return getInteger(date, Calendar.YEAR);
  493. }
  494. /**
  495. * 获取日期的月份。失败返回0。
  496. *
  497. * @param date 日期字符串
  498. * @return 月份
  499. */
  500. public static int getMonth(String date) {
  501. return getMonth(stringToDate(date));
  502. }
  503. /**
  504. * 获取日期的月份。失败返回0。
  505. *
  506. * @param date 日期
  507. * @return 月份
  508. */
  509. public static int getMonth(Date date) {
  510. return getInteger(date, Calendar.MONTH) + 1;
  511. }
  512. /**
  513. * 获取日期的天数。失败返回0。
  514. *
  515. * @param date 日期字符串
  516. * @return 天
  517. */
  518. public static int getDay(String date) {
  519. return getDay(stringToDate(date));
  520. }
  521. /**
  522. * 获取日期的天数。失败返回0。
  523. *
  524. * @param date 日期
  525. * @return 天
  526. */
  527. public static int getDay(Date date) {
  528. return getInteger(date, Calendar.DATE);
  529. }
  530. /**
  531. * 获取日期的小时。失败返回0。
  532. *
  533. * @param date 日期字符串
  534. * @return 小时
  535. */
  536. public static int getHour(String date) {
  537. return getHour(stringToDate(date));
  538. }
  539. /**
  540. * 获取日期的小时。失败返回0。
  541. *
  542. * @param date 日期
  543. * @return 小时
  544. */
  545. public static int getHour(Date date) {
  546. return getInteger(date, Calendar.HOUR_OF_DAY);
  547. }
  548. /**
  549. * 获取日期的分钟。失败返回0。
  550. *
  551. * @param date 日期字符串
  552. * @return 分钟
  553. */
  554. public static int getMinute(String date) {
  555. return getMinute(stringToDate(date));
  556. }
  557. /**
  558. * 获取日期的分钟。失败返回0。
  559. *
  560. * @param date 日期
  561. * @return 分钟
  562. */
  563. public static int getMinute(Date date) {
  564. return getInteger(date, Calendar.MINUTE);
  565. }
  566. /**
  567. * 获取日期的秒钟。失败返回0。
  568. *
  569. * @param date 日期字符串
  570. * @return 秒钟
  571. */
  572. public static int getSecond(String date) {
  573. return getSecond(stringToDate(date));
  574. }
  575. /**
  576. * 获取日期的秒钟。失败返回0。
  577. *
  578. * @param date 日期
  579. * @return 秒钟
  580. */
  581. public static int getSecond(Date date) {
  582. return getInteger(date, Calendar.SECOND);
  583. }
  584. /**
  585. * 获取日期 。默认yyyy-MM-dd格式。失败返回null。
  586. *
  587. * @param date 日期字符串
  588. * @return 日期
  589. */
  590. public static String getDate(String date) {
  591. return stringToString(date, DateStyle.YYYY_MM_DD);
  592. }
  593. /**
  594. * 获取日期。默认yyyy-MM-dd格式。失败返回null。
  595. *
  596. * @param date 日期
  597. * @return 日期
  598. */
  599. public static String getDate(Date date) {
  600. return dateToString(date, DateStyle.YYYY_MM_DD);
  601. }
  602. /**
  603. * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
  604. *
  605. * @param date 日期字符串
  606. * @return 时间
  607. */
  608. public static String getTime(String date) {
  609. return stringToString(date, DateStyle.HH_MM_SS);
  610. }
  611. /**
  612. * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
  613. *
  614. * @param date 日期
  615. * @return 时间
  616. */
  617. public static String getTime(Date date) {
  618. return dateToString(date, DateStyle.HH_MM_SS);
  619. }
  620. /**
  621. * 获取两个日期相差的天数
  622. *
  623. * @param date 日期字符串
  624. * @param otherDate 另一个日期字符串
  625. * @return 相差天数。如果失败则返回-1
  626. */
  627. public static int getIntervalDays(String date, String otherDate) {
  628. return getIntervalDays(stringToDate(date), stringToDate(otherDate));
  629. }
  630. /**
  631. * @param date 日期
  632. * @param otherDate 另一个日期
  633. * @return 相差天数。如果失败则返回-1
  634. */
  635. public static int getIntervalDays(Date date, Date otherDate) {
  636. int num = -1;
  637. Date dateTmp = DateHelper.stringToDate(DateHelper.getDate(date), DateStyle.YYYY_MM_DD);
  638. Date otherDateTmp = DateHelper.stringToDate(DateHelper.getDate(otherDate), DateStyle.YYYY_MM_DD);
  639. if (dateTmp != null && otherDateTmp != null) {
  640. long time = Math.abs(dateTmp.getTime() - otherDateTmp.getTime());
  641. num = (int) (time / (24 * 60 * 60 * 1000));
  642. }
  643. return num;
  644. }
  645. /**
  646. * 获得某天的开始时间
  647. *
  648. * @param date
  649. * @return
  650. */
  651. public static Date getDayStart(Date date) {
  652. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  653. return stringToDate(sdf.format(date));
  654. }
  655. /**
  656. * 获得某天的结束时间
  657. *
  658. * @param date
  659. * @return
  660. */
  661. public static Date getDayEnd(Date date) {
  662. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  663. return stringToDate(sdf.format(date));
  664. }
  665. /**
  666. * 或者本周开始时间
  667. *
  668. * @return
  669. */
  670. public static Date getCurrentWeekStart(Date date) {
  671. Map<String, String> map = new HashMap<>(16);
  672. Calendar cal = Calendar.getInstance();
  673. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  674. // 获取本周一的日期
  675. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  676. return stringToDate(df.format(cal.getTime()));
  677. }
  678. /**
  679. * 获取本周结束日期
  680. *
  681. * @return
  682. */
  683. public static Date getCurrentWeekEnd() {
  684. Map<String, String> map = new HashMap<>(16);
  685. Calendar cal = Calendar.getInstance();
  686. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  687. cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  688. // 增加一个星期,才是我们中国人理解的本周日的日期
  689. cal.add(Calendar.WEEK_OF_YEAR, 1);
  690. return stringToDate(df.format(cal.getTime()));
  691. }
  692. /**
  693. * 或者本月开始时间
  694. *
  695. * @return
  696. */
  697. public static Date getCurrentMonthStart() {
  698. // 获取当前日期
  699. Calendar cal = Calendar.getInstance();
  700. cal.add(Calendar.MONTH, 0);
  701. // 设置为1号,当前日期既为本月第一天
  702. cal.set(Calendar.DAY_OF_MONTH, 1);
  703. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  704. return stringToDate(df.format(cal.getTime()));
  705. }
  706. /**
  707. * 获取本月结束日期
  708. *
  709. * @return
  710. */
  711. public static Date getCurrentMonthEnd() {
  712. Calendar cal = Calendar.getInstance();
  713. // 设置为1号,当前日期既为本月第一天
  714. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  715. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  716. return stringToDate(df.format(cal.getTime()));
  717. }
  718. /**
  719. * 获得某年某月的开始时间
  720. *
  721. * @param strYear
  722. * @param strMonth
  723. * @return
  724. */
  725. public static Date getMonthStart(String strYear, String strMonth) {
  726. // 获取当前日期
  727. Calendar cal = Calendar.getInstance();
  728. cal.set(Calendar.YEAR, Integer.parseInt(strYear));
  729. cal.set(Calendar.MONTH, Integer.parseInt(strMonth));
  730. // 设置为1号,当前日期既为本月第一天
  731. cal.set(Calendar.DAY_OF_MONTH, 1);
  732. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  733. cal.set(Calendar.DAY_OF_MONTH, 1);
  734. cal.add(Calendar.DAY_OF_MONTH, -1);
  735. cal.set(Calendar.DAY_OF_MONTH, 1);
  736. Date firstDate = cal.getTime();
  737. return stringToDate(df.format(firstDate));
  738. }
  739. /**
  740. * 获得某年某月的最后的时间
  741. *
  742. * @param strYear
  743. * @param strMonth
  744. * @return
  745. */
  746. public static Date getMonthEnd(String strYear, String strMonth) {
  747. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  748. Calendar cal = Calendar.getInstance();
  749. // 不加下面2行,就是取当前时间前一个月的第一天及最后一天
  750. cal.set(Calendar.YEAR, Integer.parseInt(strYear));
  751. cal.set(Calendar.MONTH, Integer.parseInt(strMonth));
  752. cal.set(Calendar.DAY_OF_MONTH, 1);
  753. cal.add(Calendar.DAY_OF_MONTH, -1);
  754. Date lastDate = cal.getTime();
  755. return stringToDate(df.format(lastDate));
  756. }
  757. /**
  758. * 得到某天是周几
  759. *
  760. * @param “2013-06-03”格式的日期
  761. * @return 返回1是星期日、2是星期一、3是星期二、4是星期三、5是星期四、6是星期五、7是星期六
  762. */
  763. public static int getDayofweek(String date) {
  764. Calendar cal = Calendar.getInstance();
  765. if (date.equals(StrUtil.EMPTY)) {
  766. cal.setTime(new Date(System.currentTimeMillis()));
  767. } else {
  768. cal.setTime(new Date(getDateByStr2(date).getTime()));
  769. }
  770. // cal.setFirstDayOfWeek(Calendar.MONDAY);
  771. return cal.get(Calendar.DAY_OF_WEEK);
  772. }
  773. private static Date getDateByStr2(String dd) {
  774. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
  775. Date date;
  776. try {
  777. date = sd.parse(dd);
  778. } catch (ParseException e) {
  779. date = null;
  780. //log.error(e.getMessage(),e);
  781. }
  782. return date;
  783. }
  784. /**
  785. * 得到某个月的周数
  786. *
  787. * @param date
  788. * @throws Exception
  789. */
  790. public static int getMonthWeek(String date) throws Exception {
  791. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  792. Date s = sdf.parse(date);
  793. Calendar ca = Calendar.getInstance();
  794. ca.setTime(s);
  795. ca.setFirstDayOfWeek(Calendar.MONDAY);
  796. System.out.println(ca.getActualMaximum(Calendar.WEEK_OF_MONTH));
  797. return ca.getActualMaximum(Calendar.WEEK_OF_MONTH);
  798. }
  799. public static List<String> getMonthWithOffset(String month, int offset) {
  800. List<String> list = null;
  801. try {
  802. list = new ArrayList<String>();
  803. String firstDay = StrUtil.EMPTY;
  804. // 定义起始日期
  805. Date date = new SimpleDateFormat("yyyy-MM").parse(month);
  806. Date now = new Date();
  807. if (date.after(now)) {
  808. date = now;
  809. }
  810. List<String> mlist = getYearMonthBetweenDate(dateToString(date, "yyyy-MM"), dateToString(now, "yyyy-MM"));
  811. if (mlist.size() <= offset) {
  812. int preOffSet = offset * 2 + 1 - mlist.size();
  813. for (int i = preOffSet; i >= 1; i--) {
  814. Date d = addMonth(date, i * -1);
  815. list.add(dateToString(d, "yyyy-MM"));
  816. }
  817. list.addAll(mlist);
  818. return list;
  819. }
  820. // 往前数n月
  821. for (int i = offset; i >= 1; i--) {
  822. Date d = addMonth(date, i * -1);
  823. list.add(dateToString(d, "yyyy-MM"));
  824. }
  825. list.add(month);
  826. // 往后数n月
  827. for (int i = 1; i <= offset; i++) {
  828. Date d = addMonth(date, i);
  829. list.add(dateToString(d, "yyyy-MM"));
  830. }
  831. } catch (ParseException e) {
  832. return null;
  833. }
  834. return list;
  835. }
  836. /**
  837. * 获取两个日期间的年月
  838. * @param startDate
  839. * @param endDate
  840. * @return
  841. */
  842. public static List<String> getYearMonthBetweenDate(String startDate, String endDate) {
  843. List<String> list = new ArrayList<>();
  844. try {
  845. Date d1 = new SimpleDateFormat("yyyy-MM").parse(startDate);
  846. Date d2 = new SimpleDateFormat("yyyy-MM").parse(endDate);
  847. Calendar calendar1 = Calendar.getInstance();
  848. calendar1.setTime(d1);
  849. Calendar calendar2 = Calendar.getInstance();
  850. calendar2.setTime(d2);
  851. Calendar cale = Calendar.getInstance();
  852. int startDay = calendar1.get(Calendar.DAY_OF_MONTH);
  853. int endDay = calendar2.get(Calendar.DAY_OF_MONTH);
  854. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  855. // 判断是否到结束日期
  856. while (calendar1.compareTo(calendar2) <= 0) {
  857. cale.setTime(calendar1.getTime());
  858. // 如果是同一天
  859. if (calendar1.equals(calendar2)) {
  860. cale.set(Calendar.DAY_OF_MONTH, calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
  861. list.add(sdf.format(d1));
  862. } else if (calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
  863. && calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
  864. // 如果是同月 - 取第一天
  865. cale.set(Calendar.DAY_OF_MONTH, 1);
  866. list.add(sdf.format(cale.getTime()));
  867. } else {
  868. // 非同月 - 取第一天
  869. cale.set(Calendar.DAY_OF_MONTH, 1);
  870. list.add(sdf.format(cale.getTime()));
  871. cale.set(Calendar.DAY_OF_MONTH, calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
  872. }
  873. // 进行当前日期月份加1
  874. calendar1.add(Calendar.MONTH, 1);
  875. }
  876. if (endDay < startDay) {
  877. cale.setTime(d2);
  878. // 取第一天
  879. cale.set(Calendar.DAY_OF_MONTH, 1);
  880. list.add(sdf.format(cale.getTime()));
  881. }
  882. } catch (ParseException e) {
  883. return null;
  884. }
  885. return list;
  886. }
  887. public static Calendar getMonthStartTime(Integer year, Integer month){
  888. Calendar startDate= Calendar.getInstance();
  889. startDate.set(Calendar.YEAR, year);
  890. startDate.set(Calendar.MONTH, month-1);
  891. startDate.set(Calendar.DAY_OF_MONTH,1);
  892. //HOUR是12小时制
  893. startDate.set(Calendar.HOUR_OF_DAY, 0);
  894. startDate.set(Calendar.MINUTE, 0);
  895. startDate.set(Calendar.SECOND, 0);
  896. startDate.set(Calendar.MILLISECOND, 0);
  897. return startDate;
  898. }
  899. public static Calendar getMonthEndTime(Integer year, Integer month){
  900. Calendar endDate= Calendar.getInstance();
  901. endDate.set(Calendar.YEAR, year);
  902. endDate.set(Calendar.MONTH, month);
  903. //endDate.set(Calendar.DAY_OF_MONTH,endDate.getActualMaximum(Calendar.DATE));
  904. endDate.set(Calendar.DAY_OF_MONTH, 1);
  905. endDate.add(Calendar.DAY_OF_MONTH, -1);
  906. endDate.set(Calendar.HOUR_OF_DAY , 0);
  907. endDate.set(Calendar.MINUTE, 0);
  908. endDate.set(Calendar.SECOND, 0);
  909. endDate.set(Calendar.MILLISECOND, 0);
  910. return endDate;
  911. }
  912. /**
  913. * 获得两个时间的小时差
  914. * @param beginTime
  915. * @param endDate
  916. * @return
  917. */
  918. public static BigDecimal getDateTimeDifferenceHours(Date beginTime, Date endDate) {
  919. double nh = 1000.00 * 60.00 * 60.00;
  920. // long ns = 1000;
  921. // 获得两个时间的毫秒时间差异
  922. long diff = endDate.getTime() - beginTime.getTime();
  923. return new BigDecimal(diff / nh).setScale(2, BigDecimal.ROUND_HALF_UP);
  924. }
  925. /**
  926. * 清理缓存
  927. */
  928. public static void clear() {
  929. THREAD_LOCAL.remove();
  930. }
  931. public static void main(String[] args) {
  932. //getMonthWithOffset("2017-03", 3);
  933. // System.out.println(getDateTimeDifferenceHours(
  934. // DateHelper.StringToDate("2017-10-11 20:09:09", DateStyle.YYYY_MM_DD_HH_MM_SS),
  935. // DateHelper.StringToDate("2017-10-11 23:41:11", DateStyle.YYYY_MM_DD_HH_MM_SS)));
  936. }
  937. }