DateUtils.java 24 KB

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