DateUtils.java 24 KB

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