DateUtil.java 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. package jnpf.util;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.alibaba.fastjson.JSONArray;
  5. import jnpf.exception.DataException;
  6. import org.quartz.CronExpression;
  7. import org.quartz.TriggerUtils;
  8. import org.quartz.impl.triggers.CronTriggerImpl;
  9. import java.sql.Timestamp;
  10. import java.text.ParseException;
  11. import java.text.ParsePosition;
  12. import java.text.SimpleDateFormat;
  13. import java.time.*;
  14. import java.time.format.DateTimeFormatter;
  15. import java.util.*;
  16. /**
  17. * @author JNPF开发平台组
  18. * @version V3.1.0
  19. * @copyright 引迈信息技术有限公司
  20. * @date 2021/3/16 10:47
  21. */
  22. public class DateUtil {
  23. //============================借助Calendar类获取今天、昨天、本周、上周、本年及特定时间的开始时间和结束时间(返回类型为date类型)========================
  24. /**
  25. * 获取当天开始时间
  26. *
  27. * @return
  28. */
  29. public static Date getDayBegin() {
  30. Calendar cal = Calendar.getInstance();
  31. //0点
  32. cal.set(Calendar.HOUR_OF_DAY, 0);
  33. //0分
  34. cal.set(Calendar.MINUTE, 0);
  35. //0秒
  36. cal.set(Calendar.SECOND, 0);
  37. //0毫秒
  38. cal.set(Calendar.MILLISECOND, 0);
  39. return cal.getTime();
  40. }
  41. /**
  42. * 获取当前时间
  43. */
  44. public static String getNow() {
  45. LocalDateTime ldt1 = LocalDateTime.now();
  46. DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss" );
  47. String temp = dtf1.format(ldt1);
  48. return temp;
  49. }
  50. /**
  51. * 获取当前时间
  52. */
  53. public static String getNow(String zone) {
  54. LocalDateTime ldt1 = LocalDateTime.now(ZoneId.of(zone));
  55. DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss" );
  56. String temp = dtf1.format(ldt1);
  57. return temp;
  58. }
  59. /**
  60. * 获取当前时间
  61. */
  62. public static String getmmNow() {
  63. LocalDateTime ldt1 = LocalDateTime.now();
  64. DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm" );
  65. String temp = dtf1.format(ldt1);
  66. return temp;
  67. }
  68. /**
  69. * 获取当前时间字符串
  70. * yyyyMMddHHmmss
  71. */
  72. public static String nowDateTime(){
  73. LocalDateTime ldt1 = LocalDateTime.now();
  74. DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss" );
  75. String temp = dtf1.format(ldt1);
  76. return temp;
  77. }
  78. /**
  79. * 获取当前时间
  80. */
  81. public static Date getNowDate() {
  82. Date date = new Date();
  83. return date;
  84. }
  85. /**
  86. * 获取当天结束时间
  87. *
  88. * @return
  89. */
  90. public static Date getDayEnd() {
  91. Calendar cal = Calendar.getInstance();
  92. //23点
  93. cal.set(Calendar.HOUR_OF_DAY, 23);
  94. //59分
  95. cal.set(Calendar.MINUTE, 59);
  96. //59秒
  97. cal.set(Calendar.SECOND, 59);
  98. return cal.getTime();
  99. }
  100. /**
  101. * 获取昨天开始时间
  102. *
  103. * @return
  104. */
  105. public static Date getBeginDayOfYesterday() {
  106. Calendar cal = Calendar.getInstance();
  107. //当天开始时间
  108. cal.setTime(getDayBegin());
  109. //当天月份天数减1
  110. cal.add(Calendar.DAY_OF_MONTH, -1);
  111. return cal.getTime();
  112. }
  113. /**
  114. * 获取昨天结束时间
  115. *
  116. * @return
  117. */
  118. public static Date getEndDayOfYesterday() {
  119. Calendar cal = Calendar.getInstance();
  120. //当天结束时间
  121. cal.setTime(getDayEnd());
  122. //当天月份天数减1
  123. cal.add(Calendar.DAY_OF_MONTH, -1);
  124. return cal.getTime();
  125. }
  126. /**
  127. * 获取明天开始时间
  128. *
  129. * @return
  130. */
  131. public static Date getBeginDayOfTomorrow() {
  132. Calendar cal = Calendar.getInstance();
  133. //当天开始时间
  134. cal.setTime(getDayBegin());
  135. //当天月份天数加1
  136. cal.add(Calendar.DAY_OF_MONTH, 1);
  137. return cal.getTime();
  138. }
  139. /**
  140. * 获取明天结束时间
  141. *
  142. * @return
  143. */
  144. public static Date getEndDayOfTomorrow() {
  145. Calendar cal = Calendar.getInstance();
  146. //当天结束时间
  147. cal.setTime(getDayEnd());
  148. //当天月份天数加1
  149. cal.add(Calendar.DAY_OF_MONTH, 1);
  150. return cal.getTime();
  151. }
  152. /**
  153. * 获取某个日期的开始时间
  154. *
  155. * @param d
  156. * @return
  157. */
  158. public static Timestamp getDayStartTime(Date d) {
  159. Calendar calendar = Calendar.getInstance();
  160. if (null != d) {
  161. calendar.setTime(d);
  162. }
  163. calendar.set(calendar.get(Calendar.YEAR),
  164. calendar.get(Calendar.MONTH),
  165. calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  166. calendar.set(Calendar.MILLISECOND, 0);
  167. return new Timestamp(calendar.getTimeInMillis());
  168. }
  169. /**
  170. * 获取某个日期的结束时间
  171. *
  172. * @param d
  173. * @return
  174. */
  175. public static Timestamp getDayEndTime(Date d) {
  176. Calendar calendar = Calendar.getInstance();
  177. if (null != d) {
  178. calendar.setTime(d);
  179. }
  180. calendar.set(calendar.get(Calendar.YEAR),
  181. calendar.get(Calendar.MONTH),
  182. calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
  183. calendar.set(Calendar.MILLISECOND, 999);
  184. return new Timestamp(calendar.getTimeInMillis());
  185. }
  186. /**
  187. * 获取本周的开始时间
  188. *
  189. * @return
  190. */
  191. public static Date getBeginDayOfWeek() {
  192. Date date = new Date();
  193. Calendar cal = Calendar.getInstance();
  194. cal.setTime(date);
  195. int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
  196. if (dayOfWeek == 1) {
  197. dayOfWeek += 7;
  198. }
  199. cal.add(Calendar.DATE, 2 - dayOfWeek);
  200. return getDayStartTime(cal.getTime());
  201. }
  202. /**
  203. * 获取本周的结束时间
  204. *
  205. * @return
  206. */
  207. public static Date getEndDayOfWeek() {
  208. Calendar cal = Calendar.getInstance();
  209. cal.setTime(getBeginDayOfWeek());
  210. cal.add(Calendar.DAY_OF_WEEK, 6);
  211. Date weekEndSta = cal.getTime();
  212. return getDayEndTime(weekEndSta);
  213. }
  214. /**
  215. * 获取上周开始时间
  216. */
  217. @SuppressWarnings("unused" )
  218. public static Date getBeginDayOfLastWeek() {
  219. Date date = new Date();
  220. if (date == null) {
  221. return null;
  222. }
  223. Calendar cal = Calendar.getInstance();
  224. cal.setTime(date);
  225. int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
  226. if (dayofweek == 1) {
  227. dayofweek += 7;
  228. }
  229. cal.add(Calendar.DATE, 2 - dayofweek - 7);
  230. return getDayStartTime(cal.getTime());
  231. }
  232. /**
  233. * 获取上周的结束时间
  234. *
  235. * @return
  236. */
  237. public static Date getEndDayOfLastWeek() {
  238. Calendar cal = Calendar.getInstance();
  239. cal.setTime(getBeginDayOfLastWeek());
  240. cal.add(Calendar.DAY_OF_WEEK, 6);
  241. Date weekEndSta = cal.getTime();
  242. return getDayEndTime(weekEndSta);
  243. }
  244. /**
  245. * 获取今年是哪一年
  246. *
  247. * @return
  248. */
  249. public static Integer getNowYear() {
  250. Date date = new Date();
  251. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  252. gc.setTime(date);
  253. return Integer.valueOf(gc.get(1));
  254. }
  255. /**
  256. * 获取本月是哪一月
  257. *
  258. * @return
  259. */
  260. public static int getNowMonth() {
  261. Date date = new Date();
  262. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  263. gc.setTime(date);
  264. return gc.get(2) + 1;
  265. }
  266. /**
  267. * 获取本月的开始时间
  268. *
  269. * @return
  270. */
  271. public static Date getBeginDayOfMonth() {
  272. Calendar calendar = Calendar.getInstance();
  273. calendar.set(getNowYear(), getNowMonth() - 1, 1);
  274. return getDayStartTime(calendar.getTime());
  275. }
  276. /**
  277. * 获取本月的结束时间
  278. *
  279. * @return
  280. */
  281. public static Date getEndDayOfMonth() {
  282. Calendar calendar = Calendar.getInstance();
  283. calendar.set(getNowYear(), getNowMonth() - 1, 1);
  284. int day = calendar.getActualMaximum(5);
  285. calendar.set(getNowYear(), getNowMonth() - 1, day);
  286. return getDayEndTime(calendar.getTime());
  287. }
  288. /**
  289. * 获取上月的开始时间
  290. *
  291. * @return
  292. */
  293. public static Date getBeginDayOfLastMonth() {
  294. Calendar calendar = Calendar.getInstance();
  295. calendar.set(getNowYear(), getNowMonth() - 2, 1);
  296. return getDayStartTime(calendar.getTime());
  297. }
  298. /**
  299. * 获取上月的结束时间
  300. *
  301. * @return
  302. */
  303. public static Date getEndDayOfLastMonth() {
  304. Calendar calendar = Calendar.getInstance();
  305. calendar.set(getNowYear(), getNowMonth() - 2, 1);
  306. int day = calendar.getActualMaximum(5);
  307. calendar.set(getNowYear(), getNowMonth() - 2, day);
  308. return getDayEndTime(calendar.getTime());
  309. }
  310. /**
  311. * 获取本年的开始时间
  312. *
  313. * @return
  314. */
  315. public static Date getBeginDayOfYear() {
  316. Calendar cal = Calendar.getInstance();
  317. cal.set(Calendar.YEAR, getNowYear());
  318. cal.set(Calendar.MONTH, Calendar.JANUARY);
  319. cal.set(Calendar.DATE, 1);
  320. return getDayStartTime(cal.getTime());
  321. }
  322. /**
  323. * 获取本年的结束时间
  324. *
  325. * @return
  326. */
  327. public static Date getEndDayOfYear() {
  328. Calendar cal = Calendar.getInstance();
  329. cal.set(Calendar.YEAR, getNowYear());
  330. cal.set(Calendar.MONTH, Calendar.DECEMBER);
  331. cal.set(Calendar.DATE, 31);
  332. return getDayEndTime(cal.getTime());
  333. }
  334. /**
  335. * 两个日期相减得到的天数
  336. *
  337. * @param beginDate
  338. * @param endDate
  339. * @return
  340. */
  341. public static int getDiffDays(Date beginDate, Date endDate) {
  342. if (beginDate == null || endDate == null) {
  343. throw new IllegalArgumentException("getDiffDays param is null!" );
  344. }
  345. long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24);
  346. int days = (int) diff;
  347. return days;
  348. }
  349. /**
  350. * 两个日期相减得到的毫秒数
  351. *
  352. * @param beginDate
  353. * @param endDate
  354. * @return
  355. */
  356. public static long dateDiff(Date beginDate, Date endDate) {
  357. long date1ms = beginDate.getTime();
  358. long date2ms = endDate.getTime();
  359. return date2ms - date1ms;
  360. }
  361. /**
  362. * 获取两个日期中的最大日起
  363. *
  364. * @param beginDate
  365. * @param endDate
  366. * @return
  367. */
  368. public static Date max(Date beginDate, Date endDate) {
  369. if (beginDate == null) {
  370. return endDate;
  371. }
  372. if (endDate == null) {
  373. return beginDate;
  374. }
  375. //beginDate日期大于endDate
  376. if (beginDate.after(endDate)) {
  377. return beginDate;
  378. }
  379. return endDate;
  380. }
  381. /**
  382. * 获取两个日期中的最小日期
  383. *
  384. * @param beginDate
  385. * @param endDate
  386. * @return
  387. */
  388. public static Date min(Date beginDate, Date endDate) {
  389. if (beginDate == null) {
  390. return endDate;
  391. }
  392. if (endDate == null) {
  393. return beginDate;
  394. }
  395. if (beginDate.after(endDate)) {
  396. return endDate;
  397. }
  398. return beginDate;
  399. }
  400. /**
  401. * 获取某月该季度的第一个月
  402. *
  403. * @param date
  404. * @return
  405. */
  406. public static Date getFirstSeasonDate(Date date) {
  407. final int[] season = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
  408. Calendar cal = Calendar.getInstance();
  409. cal.setTime(date);
  410. int sean = season[cal.get(Calendar.MONTH)];
  411. cal.set(Calendar.MONTH, sean * 3 - 3);
  412. return cal.getTime();
  413. }
  414. /**
  415. * 返回某个日期下几天的日期
  416. *
  417. * @param date
  418. * @param i
  419. * @return
  420. */
  421. public static Date getNextDay(Date date, int i) {
  422. Calendar cal = new GregorianCalendar();
  423. cal.setTime(date);
  424. cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
  425. return cal.getTime();
  426. }
  427. /**
  428. * 返回某个日期前几天的日期
  429. *
  430. * @param date
  431. * @param i
  432. * @return
  433. */
  434. public static Date getFrontDay(Date date, int i) {
  435. Calendar cal = new GregorianCalendar();
  436. cal.setTime(date);
  437. cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
  438. return cal.getTime();
  439. }
  440. /**
  441. * 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
  442. *
  443. * @param beginYear
  444. * @param beginMonth
  445. * @param k
  446. * @return
  447. */
  448. public static List getTimeList(int beginYear, int beginMonth, int k) {
  449. List list = new ArrayList();
  450. Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
  451. int max = begincal.getActualMaximum(Calendar.DATE);
  452. for (int i = 1; i < max; i = i + k) {
  453. list.add(begincal.getTime());
  454. begincal.add(Calendar.DATE, k);
  455. }
  456. begincal = new GregorianCalendar(beginYear, beginMonth, max);
  457. list.add(begincal.getTime());
  458. return list;
  459. }
  460. /**
  461. * 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
  462. *
  463. * @param beginYear
  464. * @param beginMonth
  465. * @param endYear
  466. * @param endMonth
  467. * @param k
  468. * @return
  469. */
  470. public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) {
  471. List list = new ArrayList();
  472. if (beginYear == endYear) {
  473. for (int j = beginMonth; j <= endMonth; j++) {
  474. list.add(getTimeList(beginYear, j, k));
  475. }
  476. } else {
  477. {
  478. for (int j = beginMonth; j < 12; j++) {
  479. list.add(getTimeList(beginYear, j, k));
  480. }
  481. for (int i = beginYear + 1; i < endYear; i++) {
  482. for (int j = 0; j < 12; j++) {
  483. list.add(getTimeList(i, j, k));
  484. }
  485. }
  486. for (int j = 0; j <= endMonth; j++) {
  487. list.add(getTimeList(endYear, j, k));
  488. }
  489. }
  490. }
  491. return list;
  492. }
  493. /**
  494. * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
  495. *
  496. * @param nowTime 当前时间
  497. * @param startTime 开始时间
  498. * @param endTime 结束时间
  499. * @return
  500. */
  501. public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
  502. if (nowTime.getTime() == startTime.getTime()
  503. || nowTime.getTime() == endTime.getTime()) {
  504. return true;
  505. }
  506. Calendar date = Calendar.getInstance();
  507. date.setTime(nowTime);
  508. Calendar begin = Calendar.getInstance();
  509. begin.setTime(startTime);
  510. Calendar end = Calendar.getInstance();
  511. end.setTime(endTime);
  512. if (date.after(begin) && date.before(end)) {
  513. return true;
  514. } else {
  515. return false;
  516. }
  517. }
  518. //=================================时间格式转换==========================
  519. /**
  520. * date类型进行格式化输出
  521. *
  522. * @param pattern
  523. * @return
  524. */
  525. public static String dateNow(String pattern) {
  526. Date date = new Date();
  527. if (pattern == null && "".equals(pattern)) {
  528. return null;
  529. }
  530. SimpleDateFormat formatter = new SimpleDateFormat(pattern);
  531. String dateString = formatter.format(date);
  532. return dateString;
  533. }
  534. /**
  535. * date类型进行格式化输出
  536. *
  537. * @param date
  538. * @param pattern
  539. * @return
  540. */
  541. public static String dateToString(Date date, String pattern) {
  542. if (StringUtil.isEmpty(pattern) && date == null) {
  543. return null;
  544. }
  545. SimpleDateFormat formatter = new SimpleDateFormat(pattern);
  546. String dateString = formatter.format(date);
  547. return dateString;
  548. }
  549. /**
  550. * date类型进行格式化输出(返回类型:String)
  551. *
  552. * @param date
  553. * @return
  554. */
  555. public static String dateFormat(Date date) {
  556. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
  557. String dateString = formatter.format(date);
  558. return dateString;
  559. }
  560. /**
  561. * date类型进行格式化输出(返回类型:String)
  562. *
  563. * @param date
  564. * @return
  565. */
  566. public static String daFormat(Date date) {
  567. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd" );
  568. String dateString = formatter.format(date);
  569. return dateString;
  570. }
  571. /**
  572. * date类型进行格式化输出(返回类型:String)
  573. *
  574. * @param date
  575. * @return
  576. */
  577. public static String dateFormatByPattern(Date date, String pattern) {
  578. SimpleDateFormat formatter = new SimpleDateFormat(pattern);
  579. String dateString = formatter.format(date);
  580. return dateString;
  581. }
  582. /**
  583. * 时间戳类型进行格式化输出(返回类型:String)
  584. *
  585. * @param date
  586. * @return
  587. */
  588. public static String daFormat(Long date) {
  589. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd" );
  590. Date time = new Date(date);
  591. String dateString = formatter.format(time);
  592. return dateString;
  593. }
  594. /**
  595. * 时间戳类型进行格式化输出yyyy-MM-dd(返回类型:String)
  596. *
  597. * @param date
  598. * @return
  599. */
  600. public static String daFormatYmd(Long date) {
  601. DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd" );
  602. String dateString = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.of("+8" )));
  603. return dateString;
  604. }
  605. /**
  606. * 时间戳类型进行格式化输出(返回类型:String)
  607. *
  608. * @param date
  609. * @return
  610. */
  611. public static String daFormatHHMMSSAddEight(Long date) {
  612. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
  613. Date time = dateAddHours(new Date(date), 8);
  614. String dateString = formatter.format(time);
  615. return dateString;
  616. }
  617. /**
  618. * 时间戳类型进行格式化输出(返回类型:String)
  619. *
  620. * @param date
  621. * @return
  622. */
  623. public static String daFormatHHMMSS(Long date) {
  624. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
  625. Date time = new Date(date);
  626. String dateString = formatter.format(time);
  627. return dateString;
  628. }
  629. /**
  630. * 将"2015-08-31 21:08:06"型字符串转化为Date
  631. *
  632. * @param str
  633. * @return
  634. * @throws ParseException
  635. */
  636. public static Date stringToDate(String str) {
  637. Date date = null;
  638. if (StringUtil.isEmpty(str)) {
  639. return date;
  640. }
  641. try {
  642. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
  643. date = formatter.parse(str);
  644. } catch (ParseException e) {
  645. System.out.println(e.getMessage());
  646. return date;
  647. }
  648. return date;
  649. }
  650. /**
  651. * 将"2015-08-31"型字符串转化为Date
  652. *
  653. * @param str
  654. * @return
  655. * @throws ParseException
  656. */
  657. public static Date stringToDates(String str) {
  658. Date date = null;
  659. try {
  660. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd" );
  661. date = formatter.parse(str);
  662. } catch (ParseException e) {
  663. return date;
  664. }
  665. return date;
  666. }
  667. /**
  668. * 将CST时间类型字符串进行格式化输出
  669. *
  670. * @param str
  671. * @return
  672. * @throws ParseException
  673. */
  674. public static String cstFormat(String str) {
  675. try {
  676. SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy" , Locale.US);
  677. Date date = formatter.parse(str);
  678. return dateFormat(date);
  679. } catch (Exception e) {
  680. return "";
  681. }
  682. }
  683. /**
  684. * 将long类型转化为Date
  685. *
  686. * @param str
  687. * @return
  688. * @throws ParseException
  689. */
  690. public static Date longToDate(long str) throws ParseException {
  691. return new Date(str * 1000);
  692. }
  693. //====================================其他常见日期操作方法======================
  694. /**
  695. * 判断当前日期是否在[startDate, endDate]区间
  696. *
  697. * @param startDate 开始日期
  698. * @param endDate 结束日期
  699. * @return
  700. * @author jqlin
  701. */
  702. public static boolean isEffectiveDate(Date startDate, Date endDate) {
  703. if (startDate == null || endDate == null) {
  704. return false;
  705. }
  706. long currentTime = System.currentTimeMillis();
  707. if (currentTime >= startDate.getTime()
  708. && currentTime <= endDate.getTime()) {
  709. return true;
  710. }
  711. return false;
  712. }
  713. /**
  714. * 得到二个日期间的间隔天数
  715. *
  716. * @param secondString:后一个日期
  717. * @param firstString:前一个日期
  718. * @return
  719. */
  720. public static String getTwoDay(String secondString, String firstString) {
  721. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd" );
  722. long day = 0;
  723. try {
  724. Date secondTime = myFormatter.parse(secondString);
  725. Date firstTime = myFormatter.parse(firstString);
  726. day = (secondTime.getTime() - firstTime.getTime()) / (24 * 60 * 60 * 1000);
  727. } catch (Exception e) {
  728. return "";
  729. }
  730. return day + "";
  731. }
  732. /**
  733. * 时间前推或后推分钟,其中JJ表示分钟.
  734. *
  735. * @param stringTime:时间
  736. * @param minute:分钟(有正负之分)
  737. * @return
  738. */
  739. public static String getPreTime(String stringTime, String minute) {
  740. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
  741. String mydate1 = "";
  742. try {
  743. Date date1 = format.parse(stringTime);
  744. long time = (date1.getTime() / 1000) + Integer.parseInt(minute) * 60;
  745. date1.setTime(time * 1000);
  746. mydate1 = format.format(date1);
  747. } catch (Exception e) {
  748. return "";
  749. }
  750. return mydate1;
  751. }
  752. /**
  753. * 时间转成成秒
  754. */
  755. public static long getTime(Date data) {
  756. if (data != null) {
  757. return (data.getTime() / 1000);
  758. }
  759. return 0;
  760. }
  761. /**
  762. * 将短时间格式字符串转换为时间 yyyy-MM-dd
  763. *
  764. * @param strDate
  765. * @return
  766. */
  767. public static Date strToDate(String strDate) {
  768. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd" );
  769. ParsePosition pos = new ParsePosition(0);
  770. Date strtodate = formatter.parse(strDate, pos);
  771. return strtodate;
  772. }
  773. /**
  774. * 得到一个时间延后或前移几天的时间
  775. *
  776. * @param nowdate:时间
  777. * @param delay:前移或后延的天数
  778. * @return
  779. */
  780. public static String getNextDay(String nowdate, String delay) {
  781. try {
  782. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd" );
  783. String mdate = "";
  784. Date d = strToDate(nowdate);
  785. long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
  786. d.setTime(myTime * 1000);
  787. mdate = format.format(d);
  788. return mdate;
  789. } catch (Exception e) {
  790. return "";
  791. }
  792. }
  793. /**
  794. * 判断是否闰年
  795. *
  796. * @param ddate
  797. * @return
  798. */
  799. public static boolean isLeapYear(String ddate) {
  800. /**
  801. * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  802. * 3.能被4整除同时能被100整除则不是闰年
  803. */
  804. Date d = strToDate(ddate);
  805. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  806. gc.setTime(d);
  807. int year = gc.get(Calendar.YEAR);
  808. if ((year % 400) == 0) {
  809. return true;
  810. } else if ((year % 4) == 0) {
  811. if ((year % 100) == 0) {
  812. return false;
  813. } else {
  814. return true;
  815. }
  816. } else {
  817. return false;
  818. }
  819. }
  820. /**
  821. * 返回美国时间格式
  822. *
  823. * @param str
  824. * @return
  825. */
  826. public static String geteDate(String str) {
  827. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd" );
  828. ParsePosition pos = new ParsePosition(0);
  829. Date strtodate = formatter.parse(str, pos);
  830. String j = strtodate.toString();
  831. String[] k = j.split(" " );
  832. return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
  833. }
  834. /**
  835. * 判断二个时间是否在同一个周
  836. *
  837. * @param date1
  838. * @param date2
  839. * @return
  840. */
  841. public static boolean isSameWeekDates(Date date1, Date date2) {
  842. Calendar cal1 = Calendar.getInstance();
  843. Calendar cal2 = Calendar.getInstance();
  844. cal1.setTime(date1);
  845. cal2.setTime(date2);
  846. int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  847. if (0 == subYear) {
  848. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
  849. return true;
  850. }
  851. } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
  852. // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  853. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
  854. return true;
  855. }
  856. } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
  857. if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
  858. return true;
  859. }
  860. }
  861. return false;
  862. }
  863. /**
  864. * 产生周序列,即得到当前时间所在的年度是第几周
  865. *
  866. * @return
  867. */
  868. public static String getSeqWeek() {
  869. Calendar c = Calendar.getInstance(Locale.CHINA);
  870. String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  871. if (week.length() == 1) {
  872. week = "0" + week;
  873. }
  874. String year = Integer.toString(c.get(Calendar.YEAR));
  875. return year + "年第" + week + "周";
  876. }
  877. /**
  878. * 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
  879. *
  880. * @param sdate:日期
  881. * @param num:星期几(星期天是一周的第一天)
  882. * @return
  883. */
  884. public static String getWeek(String sdate, String num) {
  885. // 再转换为时间
  886. Date dd = strToDate(sdate);
  887. Calendar c = Calendar.getInstance();
  888. c.setTime(dd);
  889. if ("1".equals(num)) {
  890. // 返回星期一所在的日期
  891. c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  892. } else if ("2".equals(num)) {
  893. // 返回星期二所在的日期
  894. c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
  895. } else if ("3".equals(num)) {
  896. // 返回星期三所在的日期
  897. c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  898. } else if ("4".equals(num)) {
  899. // 返回星期四所在的日期
  900. c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  901. } else if ("5".equals(num)) {
  902. // 返回星期五所在的日期
  903. c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  904. } else if ("6".equals(num)) {
  905. // 返回星期六所在的日期
  906. c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  907. } else if ("0".equals(num)) {
  908. // 返回星期日所在的日期
  909. c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  910. }
  911. return new SimpleDateFormat("yyyy-MM-dd" ).format(c.getTime());
  912. }
  913. /**
  914. * 根据一个日期,返回是星期几的字符串
  915. *
  916. * @param sdate
  917. * @return
  918. */
  919. public static String getWeek(String sdate) {
  920. // 再转换为时间
  921. Date date = strToDate(sdate);
  922. Calendar c = Calendar.getInstance();
  923. c.setTime(date);
  924. return new SimpleDateFormat("EEEE" ).format(c.getTime());
  925. }
  926. /**
  927. * 根据一个日期,返回是星期几的字符串
  928. *
  929. * @param sdate
  930. * @return
  931. */
  932. public static String getWeekStr(String sdate) {
  933. String str = "";
  934. str = getWeek(sdate);
  935. if ("1".equals(str)) {
  936. str = "星期日";
  937. } else if ("2".equals(str)) {
  938. str = "星期一";
  939. } else if ("3".equals(str)) {
  940. str = "星期二";
  941. } else if ("4".equals(str)) {
  942. str = "星期三";
  943. } else if ("5".equals(str)) {
  944. str = "星期四";
  945. } else if ("6".equals(str)) {
  946. str = "星期五";
  947. } else if ("7".equals(str)) {
  948. str = "星期六";
  949. }
  950. return str;
  951. }
  952. /**
  953. * 根据一个日期,返回是星期几的字符串
  954. *
  955. * @param date
  956. * @return
  957. */
  958. public static String getWeekStr(Date date) {
  959. Calendar c = Calendar.getInstance();
  960. c.setTime(date);
  961. String str = c.get(Calendar.DAY_OF_WEEK) + "";
  962. if ("1".equals(str)) {
  963. str = "SUN";
  964. } else if ("2".equals(str)) {
  965. str = "MON";
  966. } else if ("3".equals(str)) {
  967. str = "TUE";
  968. } else if ("4".equals(str)) {
  969. str = "WED";
  970. } else if ("5".equals(str)) {
  971. str = "THU";
  972. } else if ("6".equals(str)) {
  973. str = "FRI";
  974. } else if ("7".equals(str)) {
  975. str = "SAT";
  976. }
  977. return str;
  978. }
  979. /**
  980. * 两个时间之间的天数
  981. *
  982. * @param date1
  983. * @param date2
  984. * @return
  985. */
  986. public static long getDays(String date1, String date2) {
  987. if (date1 == null || "".equals(date1)) {
  988. return 0;
  989. }
  990. if (date2 == null || "".equals(date2)) {
  991. return 0;
  992. }
  993. // 转换为标准时间
  994. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd" );
  995. Date date = null;
  996. Date mydate = null;
  997. try {
  998. date = myFormatter.parse(date1);
  999. mydate = myFormatter.parse(date2);
  1000. } catch (Exception e) {
  1001. throw new RuntimeException(e);
  1002. }
  1003. long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
  1004. return day;
  1005. }
  1006. /**
  1007. * 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
  1008. * 此函数返回该日历第一行星期日所在的日期
  1009. *
  1010. * @param sdate
  1011. * @return
  1012. */
  1013. public static String getNowMonth(String sdate) {
  1014. // 取该时间所在月的一号
  1015. sdate = sdate.substring(0, 8) + "01";
  1016. // 得到这个月的1号是星期几
  1017. Date date = strToDate(sdate);
  1018. Calendar c = Calendar.getInstance();
  1019. c.setTime(date);
  1020. int u = c.get(Calendar.DAY_OF_WEEK);
  1021. String newday = getNextDay(sdate, (1 - u) + "" );
  1022. return newday;
  1023. }
  1024. /**
  1025. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写
  1026. *
  1027. * @param sformat
  1028. * @return
  1029. */
  1030. public static String getUserDate(String sformat) {
  1031. Date currentTime = new Date();
  1032. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  1033. String dateString = formatter.format(currentTime);
  1034. return dateString;
  1035. }
  1036. /**
  1037. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写
  1038. *
  1039. * @param sformat
  1040. * @return
  1041. */
  1042. public static String getDateString(Date date, String sformat) {
  1043. Date currentTime = null;
  1044. if (date == null) {
  1045. currentTime = new Date();
  1046. } else {
  1047. currentTime = date;
  1048. }
  1049. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  1050. String dateString = formatter.format(currentTime);
  1051. return dateString;
  1052. }
  1053. /**
  1054. * 返回一个i位数的随机数
  1055. *
  1056. * @param i
  1057. * @return
  1058. */
  1059. public static String getRandom(int i) {
  1060. Random jjj = new Random();
  1061. if (i == 0) {
  1062. return "";
  1063. }
  1064. String jj = "";
  1065. for (int k = 0; k < i; k++) {
  1066. jj = jj + jjj.nextInt(9);
  1067. }
  1068. return jj;
  1069. }
  1070. //====================================日期格式转换======================
  1071. /**
  1072. * Date转换为LocalDateTime
  1073. *
  1074. * @param date
  1075. */
  1076. public static LocalDateTime dateToLocalDateTime(Date date) {
  1077. if (date != null) {
  1078. //An instantaneous point on the time-line.(时间线上的一个瞬时点。)
  1079. Instant instant = date.toInstant();
  1080. //A time-zone ID, such ZxingCodeUtil {@code Europe/Paris}.(时区)
  1081. ZoneId zoneId = ZoneId.systemDefault();
  1082. LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
  1083. return localDateTime;
  1084. }
  1085. return null;
  1086. }
  1087. /**
  1088. * LocalDateTime转换为Date
  1089. *
  1090. * @param localDateTime
  1091. */
  1092. public static Date localDateTimeToDate(LocalDateTime localDateTime) {
  1093. if (localDateTime != null) {
  1094. ZoneId zoneId = ZoneId.systemDefault();
  1095. //Combines this date-time with a time-zone to create a ZonedDateTime.
  1096. ZonedDateTime zdt = localDateTime.atZone(zoneId);
  1097. Date date = Date.from(zdt.toInstant());
  1098. return date;
  1099. }
  1100. return null;
  1101. }
  1102. //====================================当前时间加减======================
  1103. /**
  1104. * 时间加减小时
  1105. *
  1106. * @param startDate 要处理的时间,Null则为当前时间
  1107. * @param hours 加减的小时
  1108. * @return Date
  1109. */
  1110. public static Date dateAddHours(Date startDate, int hours) {
  1111. if (startDate == null) {
  1112. startDate = new Date();
  1113. }
  1114. Calendar c = Calendar.getInstance();
  1115. c.setTime(startDate);
  1116. c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
  1117. return c.getTime();
  1118. }
  1119. /**
  1120. * 时间加减分钟
  1121. *
  1122. * @param startDate 要处理的时间,Null则为当前时间
  1123. * @param minutes 加减的分钟
  1124. * @return Date
  1125. */
  1126. public static Date dateAddMinutes(Date startDate, int minutes) {
  1127. if (startDate == null) {
  1128. startDate = new Date();
  1129. }
  1130. Calendar c = Calendar.getInstance();
  1131. c.setTime(startDate);
  1132. c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
  1133. return c.getTime();
  1134. }
  1135. /**
  1136. * 时间加减秒数
  1137. *
  1138. * @param startDate 要处理的时间,Null则为当前时间
  1139. * @param seconds 加减的秒数
  1140. * @return Date
  1141. */
  1142. public static Date dateAddSeconds(Date startDate, int seconds) {
  1143. if (startDate == null) {
  1144. startDate = new Date();
  1145. }
  1146. Calendar c = Calendar.getInstance();
  1147. c.setTime(startDate);
  1148. c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
  1149. return c.getTime();
  1150. }
  1151. /**
  1152. * 时间加减年数
  1153. *
  1154. * @param startDate 要处理的时间,Null则为当前时间
  1155. * @param years 加减的年数
  1156. * @return Date
  1157. */
  1158. public static Date dateAddYears(Date startDate, int years) {
  1159. if (startDate == null) {
  1160. startDate = new Date();
  1161. }
  1162. Calendar c = Calendar.getInstance();
  1163. c.setTime(startDate);
  1164. c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years);
  1165. return c.getTime();
  1166. }
  1167. /**
  1168. * 时间加减月数
  1169. *
  1170. * @param startDate 要处理的时间,Null则为当前时间
  1171. * @param months 加减的月数
  1172. * @return Date
  1173. */
  1174. public static Date dateAddMonths(Date startDate, int months) {
  1175. if (startDate == null) {
  1176. startDate = new Date();
  1177. }
  1178. Calendar c = Calendar.getInstance();
  1179. c.setTime(startDate);
  1180. c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months);
  1181. return c.getTime();
  1182. }
  1183. /**
  1184. * 时间加减天数
  1185. *
  1186. * @param startDate 要处理的时间,Null则为当前时间
  1187. * @param days 加减的天数
  1188. * @return Date
  1189. */
  1190. public static Date dateAddDays(Date startDate, int days) {
  1191. if (startDate == null) {
  1192. startDate = new Date();
  1193. }
  1194. Calendar c = Calendar.getInstance();
  1195. c.setTime(startDate);
  1196. c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
  1197. return c.getTime();
  1198. }
  1199. /**
  1200. * 时间加减小时
  1201. *
  1202. * @param localDateTime 要处理的时间,Null则为当前时间
  1203. * @param hours 加减的小时
  1204. * @return Date
  1205. */
  1206. public static LocalDateTime localDateAddHours(LocalDateTime localDateTime, int hours) {
  1207. if (localDateTime == null) {
  1208. localDateTime = LocalDateTime.now();
  1209. }
  1210. return localDateTime.plusHours(hours);
  1211. }
  1212. /**
  1213. * 时间加减分钟
  1214. *
  1215. * @param localDateTime 要处理的时间,Null则为当前时间
  1216. * @param minutes 加减的分钟
  1217. * @return Date
  1218. */
  1219. public static LocalDateTime localDateAddMinutes(LocalDateTime localDateTime, int minutes) {
  1220. if (localDateTime == null) {
  1221. localDateTime = LocalDateTime.now();
  1222. }
  1223. return localDateTime.plusMinutes(minutes);
  1224. }
  1225. /**
  1226. * 时间加减秒数
  1227. *
  1228. * @param localDateTime 要处理的时间,Null则为当前时间
  1229. * @param seconds 加减的秒数
  1230. * @return Date
  1231. */
  1232. public static LocalDateTime localDateAddSeconds(LocalDateTime localDateTime, int seconds) {
  1233. if (localDateTime == null) {
  1234. localDateTime = LocalDateTime.now();
  1235. }
  1236. return localDateTime.plusSeconds(seconds);
  1237. }
  1238. /**
  1239. * 时间加减年数
  1240. *
  1241. * @param localDateTime 要处理的时间,Null则为当前时间
  1242. * @param years 加减的年数
  1243. * @return Date
  1244. */
  1245. public static LocalDateTime localDateAddYears(LocalDateTime localDateTime, int years) {
  1246. if (localDateTime == null) {
  1247. localDateTime = LocalDateTime.now();
  1248. }
  1249. return localDateTime.plusYears(years);
  1250. }
  1251. /**
  1252. * 时间加减月数
  1253. *
  1254. * @param localDateTime 要处理的时间,Null则为当前时间
  1255. * @param months 加减的月数
  1256. * @return Date
  1257. */
  1258. public static LocalDateTime localDateMonths(LocalDateTime localDateTime, int months) {
  1259. if (localDateTime == null) {
  1260. localDateTime = LocalDateTime.now();
  1261. }
  1262. return localDateTime.plusMonths(months);
  1263. }
  1264. /**
  1265. * 时间加减天数
  1266. *
  1267. * @param localDateTime 要处理的时间,Null则为当前时间
  1268. * @param days 加减的天数
  1269. * @return Date
  1270. */
  1271. public static LocalDateTime localDateAddDays(LocalDateTime localDateTime, int days) {
  1272. if (localDateTime == null) {
  1273. localDateTime = LocalDateTime.now();
  1274. }
  1275. return localDateTime.plusDays(days);
  1276. }
  1277. //====================================时间比较======================
  1278. /**
  1279. * 时间比较(如果myDate>compareDate返回1,<返回-1,相等返回0)
  1280. *
  1281. * @param myDate 时间
  1282. * @param compareDate 要比较的时间
  1283. * @return int
  1284. */
  1285. public static int dateCompare(Date myDate, Date compareDate) {
  1286. Calendar myCal = Calendar.getInstance();
  1287. Calendar compareCal = Calendar.getInstance();
  1288. myCal.setTime(myDate);
  1289. compareCal.setTime(compareDate);
  1290. return myCal.compareTo(compareCal);
  1291. }
  1292. /**
  1293. * 获取两个时间中最小的一个时间
  1294. *
  1295. * @param date
  1296. * @param compareDate
  1297. * @return Date
  1298. */
  1299. public static Date dateMin(Date date, Date compareDate) {
  1300. if (date == null) {
  1301. return compareDate;
  1302. }
  1303. if (compareDate == null) {
  1304. return date;
  1305. }
  1306. if (1 == dateCompare(date, compareDate)) {
  1307. return compareDate;
  1308. } else if (-1 == dateCompare(date, compareDate)) {
  1309. return date;
  1310. }
  1311. return date;
  1312. }
  1313. /**
  1314. * 获取两个时间中最大的一个时间
  1315. *
  1316. * @param date
  1317. * @param compareDate
  1318. * @return Date
  1319. */
  1320. public static Date dateMax(Date date, Date compareDate) {
  1321. if (date == null) {
  1322. return compareDate;
  1323. }
  1324. if (compareDate == null) {
  1325. return date;
  1326. }
  1327. if (1 == dateCompare(date, compareDate)) {
  1328. return date;
  1329. } else if (-1 == dateCompare(date, compareDate)) {
  1330. return compareDate;
  1331. }
  1332. return date;
  1333. }
  1334. /**
  1335. * 获取时间当年某个月的最后一天
  1336. *
  1337. * @param startDate
  1338. * @param month 月份
  1339. * @return int 天数
  1340. */
  1341. public static int getLastDayOfMonth(Date startDate, int month) {
  1342. if (startDate == null) {
  1343. startDate = new Date();
  1344. }
  1345. Calendar c = Calendar.getInstance();
  1346. c.setTime(startDate);
  1347. c.set(c.get(Calendar.YEAR), month, 1);
  1348. c.add(Calendar.DATE, -1);
  1349. int day = c.get(Calendar.DAY_OF_MONTH);
  1350. return day;
  1351. }
  1352. /**
  1353. * 获取固定日期范围内的所有日期,以数组形式返回
  1354. */
  1355. public static List<Date> getAllDays(Date startTime, Date endTime) {
  1356. List<Date> listDay = new ArrayList<>();
  1357. Date dtDay = new Date();
  1358. for (dtDay = startTime; dtDay.compareTo(endTime) <= 0; dtDay = DateUtil.dateAddDays(dtDay, 1)) {
  1359. listDay.add(dtDay);
  1360. }
  1361. return listDay;
  1362. }
  1363. //====================================通过cron转换成时间======================
  1364. /**
  1365. * 获取最近几次的运行时间
  1366. *
  1367. * @param cron 表达式
  1368. * @param numTimes 次数
  1369. * @return
  1370. */
  1371. public static List<String> getRecentExecTime(String cron, int numTimes) {
  1372. List<String> list = new ArrayList<>();
  1373. try {
  1374. CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
  1375. cronTriggerImpl.setCronExpression(cron);
  1376. // 这个是重点,一行代码搞定
  1377. List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, numTimes);
  1378. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
  1379. for (Date date : dates) {
  1380. list.add(dateFormat.format(date));
  1381. }
  1382. } catch (ParseException e) {
  1383. e.getMessage();
  1384. }
  1385. return list;
  1386. }
  1387. /**
  1388. * 获取从某个时间段下一次执行的运行时间
  1389. *
  1390. * @param cron 表达式
  1391. * @param dates 日期
  1392. * @return
  1393. */
  1394. public static String getNextCron(String cron, Date dates) {
  1395. String crons = null;
  1396. if (dates == null) {
  1397. dates = new Date();
  1398. }
  1399. if (StringUtil.isEmpty(cron)) {
  1400. return crons;
  1401. }
  1402. CronExpression cronExpression = null;
  1403. try {
  1404. cronExpression = new CronExpression(cron);
  1405. } catch (ParseException e) {
  1406. System.out.println(e.getMessage());
  1407. return crons;
  1408. }
  1409. Date date = cronExpression.getNextValidTimeAfter(dates);
  1410. if (date != null) {
  1411. crons = dateFormat(date);
  1412. }
  1413. return crons;
  1414. }
  1415. /**
  1416. * 获取从某个时间段下一次执行的运行时间
  1417. *
  1418. * @param cron 表达式
  1419. * @param dates 日期
  1420. * @return
  1421. */
  1422. public static Date getNextCronDate(String cron, Date dates) {
  1423. Date crons = new Date();
  1424. if (dates == null) {
  1425. dates = new Date();
  1426. }
  1427. if (StringUtil.isEmpty(cron)) {
  1428. return crons;
  1429. }
  1430. CronExpression cronExpression = null;
  1431. try {
  1432. cronExpression = new CronExpression(cron);
  1433. } catch (ParseException e) {
  1434. System.out.println(e.getMessage());
  1435. return crons;
  1436. }
  1437. Date date = cronExpression.getNextValidTimeAfter(dates);
  1438. return date;
  1439. }
  1440. public static void getNextDate(int num, String repetition, Date dates, Date repeatTime, List<Date> dataList) {
  1441. if (dates == null) {
  1442. dates = new Date();
  1443. }
  1444. if (repeatTime == null) {
  1445. repeatTime = new Date();
  1446. }
  1447. int result = 0;
  1448. List<String> repetitionList = new ArrayList() {{
  1449. add("2" );
  1450. add("3" );
  1451. add("4" );
  1452. add("5" );
  1453. }};
  1454. if (ObjectUtil.isNotEmpty(repetition) && repetitionList.contains(repetition)) {
  1455. switch (repetition) {
  1456. case "2":
  1457. DateTime day = cn.hutool.core.date.DateUtil.offsetDay(dates, num);
  1458. if (day.getTime() <= repeatTime.getTime()) {
  1459. dataList.add(day);
  1460. num++;
  1461. result++;
  1462. }
  1463. break;
  1464. case "3":
  1465. DateTime week = cn.hutool.core.date.DateUtil.offsetDay(dates, num * 7);
  1466. if (week.getTime() <= repeatTime.getTime()) {
  1467. dataList.add(week);
  1468. num++;
  1469. result++;
  1470. }
  1471. break;
  1472. case "4":
  1473. DateTime month = cn.hutool.core.date.DateUtil.offsetMonth(dates, num);
  1474. if (month.getTime() <= repeatTime.getTime()) {
  1475. dataList.add(month);
  1476. num++;
  1477. result++;
  1478. }
  1479. break;
  1480. case "5":
  1481. DateTime year = cn.hutool.core.date.DateUtil.offsetMonth(dates, num * 12);
  1482. if (year.getTime() <= repeatTime.getTime()) {
  1483. dataList.add(year);
  1484. num++;
  1485. result++;
  1486. }
  1487. break;
  1488. default:
  1489. break;
  1490. }
  1491. if (result > 0) {
  1492. getNextDate(num, repetition, dates, repeatTime, dataList);
  1493. }
  1494. } else {
  1495. dataList.add(dates);
  1496. }
  1497. }
  1498. public static void getNextDate(Date dates, String repetition, Date repeatTime, List<Date> dataList) {
  1499. if (dates == null) {
  1500. dates = new Date();
  1501. }
  1502. if (repeatTime == null) {
  1503. repeatTime = new Date();
  1504. }
  1505. List<String> repetitionList = new ArrayList() {{
  1506. add("2" );
  1507. add("3" );
  1508. add("4" );
  1509. add("5" );
  1510. }};
  1511. if (repeatTime.getTime() >= dates.getTime() && ObjectUtil.isNotEmpty(repetition) && repetitionList.contains(repetition)) {
  1512. dataList.add(dates);
  1513. Date date = dates;
  1514. switch (repetition) {
  1515. case "2":
  1516. date = cn.hutool.core.date.DateUtil.offsetDay(dates, 1);
  1517. break;
  1518. case "3":
  1519. date = cn.hutool.core.date.DateUtil.offsetDay(dates, 7);
  1520. break;
  1521. case "4":
  1522. date = cn.hutool.core.date.DateUtil.offsetMonth(dates, 1);
  1523. break;
  1524. case "5":
  1525. date = cn.hutool.core.date.DateUtil.offsetMonth(dates, 12);
  1526. break;
  1527. default:
  1528. break;
  1529. }
  1530. getNextDate(date, repetition, repeatTime, dataList);
  1531. } else if (!repetitionList.contains(repetition)) {
  1532. dataList.add(dates);
  1533. }
  1534. }
  1535. /**
  1536. * 时间转成cron
  1537. *
  1538. * @return
  1539. */
  1540. public static String getDateToCron(Date date) {
  1541. String cron = null;
  1542. String dateFormat = "ss mm HH dd MM ?";
  1543. if (date == null) {
  1544. cron = dateNow(dateFormat);
  1545. } else {
  1546. cron = dateToString(date, dateFormat);
  1547. }
  1548. return cron;
  1549. }
  1550. public static boolean timeCalendar(Date nowTime, Date dayTimeStart, Date dayTimeEnd) {
  1551. //设置当前时间
  1552. Calendar date = Calendar.getInstance();
  1553. date.setTime(nowTime);
  1554. //设置开始时间
  1555. Calendar timeStart = Calendar.getInstance();
  1556. timeStart.setTime(dayTimeStart);
  1557. //设置结束时间
  1558. Calendar timeEnd = Calendar.getInstance();
  1559. timeEnd.setTime(dayTimeEnd);
  1560. if (date.equals(timeStart) || date.equals(timeEnd)) {
  1561. return true;
  1562. }
  1563. if ((date.after(timeStart) && date.before(timeEnd))) {
  1564. return true;
  1565. } else {
  1566. return false;
  1567. }
  1568. }
  1569. public static boolean isValidDate(String str) {
  1570. boolean convertSuccess = true;
  1571. // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
  1572. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
  1573. if (str.length() == 10) {
  1574. format = new SimpleDateFormat("yyyy-MM-dd" );
  1575. }
  1576. try {
  1577. //设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
  1578. format.setLenient(false);
  1579. format.parse(str);
  1580. } catch (ParseException e) {
  1581. convertSuccess = false;
  1582. }
  1583. return convertSuccess;
  1584. }
  1585. public static boolean isOverlap(Date leftStartDate, Date leftEndDate, Date rightStartDate, Date rightEndDate) {
  1586. return ((leftStartDate.getTime() >= rightStartDate.getTime())
  1587. && leftStartDate.getTime() < rightEndDate.getTime())
  1588. ||
  1589. ((leftStartDate.getTime() > rightStartDate.getTime())
  1590. && leftStartDate.getTime() <= rightEndDate.getTime())
  1591. ||
  1592. ((rightStartDate.getTime() >= leftStartDate.getTime())
  1593. && rightStartDate.getTime() < leftEndDate.getTime())
  1594. ||
  1595. ((rightStartDate.getTime() > leftStartDate.getTime())
  1596. && rightStartDate.getTime() <= leftEndDate.getTime());
  1597. }
  1598. /**
  1599. * 时间范围添加连接符
  1600. *
  1601. * @param jsonArray
  1602. * @param type
  1603. * @param format
  1604. * @return
  1605. */
  1606. public static JSONArray addCon(JSONArray jsonArray, String type, String format) {
  1607. if ("timeRange".equals(type)) {
  1608. String value1 = jsonArray.get(0).toString();
  1609. String value2 = jsonArray.get(1).toString();
  1610. jsonArray.clear();
  1611. jsonArray.add(value1 + "至" );
  1612. jsonArray.add(value2);
  1613. }
  1614. if ("dateRange".equals(type)) {
  1615. DateTimeFormatter ftfDateRange = DateTimeFormatter.ofPattern(format);
  1616. long date1 = Long.parseLong(String.valueOf(jsonArray.get(0)));
  1617. long date2 = Long.parseLong(String.valueOf(jsonArray.get(1)));
  1618. String value1 = ftfDateRange.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(date1), ZoneId.systemDefault()));
  1619. String value2 = ftfDateRange.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(date2), ZoneId.systemDefault()));
  1620. jsonArray.clear();
  1621. jsonArray.add(value1 + "至" );
  1622. jsonArray.add(value2);
  1623. }
  1624. return jsonArray;
  1625. }
  1626. /**
  1627. * 获取时间戳
  1628. */
  1629. public static Long getMillis2() {
  1630. return Instant.now().toEpochMilli();
  1631. }
  1632. /**
  1633. * LocalDateTime转时间戳
  1634. */
  1635. public static Long localDateTime2Millis(LocalDateTime localDateTime) {
  1636. return localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
  1637. }
  1638. /**
  1639. * LocalDate转时间戳
  1640. */
  1641. public static Long localDate2Millis(LocalDate localDate) {
  1642. return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
  1643. }
  1644. /**
  1645. * Clock转时间戳
  1646. */
  1647. public static Long clock2Millis(Clock clock) {
  1648. return clock.millis();
  1649. }
  1650. /**
  1651. * ZoneDateTIme转时间戳(这个不常用吧~)
  1652. */
  1653. public static Long zoneDateTime2Millis(ZonedDateTime zonedDateTime) {
  1654. return zonedDateTime.toLocalDateTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
  1655. }
  1656. /**
  1657. * String转时间戳(JDK8)
  1658. */
  1659. public static Long string2MillisWithJdk8(String dateStr, String formatStr) {
  1660. return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(formatStr)).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
  1661. }
  1662. /**
  1663. * ZonedDateTime转String
  1664. *
  1665. * @param zonedDateTime
  1666. * @return
  1667. */
  1668. public static String getZonedDateTimeToString(ZonedDateTime zonedDateTime) {
  1669. ZonedDateTime zoneDateTime1 = zonedDateTime.plusHours(11);
  1670. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss" );
  1671. return zoneDateTime1.format(formatter);
  1672. }
  1673. /**
  1674. * 根据格式判断时间值是否正确
  1675. * @param dataStr
  1676. * @param format
  1677. * @return
  1678. */
  1679. public static Date checkDate(String dataStr,String format){
  1680. try {
  1681. if (format.length() != dataStr.length()) throw new DataException();
  1682. SimpleDateFormat formatter = new SimpleDateFormat(format);
  1683. formatter.setLenient(false);
  1684. return formatter.parse(dataStr);
  1685. } catch (Exception e) {
  1686. return null;
  1687. }
  1688. }
  1689. }