DateUtils.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. package cn.cslg.pas.common.utils;
  2. import cn.cslg.pas.common.model.cronModel.Calculate;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.date.DateTime;
  5. import org.joda.time.format.DateTimeFormat;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import java.lang.management.ManagementFactory;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.time.format.DateTimeFormatter;
  12. import java.util.*;
  13. import java.util.regex.Pattern;
  14. public class DateUtils {
  15. public static final String START_TIME = " 00:00:00";
  16. public static final String END_TIME = " 23:59:59";
  17. public final static String FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
  18. public final static String[] REPLACE_STRING = new String[]{"GMT+0800", "GMT+08:00"};
  19. public final static String SPLIT_STRING = "(中国标准时间)";
  20. public static Logger log = LoggerFactory.getLogger(DateUtils.class);
  21. public static String YYYY = "yyyy";
  22. public static String YYYY_MM = "yyyy-MM";
  23. public static String YYYY_MM_DD = "yyyy-MM-dd";
  24. public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
  25. public static String YYYYMMDD = "yyyyMMdd";
  26. public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  27. private static String[] parsePatterns = {
  28. "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
  29. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
  30. "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
  31. public static String getDateSourceName(Date startTime, Date endTime, Integer offset, Integer index) {
  32. String ret = null;
  33. switch (offset) {
  34. //月份
  35. case -1:
  36. ret = DateUtil.format(startTime, "yyyy-MM");
  37. break;
  38. //季度
  39. case -3:
  40. ret = String.format("%s-Q%s", DateUtil.format(startTime, "yyyy"), (index % 4) + 1);
  41. break;
  42. //半年
  43. case -6:
  44. ret = String.format("%s-%s", DateUtil.format(startTime, "yyyy"), index % 2 == 0 ? "H1" : "H2");
  45. break;
  46. //1年
  47. case -12:
  48. ret = DateUtil.format(startTime, "yyyy");
  49. break;
  50. //2年,3年,5年
  51. case -24:
  52. case -36:
  53. case -60:
  54. ret = String.format("%s-%s", DateUtil.format(DateUtil.offsetMonth(endTime, offset / 12 * -1), "yyyy"), DateUtil.format(startTime, "yyyy"));
  55. break;
  56. }
  57. return ret;
  58. }
  59. private DateUtils() {
  60. }
  61. public static Integer getWeek(String beginDateStr, String endDateStr, String date) {
  62. DateTime beginDate = DateUtil.parseDate(beginDateStr);
  63. DateTime endDate = DateUtil.parseDate(endDateStr);
  64. DateTime currentDate = DateUtil.parseDate(date);
  65. int week = 1;
  66. if (DateUtil.isIn(currentDate, beginDate, endDate)) {
  67. Date endDateOfWeek = DateUtil.endOfWeek(beginDate);
  68. while (!DateUtil.isIn(currentDate, beginDate, endDateOfWeek)) {
  69. ++week;
  70. endDateOfWeek = DateUtil.endOfWeek(DateUtil.offsetDay(endDateOfWeek, 1));
  71. }
  72. return week;
  73. }
  74. return -1;
  75. }
  76. public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
  77. Calendar date = Calendar.getInstance();
  78. date.setTime(nowTime);
  79. Calendar begin = Calendar.getInstance();
  80. begin.setTime(beginTime);
  81. Calendar end = Calendar.getInstance();
  82. end.setTime(endTime);
  83. return date.after(begin) && date.before(end);
  84. }
  85. /**
  86. * 获取现在的时间 yyyy-MM-dd HH:mm:ss
  87. */
  88. public static String getNowTime() {
  89. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  90. Date date = new Date(System.currentTimeMillis());
  91. return format.format(date);
  92. }
  93. /**
  94. * 获取当前Date型日期
  95. *
  96. * @return Date() 当前日期
  97. */
  98. public static Date getNowDate() {
  99. return new Date();
  100. }
  101. public static Integer getDateTime() {
  102. return (int) (new Date().getTime() / 1000);
  103. }
  104. // public static Integer getDateTime(String date) {
  105. // int dateTime = 0;
  106. // if (date.contains("/")) {
  107. // dateTime = Math.toIntExact(strToDateTime(date, parsePatterns[4]).getTime() / 1000);
  108. // } else if (date.contains("-")) {
  109. // dateTime = Math.toIntExact(strToDateTime(date, YYYY_MM_DD).getTime() / 1000);
  110. // }
  111. // return dateTime;
  112. // }
  113. public static Date getDateTime(String date) {
  114. Date dateTime = null;
  115. if (date.contains("/")) {
  116. dateTime = strToDateTime(date, parsePatterns[4]);
  117. } else if (date.contains("-")) {
  118. dateTime = strToDateTime(date, YYYY_MM_DD);
  119. }
  120. return dateTime;
  121. }
  122. /**
  123. * @author 陌溪
  124. * @date 2018年6月14日
  125. */
  126. public static String getNowTimeFormat(String format) {
  127. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
  128. Date date = new Date(System.currentTimeMillis());
  129. return simpleDateFormat.format(date);
  130. }
  131. public static Date str2Date(String dateString) {
  132. try {
  133. dateString = dateString.split(Pattern.quote(SPLIT_STRING))[0].replace(REPLACE_STRING[0], REPLACE_STRING[1]);
  134. SimpleDateFormat sf1 = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
  135. return sf1.parse(dateString);
  136. } catch (Exception e) {
  137. throw new RuntimeException("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
  138. }
  139. }
  140. public static Date strToDate(String dateString) {
  141. try {
  142. SimpleDateFormat sf1;
  143. if (dateString.contains("-")) {
  144. sf1 = new SimpleDateFormat("yyyy-MM-dd");
  145. } else if (dateString.contains("/")) {
  146. sf1 = new SimpleDateFormat("yyyy/MM/dd");
  147. } else if (dateString.contains(":")) {
  148. sf1 = new SimpleDateFormat("yyyy:MM:dd");
  149. } else if (dateString.contains(".")) {
  150. sf1 = new SimpleDateFormat("yyyy.MM.dd");
  151. } else {
  152. sf1 = new SimpleDateFormat("yyyyMMdd");
  153. }
  154. return sf1.parse(dateString);
  155. } catch (Exception e) {
  156. throw new RuntimeException("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
  157. }
  158. }
  159. /**
  160. * 获取今天开始的时间
  161. */
  162. public static String getToDayStartTime() {
  163. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  164. Date date = new Date(System.currentTimeMillis());
  165. return format.format(date);
  166. }
  167. /**
  168. * 获取今天结束的时间
  169. */
  170. public static String getToDayEndTime() {
  171. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  172. Date date = new Date(System.currentTimeMillis());
  173. return format.format(date);
  174. }
  175. /**
  176. * 获取昨天开始的时间
  177. */
  178. public static String getYesterdayStartTime() {
  179. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  180. Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
  181. return format.format(date);
  182. }
  183. /**
  184. * 获取昨天结束的时间
  185. */
  186. public static String getYesterdayEndTime() {
  187. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  188. Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
  189. return format.format(date);
  190. }
  191. /**
  192. * 获取某天开始的时间
  193. */
  194. public static String getOneDayStartTime(String oneDay) {
  195. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  196. Date date = new Date(oneDay);
  197. return format.format(oneDay);
  198. }
  199. /**
  200. * 获取某天开始的日期
  201. */
  202. public static String getOneDayStartTime(Date oneDay) {
  203. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  204. return format.format(oneDay);
  205. }
  206. /**
  207. * 获取某天结束的时间
  208. */
  209. public static String getOneDayEndTime(String oneDay) {
  210. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  211. Date date = new Date(oneDay);
  212. return format.format(date);
  213. }
  214. /**
  215. * 获取某天结束的日期
  216. */
  217. public static String getOneDayEndTime(Date oneDay) {
  218. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  219. return format.format(oneDay);
  220. }
  221. /**
  222. * 获取本周开始的时间
  223. */
  224. public static Date getWeekStartTime() {
  225. //获得本周一0点时间
  226. Calendar cal = Calendar.getInstance();
  227. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  228. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  229. return cal.getTime();
  230. }
  231. /**
  232. * 将 String 转换成 Date
  233. */
  234. public static Date strToDateTime(String dateTime) {
  235. Date date = null;
  236. try {
  237. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  238. date = format.parse(dateTime);
  239. } catch (ParseException e) {
  240. e.printStackTrace();
  241. }
  242. return date;
  243. }
  244. /**
  245. * 将 String 转换成 Date (转换格式可传入)
  246. */
  247. public static Date strToDateTime(String dateTime, String fmt) {
  248. Date date = null;
  249. try {
  250. SimpleDateFormat format = new SimpleDateFormat(fmt);
  251. date = format.parse(dateTime);
  252. } catch (ParseException e) {
  253. e.printStackTrace();
  254. }
  255. return date;
  256. }
  257. /**
  258. * 将 Date 转换成时间戳
  259. */
  260. public static Long dateToStamp(String s) throws ParseException {
  261. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  262. Date date = simpleDateFormat.parse(s);
  263. return date.getTime();
  264. }
  265. /**
  266. * 将 Date 转换成 String
  267. */
  268. public static String dateTimeToStr(Date dateTime) {
  269. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  270. return format.format(dateTime);
  271. }
  272. public static String dateTimeToStr(Date dateTime, String fmt) {
  273. SimpleDateFormat format = new SimpleDateFormat(fmt);
  274. return format.format(dateTime);
  275. }
  276. /**
  277. * 获取本周开始的时间的字符串
  278. */
  279. public static String getWeekStartTimeStr() {
  280. //获得本周一0点时间
  281. Calendar cal = Calendar.getInstance();
  282. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  283. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  284. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  285. return format.format(cal.getTime());
  286. }
  287. /**
  288. * 获取本周结束的时间
  289. */
  290. public static Date getWeekEndTime() {
  291. Calendar cal = Calendar.getInstance();
  292. cal.setTime(getWeekStartTime());
  293. cal.add(Calendar.DAY_OF_WEEK, 7);
  294. return cal.getTime();
  295. }
  296. /**
  297. * 获取本周结束的时间的字符串
  298. */
  299. public static String getWeekEndTimeStr() {
  300. Calendar cal = Calendar.getInstance();
  301. cal.setTime(getWeekStartTime());
  302. cal.add(Calendar.DAY_OF_WEEK, 7);
  303. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  304. return format.format(cal.getTime());
  305. }
  306. /**
  307. * 获取上周开始的时间的字符串
  308. */
  309. public static String getLastWeekStartTimeStr() {
  310. int weeks = -1;
  311. int mondayPlus = getMondayPlus();
  312. GregorianCalendar currentDate = new GregorianCalendar();
  313. currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
  314. Date monday = currentDate.getTime();
  315. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  316. return format.format(monday);
  317. }
  318. /**
  319. * 获取本月开始的时间
  320. */
  321. public static Date getMonthStartTime() {
  322. Calendar cal = Calendar.getInstance();
  323. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  324. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
  325. return cal.getTime();
  326. }
  327. /**
  328. * 获取本月开始的时间的字符串
  329. */
  330. public static String getMonthStartTimeStr() {
  331. Calendar cal = Calendar.getInstance();
  332. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  333. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
  334. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  335. return format.format(cal.getTime());
  336. }
  337. /**
  338. * 获取本月结束的时间
  339. */
  340. public static Date getMonthEndTime() {
  341. Calendar cal = Calendar.getInstance();
  342. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  343. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  344. cal.set(Calendar.HOUR_OF_DAY, 24);
  345. return cal.getTime();
  346. }
  347. /**
  348. * 获取本月结束的时间的字符串
  349. */
  350. public static String getMonthEndTimeStr() {
  351. Calendar cal = Calendar.getInstance();
  352. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  353. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  354. cal.set(Calendar.HOUR_OF_DAY, 24);
  355. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  356. return format.format(cal.getTime());
  357. }
  358. /**
  359. * 获取当月的 天数
  360. */
  361. public static int getCurrentMonthDay() {
  362. Calendar a = Calendar.getInstance();
  363. a.set(Calendar.DATE, 1);
  364. a.roll(Calendar.DATE, -1);
  365. return a.get(Calendar.DATE);
  366. }
  367. /**
  368. * 得到二个日期间的间隔天数
  369. */
  370. public static int getDayByTwoDay(String date1, String date2) {
  371. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  372. long day;
  373. try {
  374. Date date = myFormatter.parse(date1);
  375. Date myDate = myFormatter.parse(date2);
  376. day = (date.getTime() - myDate.getTime()) / (24 * 60 * 60 * 1000);
  377. } catch (Exception e) {
  378. return 0;
  379. }
  380. return (int) day;
  381. }
  382. /**
  383. * 得到两个日期相差的秒数
  384. */
  385. public static int getSecondByTwoDay(Date lastDate, Date date) {
  386. long second;
  387. try {
  388. second = (lastDate.getTime() - date.getTime()) / 1000;
  389. } catch (Exception e) {
  390. return 0;
  391. }
  392. return (int) second;
  393. }
  394. /**
  395. * 判断某个日期属于本周的第几天 (星期一代表第一天)
  396. */
  397. public static int getDaysByWeek(String dateTime) throws ParseException {
  398. Calendar cal = Calendar.getInstance();
  399. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  400. Date date = dateFormat.parse(dateTime);
  401. cal.setTime(date);
  402. int day = cal.get(Calendar.DAY_OF_WEEK);
  403. day = day - 1;
  404. if (day == 0) {
  405. day = 7;
  406. }
  407. return day;
  408. }
  409. /**
  410. * 判断某个日期属于本月的第几天
  411. */
  412. public static int getDaysByMonth(String dateTime) throws ParseException {
  413. Calendar cal = Calendar.getInstance();
  414. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  415. Date date = dateFormat.parse(dateTime);
  416. cal.setTime(date);
  417. return cal.get(Calendar.DAY_OF_MONTH);
  418. }
  419. /**
  420. * 根据年 月 获取对应的月份 天数
  421. */
  422. public static int getDaysByYearMonth(int year, int month) {
  423. Calendar a = Calendar.getInstance();
  424. a.set(Calendar.YEAR, year);
  425. a.set(Calendar.MONTH, month - 1);
  426. a.set(Calendar.DATE, 1);
  427. a.roll(Calendar.DATE, -1);
  428. return a.get(Calendar.DATE);
  429. }
  430. /**
  431. * 获取当前的年
  432. */
  433. public static Integer getYears() {
  434. Calendar calendar = new GregorianCalendar(TimeZone
  435. .getDefault());
  436. return calendar.get(Calendar.YEAR);
  437. }
  438. /**
  439. * 获取当前的月
  440. */
  441. public static Integer getMonth() {
  442. Calendar calendar = new GregorianCalendar(TimeZone
  443. .getDefault());
  444. return calendar.get(Calendar.MONTH) + 1;
  445. }
  446. /**
  447. * 获取当前天
  448. */
  449. public static Integer getDay() {
  450. Calendar calendar = new GregorianCalendar(TimeZone
  451. .getDefault());
  452. return calendar.get(Calendar.DAY_OF_MONTH);
  453. }
  454. /**
  455. * wx支付的过期时间
  456. */
  457. public static String getTime(double hour) {
  458. long time = (long) (System.currentTimeMillis() + hour * 60 * 60 * 1000L);
  459. Date date = new Date(time);
  460. SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
  461. return format.format(date);
  462. }
  463. /**
  464. * 获得当前日期与本周日相差的天数
  465. */
  466. private static int getMondayPlus() {
  467. Calendar cd = Calendar.getInstance();
  468. // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
  469. // 因为按中国礼拜一作为第一天所以这里减1
  470. int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1;
  471. if (dayOfWeek == 1) {
  472. return 0;
  473. } else {
  474. return 1 - dayOfWeek;
  475. }
  476. }
  477. /**
  478. * 获取几天之后的日期
  479. *
  480. * @param date yyyy-MM-dd HH:mm:ss
  481. * @param day 加减的天数
  482. */
  483. public static Date getDate(String date, int day) {
  484. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  485. Calendar cal = Calendar.getInstance();
  486. Date beforeDate;
  487. try {
  488. beforeDate = format.parse(date);
  489. cal.setTime(beforeDate);
  490. cal.add(Calendar.DAY_OF_MONTH, day);
  491. return cal.getTime();
  492. } catch (ParseException e) {
  493. e.printStackTrace();
  494. }
  495. return null;
  496. }
  497. /**
  498. * 获取某个日期 在加上 秒数的时间
  499. *
  500. * @param beforeDate yyyy-MM-dd HH:mm:ss
  501. * @param timeSecond 加减的秒数
  502. */
  503. public static String getDateStr(Date beforeDate, Long timeSecond) {
  504. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  505. try {
  506. // 返回毫秒数 + 添加的毫秒数
  507. Long time = beforeDate.getTime() + timeSecond * 1000;
  508. return format.format(time);
  509. } catch (Exception e) {
  510. log.error(e.getMessage());
  511. }
  512. return "";
  513. }
  514. /**
  515. * 把date转换成字符串
  516. */
  517. public static String formatDate(Date date, String code) {
  518. SimpleDateFormat format = new SimpleDateFormat(code);
  519. return format.format(date);
  520. }
  521. public static String formatDate(Integer timestamp, String code) {
  522. if (timestamp == null || timestamp == 0) {
  523. return "";
  524. }
  525. return formatDate(new Date(timestamp * 1000L), code);
  526. }
  527. /**
  528. * 获取过去N天内的日期数组
  529. *
  530. * @param intervals intervals天内
  531. * @param formatStr 格式化字符串 yyyy-MM-dd
  532. * @return 日期数组
  533. */
  534. public static ArrayList<String> getDaysByN(int intervals, String formatStr) {
  535. ArrayList<String> pastDaysList = new ArrayList<>();
  536. for (int i = intervals - 1; i >= 0; i--) {
  537. pastDaysList.add(getPastDate(i, formatStr));
  538. }
  539. return pastDaysList;
  540. }
  541. /**
  542. * 获取过去第几天的日期
  543. */
  544. public static String getPastDate(int past, String formatStr) {
  545. Calendar calendar = Calendar.getInstance();
  546. calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
  547. Date today = calendar.getTime();
  548. SimpleDateFormat format = new SimpleDateFormat(formatStr);
  549. return format.format(today);
  550. }
  551. /**
  552. * 获取某个时间段内所有日期
  553. */
  554. public static List<String> getDayBetweenDates(String begin, String end) {
  555. Date dBegin = strToDateTime(begin);
  556. Date dEnd = strToDateTime(end);
  557. List<String> lDate = new ArrayList<>();
  558. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
  559. lDate.add(sd.format(dBegin));
  560. Calendar calBegin = Calendar.getInstance();
  561. // 使用给定的 Date 设置此 Calendar 的时间
  562. calBegin.setTime(dBegin);
  563. Calendar calEnd = Calendar.getInstance();
  564. // 使用给定的 Date 设置此 Calendar 的时间
  565. calEnd.setTime(dEnd);
  566. // 测试此日期是否在指定日期之后
  567. while (dEnd.after(calBegin.getTime())) {
  568. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  569. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  570. lDate.add(sd.format(calBegin.getTime()));
  571. }
  572. return lDate;
  573. }
  574. /**
  575. * 获取服务器启动时间
  576. */
  577. public static Date getServerStartDate() {
  578. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  579. return new Date(time);
  580. }
  581. /**
  582. * 计算两个时间差
  583. */
  584. public static String getDatePoor(Date endDate, Date nowDate) {
  585. long nd = 1000 * 24 * 60 * 60;
  586. long nh = 1000 * 60 * 60;
  587. long nm = 1000 * 60;
  588. // 获得两个时间的毫秒时间差异
  589. long diff = endDate.getTime() - nowDate.getTime();
  590. // 计算差多少天
  591. long day = diff / nd;
  592. // 计算差多少小时
  593. long hour = diff % nd / nh;
  594. // 计算差多少分钟
  595. long min = diff % nd % nh / nm;
  596. return day + "天" + hour + "小时" + min + "分钟";
  597. }
  598. public static long getTimeDiff(Date date) {
  599. long NTime = date.getTime();
  600. long OTime = getNowDate().getTime();
  601. return (NTime - OTime) / 1000 / 60;
  602. }
  603. public static Date setDateHourAndMinute(Date date, int hour, int minute) {
  604. Calendar calendar = Calendar.getInstance();
  605. calendar.setTime(date);
  606. calendar.set(Calendar.HOUR_OF_DAY, hour);
  607. calendar.set(Calendar.MINUTE, minute);
  608. calendar.set(Calendar.SECOND, 0);
  609. return calendar.getTime();
  610. }
  611. /**
  612. * 根据起止条数计算开始页数、开始页数的开始位置、结束页数、结束页数的结束位置
  613. *
  614. * @param startNumber 起始条数
  615. * @param endNumber 终止条数
  616. * @return 返回计算结果对象(开始页数、开始页数的开始位置、结束页数、结束页数的结束位置)
  617. */
  618. public static Calculate calculateFromStartAndEndNumber(Integer startNumber, Integer endNumber,Integer pageSize) {
  619. int startPage; //检索开始页数
  620. int startNum; //检索开始页数的开始专利位置
  621. int endPage; //检索结束页数
  622. int endNum; //检索结束页数的结束专利位置
  623. if (startNumber % pageSize != 0) {
  624. startPage = startNumber / pageSize;
  625. startNum = startNumber % pageSize;
  626. } else {
  627. startPage = startNumber / pageSize;
  628. startNum = pageSize;
  629. }
  630. if (endNumber % pageSize != 0) {
  631. endPage = endNumber / pageSize + 1;
  632. endNum = endNumber % pageSize;
  633. } else {
  634. endPage = endNumber / pageSize;
  635. endNum = pageSize;
  636. }
  637. Calculate calculate = new Calculate()
  638. .setStartPage(startPage)
  639. .setStartNum(startNum)
  640. .setEndPage(endPage)
  641. .setEndNum(endNum);
  642. return calculate;
  643. }
  644. /**
  645. * 专利之星返回日期格式为字符串 yyyyMMdd,如 "20230713",本方法将其转成10位数字时间戳
  646. *
  647. * @param dateStr yyyyMMdd格式字符串日期
  648. * @return 返回10位数字时间戳
  649. */
  650. public static int stringDateToTimeStamp(String dateStr) {
  651. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  652. Date date;
  653. try {
  654. date = dateFormat.parse(dateStr);
  655. } catch (ParseException e) {
  656. //日期格式转换异常
  657. e.printStackTrace();
  658. return Integer.parseInt(dateStr);
  659. }
  660. long timeStamp = date.getTime() / 1000;
  661. return (int) timeStamp;
  662. }
  663. public static void main(String[] args) {
  664. System.out.println(12%50);
  665. }
  666. }