DateUtils.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. System.out.println ("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
  157. return null;
  158. }
  159. }
  160. /**
  161. * 获取今天开始的时间
  162. */
  163. public static String getToDayStartTime() {
  164. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  165. Date date = new Date(System.currentTimeMillis());
  166. return format.format(date);
  167. }
  168. /**
  169. * 获取今天结束的时间
  170. */
  171. public static String getToDayEndTime() {
  172. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  173. Date date = new Date(System.currentTimeMillis());
  174. return format.format(date);
  175. }
  176. /**
  177. * 获取昨天开始的时间
  178. */
  179. public static String getYesterdayStartTime() {
  180. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  181. Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
  182. return format.format(date);
  183. }
  184. /**
  185. * 获取昨天结束的时间
  186. */
  187. public static String getYesterdayEndTime() {
  188. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  189. Date date = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000L);
  190. return format.format(date);
  191. }
  192. /**
  193. * 获取某天开始的时间
  194. */
  195. public static String getOneDayStartTime(String oneDay) {
  196. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  197. Date date = new Date(oneDay);
  198. return format.format(oneDay);
  199. }
  200. /**
  201. * 获取某天开始的日期
  202. */
  203. public static String getOneDayStartTime(Date oneDay) {
  204. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  205. return format.format(oneDay);
  206. }
  207. /**
  208. * 获取某天结束的时间
  209. */
  210. public static String getOneDayEndTime(String oneDay) {
  211. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  212. Date date = new Date(oneDay);
  213. return format.format(date);
  214. }
  215. /**
  216. * 获取某天结束的日期
  217. */
  218. public static String getOneDayEndTime(Date oneDay) {
  219. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  220. return format.format(oneDay);
  221. }
  222. /**
  223. * 获取本周开始的时间
  224. */
  225. public static Date getWeekStartTime() {
  226. //获得本周一0点时间
  227. Calendar cal = Calendar.getInstance();
  228. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  229. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  230. return cal.getTime();
  231. }
  232. /**
  233. * 将 String 转换成 Date
  234. */
  235. public static Date strToDateTime(String dateTime) {
  236. Date date = null;
  237. try {
  238. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  239. date = format.parse(dateTime);
  240. } catch (ParseException e) {
  241. e.printStackTrace();
  242. }
  243. return date;
  244. }
  245. /**
  246. * 将 String 转换成 Date (转换格式可传入)
  247. */
  248. public static Date strToDateTime(String dateTime, String fmt) {
  249. Date date = null;
  250. try {
  251. SimpleDateFormat format = new SimpleDateFormat(fmt);
  252. date = format.parse(dateTime);
  253. } catch (ParseException e) {
  254. e.printStackTrace();
  255. }
  256. return date;
  257. }
  258. /**
  259. * 将 Date 转换成时间戳
  260. */
  261. public static Long dateToStamp(String s) throws ParseException {
  262. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  263. Date date = simpleDateFormat.parse(s);
  264. return date.getTime();
  265. }
  266. /**
  267. * 将 Date 转换成 String
  268. */
  269. public static String dateTimeToStr(Date dateTime) {
  270. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  271. return format.format(dateTime);
  272. }
  273. public static String dateTimeToStr(Date dateTime, String fmt) {
  274. SimpleDateFormat format = new SimpleDateFormat(fmt);
  275. return format.format(dateTime);
  276. }
  277. /**
  278. * 获取本周开始的时间的字符串
  279. */
  280. public static String getWeekStartTimeStr() {
  281. //获得本周一0点时间
  282. Calendar cal = Calendar.getInstance();
  283. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  284. cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  285. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  286. return format.format(cal.getTime());
  287. }
  288. /**
  289. * 获取本周结束的时间
  290. */
  291. public static Date getWeekEndTime() {
  292. Calendar cal = Calendar.getInstance();
  293. cal.setTime(getWeekStartTime());
  294. cal.add(Calendar.DAY_OF_WEEK, 7);
  295. return cal.getTime();
  296. }
  297. /**
  298. * 获取本周结束的时间的字符串
  299. */
  300. public static String getWeekEndTimeStr() {
  301. Calendar cal = Calendar.getInstance();
  302. cal.setTime(getWeekStartTime());
  303. cal.add(Calendar.DAY_OF_WEEK, 7);
  304. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  305. return format.format(cal.getTime());
  306. }
  307. /**
  308. * 获取上周开始的时间的字符串
  309. */
  310. public static String getLastWeekStartTimeStr() {
  311. int weeks = -1;
  312. int mondayPlus = getMondayPlus();
  313. GregorianCalendar currentDate = new GregorianCalendar();
  314. currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
  315. Date monday = currentDate.getTime();
  316. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  317. return format.format(monday);
  318. }
  319. /**
  320. * 获取本月开始的时间
  321. */
  322. public static Date getMonthStartTime() {
  323. Calendar cal = Calendar.getInstance();
  324. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  325. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
  326. return cal.getTime();
  327. }
  328. /**
  329. * 获取本月开始的时间的字符串
  330. */
  331. public static String getMonthStartTimeStr() {
  332. Calendar cal = Calendar.getInstance();
  333. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  334. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
  335. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  336. return format.format(cal.getTime());
  337. }
  338. /**
  339. * 获取本月结束的时间
  340. */
  341. public static Date getMonthEndTime() {
  342. Calendar cal = Calendar.getInstance();
  343. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  344. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  345. cal.set(Calendar.HOUR_OF_DAY, 24);
  346. return cal.getTime();
  347. }
  348. /**
  349. * 获取本月结束的时间的字符串
  350. */
  351. public static String getMonthEndTimeStr() {
  352. Calendar cal = Calendar.getInstance();
  353. cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  354. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  355. cal.set(Calendar.HOUR_OF_DAY, 24);
  356. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
  357. return format.format(cal.getTime());
  358. }
  359. /**
  360. * 获取当月的 天数
  361. */
  362. public static int getCurrentMonthDay() {
  363. Calendar a = Calendar.getInstance();
  364. a.set(Calendar.DATE, 1);
  365. a.roll(Calendar.DATE, -1);
  366. return a.get(Calendar.DATE);
  367. }
  368. /**
  369. * 得到二个日期间的间隔天数
  370. */
  371. public static int getDayByTwoDay(String date1, String date2) {
  372. SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
  373. long day;
  374. try {
  375. Date date = myFormatter.parse(date1);
  376. Date myDate = myFormatter.parse(date2);
  377. day = (date.getTime() - myDate.getTime()) / (24 * 60 * 60 * 1000);
  378. } catch (Exception e) {
  379. return 0;
  380. }
  381. return (int) day;
  382. }
  383. /**
  384. * 得到两个日期相差的秒数
  385. */
  386. public static int getSecondByTwoDay(Date lastDate, Date date) {
  387. long second;
  388. try {
  389. second = (lastDate.getTime() - date.getTime()) / 1000;
  390. } catch (Exception e) {
  391. return 0;
  392. }
  393. return (int) second;
  394. }
  395. /**
  396. * 判断某个日期属于本周的第几天 (星期一代表第一天)
  397. */
  398. public static int getDaysByWeek(String dateTime) throws ParseException {
  399. Calendar cal = Calendar.getInstance();
  400. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  401. Date date = dateFormat.parse(dateTime);
  402. cal.setTime(date);
  403. int day = cal.get(Calendar.DAY_OF_WEEK);
  404. day = day - 1;
  405. if (day == 0) {
  406. day = 7;
  407. }
  408. return day;
  409. }
  410. /**
  411. * 判断某个日期属于本月的第几天
  412. */
  413. public static int getDaysByMonth(String dateTime) throws ParseException {
  414. Calendar cal = Calendar.getInstance();
  415. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  416. Date date = dateFormat.parse(dateTime);
  417. cal.setTime(date);
  418. return cal.get(Calendar.DAY_OF_MONTH);
  419. }
  420. /**
  421. * 根据年 月 获取对应的月份 天数
  422. */
  423. public static int getDaysByYearMonth(int year, int month) {
  424. Calendar a = Calendar.getInstance();
  425. a.set(Calendar.YEAR, year);
  426. a.set(Calendar.MONTH, month - 1);
  427. a.set(Calendar.DATE, 1);
  428. a.roll(Calendar.DATE, -1);
  429. return a.get(Calendar.DATE);
  430. }
  431. /**
  432. * 获取当前的年
  433. */
  434. public static Integer getYears() {
  435. Calendar calendar = new GregorianCalendar(TimeZone
  436. .getDefault());
  437. return calendar.get(Calendar.YEAR);
  438. }
  439. /**
  440. * 获取当前的月
  441. */
  442. public static Integer getMonth() {
  443. Calendar calendar = new GregorianCalendar(TimeZone
  444. .getDefault());
  445. return calendar.get(Calendar.MONTH) + 1;
  446. }
  447. /**
  448. * 获取当前天
  449. */
  450. public static Integer getDay() {
  451. Calendar calendar = new GregorianCalendar(TimeZone
  452. .getDefault());
  453. return calendar.get(Calendar.DAY_OF_MONTH);
  454. }
  455. /**
  456. * wx支付的过期时间
  457. */
  458. public static String getTime(double hour) {
  459. long time = (long) (System.currentTimeMillis() + hour * 60 * 60 * 1000L);
  460. Date date = new Date(time);
  461. SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
  462. return format.format(date);
  463. }
  464. /**
  465. * 获得当前日期与本周日相差的天数
  466. */
  467. private static int getMondayPlus() {
  468. Calendar cd = Calendar.getInstance();
  469. // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
  470. // 因为按中国礼拜一作为第一天所以这里减1
  471. int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1;
  472. if (dayOfWeek == 1) {
  473. return 0;
  474. } else {
  475. return 1 - dayOfWeek;
  476. }
  477. }
  478. /**
  479. * 获取几天之后的日期
  480. *
  481. * @param date yyyy-MM-dd HH:mm:ss
  482. * @param day 加减的天数
  483. */
  484. public static Date getDate(String date, int day) {
  485. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  486. Calendar cal = Calendar.getInstance();
  487. Date beforeDate;
  488. try {
  489. beforeDate = format.parse(date);
  490. cal.setTime(beforeDate);
  491. cal.add(Calendar.DAY_OF_MONTH, day);
  492. return cal.getTime();
  493. } catch (ParseException e) {
  494. e.printStackTrace();
  495. }
  496. return null;
  497. }
  498. /**
  499. * 获取几天之后的日期
  500. *
  501. * @param date yyyy-MM-dd HH:mm:ss
  502. * @param day 加减的天数
  503. */
  504. public static Date getDate(Date date, int day) {
  505. Calendar cal = Calendar.getInstance();
  506. cal.setTime(date);
  507. cal.add(Calendar.DAY_OF_MONTH, day);
  508. return cal.getTime();
  509. }
  510. /**
  511. * 获取某个日期 在加上 秒数的时间
  512. *
  513. * @param beforeDate yyyy-MM-dd HH:mm:ss
  514. * @param timeSecond 加减的秒数
  515. */
  516. public static String getDateStr(Date beforeDate, Long timeSecond) {
  517. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  518. try {
  519. // 返回毫秒数 + 添加的毫秒数
  520. Long time = beforeDate.getTime() + timeSecond * 1000;
  521. return format.format(time);
  522. } catch (Exception e) {
  523. log.error(e.getMessage());
  524. }
  525. return "";
  526. }
  527. /**
  528. * 把date转换成字符串
  529. */
  530. public static String formatDate(Date date, String code) {
  531. SimpleDateFormat format = new SimpleDateFormat(code);
  532. return format.format(date);
  533. }
  534. public static String formatDate(Integer timestamp, String code) {
  535. if (timestamp == null || timestamp == 0) {
  536. return "";
  537. }
  538. return formatDate(new Date(timestamp * 1000L), code);
  539. }
  540. /**
  541. * 获取过去N天内的日期数组
  542. *
  543. * @param intervals intervals天内
  544. * @param formatStr 格式化字符串 yyyy-MM-dd
  545. * @return 日期数组
  546. */
  547. public static ArrayList<String> getDaysByN(int intervals, String formatStr) {
  548. ArrayList<String> pastDaysList = new ArrayList<>();
  549. for (int i = intervals - 1; i >= 0; i--) {
  550. pastDaysList.add(getPastDate(i, formatStr));
  551. }
  552. return pastDaysList;
  553. }
  554. /**
  555. * 获取过去第几天的日期
  556. */
  557. public static String getPastDate(int past, String formatStr) {
  558. Calendar calendar = Calendar.getInstance();
  559. calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
  560. Date today = calendar.getTime();
  561. SimpleDateFormat format = new SimpleDateFormat(formatStr);
  562. return format.format(today);
  563. }
  564. /**
  565. * 获取某个时间段内所有日期
  566. */
  567. public static List<String> getDayBetweenDates(String begin, String end) {
  568. Date dBegin = strToDateTime(begin);
  569. Date dEnd = strToDateTime(end);
  570. List<String> lDate = new ArrayList<>();
  571. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
  572. lDate.add(sd.format(dBegin));
  573. Calendar calBegin = Calendar.getInstance();
  574. // 使用给定的 Date 设置此 Calendar 的时间
  575. calBegin.setTime(dBegin);
  576. Calendar calEnd = Calendar.getInstance();
  577. // 使用给定的 Date 设置此 Calendar 的时间
  578. calEnd.setTime(dEnd);
  579. // 测试此日期是否在指定日期之后
  580. while (dEnd.after(calBegin.getTime())) {
  581. // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
  582. calBegin.add(Calendar.DAY_OF_MONTH, 1);
  583. lDate.add(sd.format(calBegin.getTime()));
  584. }
  585. return lDate;
  586. }
  587. /**
  588. * 获取服务器启动时间
  589. */
  590. public static Date getServerStartDate() {
  591. long time = ManagementFactory.getRuntimeMXBean().getStartTime();
  592. return new Date(time);
  593. }
  594. /**
  595. * 计算两个时间差
  596. */
  597. public static String getDatePoor(Date endDate, Date nowDate) {
  598. long nd = 1000 * 24 * 60 * 60;
  599. long nh = 1000 * 60 * 60;
  600. long nm = 1000 * 60;
  601. // 获得两个时间的毫秒时间差异
  602. long diff = endDate.getTime() - nowDate.getTime();
  603. // 计算差多少天
  604. long day = diff / nd;
  605. // 计算差多少小时
  606. long hour = diff % nd / nh;
  607. // 计算差多少分钟
  608. long min = diff % nd % nh / nm;
  609. return day + "天" + hour + "小时" + min + "分钟";
  610. }
  611. public static long getTimeDiff(Date date) {
  612. long NTime = date.getTime();
  613. long OTime = getNowDate().getTime();
  614. return (NTime - OTime) / 1000 / 60;
  615. }
  616. public static Date setDateHourAndMinute(Date date, int hour, int minute) {
  617. Calendar calendar = Calendar.getInstance();
  618. calendar.setTime(date);
  619. calendar.set(Calendar.HOUR_OF_DAY, hour);
  620. calendar.set(Calendar.MINUTE, minute);
  621. calendar.set(Calendar.SECOND, 0);
  622. return calendar.getTime();
  623. }
  624. /**
  625. * 根据起止条数计算开始页数、开始页数的开始位置、结束页数、结束页数的结束位置
  626. *
  627. * @param startNumber 起始条数
  628. * @param endNumber 终止条数
  629. * @return 返回计算结果对象(开始页数、开始页数的开始位置、结束页数、结束页数的结束位置)
  630. */
  631. public static Calculate calculateFromStartAndEndNumber(Integer startNumber, Integer endNumber,Integer pageSize) {
  632. int startPage; //检索开始页数
  633. int startNum; //检索开始页数的开始专利位置
  634. int endPage; //检索结束页数
  635. int endNum; //检索结束页数的结束专利位置
  636. if (startNumber % pageSize != 0) {
  637. startPage = startNumber / pageSize;
  638. startNum = startNumber % pageSize;
  639. } else {
  640. startPage = startNumber / pageSize;
  641. startNum = pageSize;
  642. }
  643. if (endNumber % pageSize != 0) {
  644. endPage = endNumber / pageSize + 1;
  645. endNum = endNumber % pageSize;
  646. } else {
  647. endPage = endNumber / pageSize;
  648. endNum = pageSize;
  649. }
  650. Calculate calculate = new Calculate()
  651. .setStartPage(startPage)
  652. .setStartNum(startNum)
  653. .setEndPage(endPage)
  654. .setEndNum(endNum);
  655. return calculate;
  656. }
  657. /**
  658. * 专利之星返回日期格式为字符串 yyyyMMdd,如 "20230713",本方法将其转成10位数字时间戳
  659. *
  660. * @param dateStr yyyyMMdd格式字符串日期
  661. * @return 返回10位数字时间戳
  662. */
  663. public static int stringDateToTimeStamp(String dateStr) {
  664. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  665. Date date;
  666. try {
  667. date = dateFormat.parse(dateStr);
  668. } catch (ParseException e) {
  669. //日期格式转换异常
  670. e.printStackTrace();
  671. return Integer.parseInt(dateStr);
  672. }
  673. long timeStamp = date.getTime() / 1000;
  674. return (int) timeStamp;
  675. }
  676. public static String strToStr(String dateString,String dateForm) {
  677. try {
  678. SimpleDateFormat sf1;
  679. if (dateString.contains("-")) {
  680. sf1 = new SimpleDateFormat("yyyy-MM-dd");
  681. } else if (dateString.contains("/")) {
  682. sf1 = new SimpleDateFormat("yyyy/MM/dd");
  683. } else if (dateString.contains(":")) {
  684. sf1 = new SimpleDateFormat("yyyy:MM:dd");
  685. } else if (dateString.contains(".")) {
  686. sf1 = new SimpleDateFormat("yyyy.MM.dd");
  687. } else {
  688. sf1 = new SimpleDateFormat("yyyyMMdd");
  689. }
  690. SimpleDateFormat format = new SimpleDateFormat(dateForm);
  691. Date a= sf1.parse(dateString);
  692. String reDate= format.format(a);
  693. return reDate;
  694. } catch (Exception e) {
  695. return null;
  696. }
  697. }
  698. /*
  699. * 获取时间
  700. */
  701. public static String toGMTString(Date date) {
  702. SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
  703. df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
  704. return df.format(date);
  705. }
  706. }