DateUtils.java 22 KB

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