DateHelper.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. package com.ssj.framework.basic.utils;
  2. import org.apache.commons.lang3.time.DateFormatUtils;
  3. import java.text.DateFormat;
  4. import java.text.NumberFormat;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.time.Instant;
  8. import java.time.LocalDate;
  9. import java.time.ZoneId;
  10. import java.util.*;
  11. public class DateHelper {
  12. private static final long ONE_MINUTE = 60;
  13. private static final long ONE_HOUR = 3600;
  14. private static final long ONE_DAY = 86400;
  15. static String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
  16. static Integer[] weekDay = {7, 1, 2, 3, 4, 5, 6};
  17. /**
  18. * 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
  19. *
  20. * @param sformat yyyyMMddhhmmss
  21. * @return
  22. */
  23. public static String getUserDate(String sformat) {
  24. Date currentTime = new Date();
  25. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  26. String dateString = formatter.format(currentTime);
  27. return dateString;
  28. }
  29. public static String getOrderNo(int k) {
  30. return getUserDate("yyyyMMddHHmmss") + getRandom(k);
  31. }
  32. /**
  33. * 返回一个随机数
  34. *
  35. * @param i
  36. * @return
  37. */
  38. public static String getRandom(int i) {
  39. Random jjj = new Random();
  40. if (i == 0){
  41. return "";
  42. }
  43. String jj = "";
  44. for (int k = 0; k < i; k++) {
  45. jj = jj + jjj.nextInt(9);
  46. }
  47. return jj;
  48. }
  49. public static String formatDateByFormat(Date date, String format) {
  50. String result = "";
  51. if (date != null) {
  52. try {
  53. SimpleDateFormat sdf = new SimpleDateFormat(format);
  54. result = sdf.format(date);
  55. result = "/" + result;
  56. } catch (Exception ex) {
  57. ex.printStackTrace();
  58. }
  59. }
  60. return result;
  61. }
  62. public static Date parseDate(String dateStr, String format) {
  63. Date date = null;
  64. try {
  65. DateFormat df = new SimpleDateFormat(format);
  66. String dt = dateStr;
  67. if ((!"".equals(dt)) && (dt.length() < format.length())) {
  68. dt = dt + format.substring(dt.length()).replaceAll("[YyMmDdHhSs]", "0");
  69. }
  70. date = df.parse(dt);
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. return date;
  75. }
  76. public static String format(String dateStr) throws Exception {
  77. Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
  78. SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
  79. return format.format(date.getTime());
  80. }
  81. public static String format(Date date, String format) {
  82. String result = "";
  83. try {
  84. if (date != null) {
  85. DateFormat df = new SimpleDateFormat(format);
  86. result = df.format(date);
  87. }
  88. } catch (Exception localException) {
  89. }
  90. return result;
  91. }
  92. public static String getDate(Date date) {
  93. return format(date, "yyyy/MM/dd");
  94. }
  95. public static String getDateTime(Date date) {
  96. return format(date, "yyyy-MM-dd HH:mm:ss");
  97. }
  98. /**
  99. * 获取好友的时间显示,如 5分钟前
  100. *
  101. * @param
  102. * @return
  103. */
  104. public static String getTimeDes(Date date) {
  105. Calendar calendar = Calendar.getInstance();
  106. calendar.setTime(date);
  107. long time = date.getTime() / 1000;
  108. long now = System.currentTimeMillis() / 1000;
  109. long ago = now - time;
  110. if ( ONE_HOUR >=ago ) {
  111. return ago / ONE_MINUTE + "分钟前";
  112. } else if (ONE_DAY >=ago) {
  113. return ago / ONE_HOUR + "小时前";
  114. } else if ( ONE_DAY * 2 >=ago ) {
  115. return "昨天";
  116. } else if ( ONE_DAY * 3 >=ago) {
  117. return "前天";
  118. } else {
  119. return ago / ONE_DAY + "天前";
  120. }
  121. }
  122. public static String getDays(Date date, int i) throws Exception {
  123. int days = daysOfTwo(date, new Date());
  124. SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
  125. String time = "";
  126. if (i == 1) {
  127. time = "<br/>" + format.format(date.getTime());
  128. }
  129. if (days == 0) {
  130. return "今天" + time;
  131. } else if (days == 1) {
  132. return "昨天" + time;
  133. } else if (days == 2) {
  134. return "前天" + time;
  135. } else if (days <= 7 && days >= 3) {
  136. return numToUpper(days - 1) + "天前" + time;
  137. } else if (days < 0) {
  138. if (days == -1) {
  139. return "明天" + time;
  140. } else if (days == -2) {
  141. return "后天" + time;
  142. }
  143. return Math.abs(days) + "天" + time;
  144. } else {
  145. if (days / 7 > 3) {
  146. return numToUpper(days / 30) + "月前" + time;
  147. }
  148. return numToUpper(days / 7) + "周前" + time;
  149. }
  150. }
  151. public static String numToUpper(int num) {
  152. String[] u = {"上", "两", "三", "四", "五", "六", "七", "八", "九"};
  153. char[] str = String.valueOf(num).toCharArray();
  154. String rstr = "";
  155. for (int i = 0; i < str.length; i++) {
  156. rstr = rstr + u[Integer.parseInt(str[i] + "")];
  157. }
  158. return rstr;
  159. }
  160. /**
  161. * 将时间转换为标准的天+小时+分+秒格式
  162. */
  163. public static String getStandardTime(long diff){
  164. String DateTimes = null;
  165. long hours = (diff % ( 60 * 60 * 24)) / (60 * 60);
  166. long minutes = (diff % ( 60 * 60)) /60;
  167. long seconds = diff % 60;
  168. if(hours>0){
  169. DateTimes=hours + ":" + minutes + ":" + seconds;
  170. }else if(minutes>0){
  171. DateTimes="00"+":"+minutes + ":" + seconds;
  172. }else{
  173. DateTimes="00"+":"+"00"+":"+seconds;
  174. } return DateTimes;
  175. }
  176. public static String getDifferenceTime(Date one, Date two) {
  177. String DateTimes = null;
  178. long day = 0;
  179. long hour = 0;
  180. long min = 0;
  181. long sec = 0;
  182. long time1 = one.getTime();
  183. long time2 = two.getTime();
  184. long diff;
  185. if (time1 < time2) {
  186. diff = (time2 - time1);
  187. } else {
  188. diff = (time1 - time2);
  189. }
  190. long days = diff / (1000 * 60 * 60 * 24);
  191. long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
  192. long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
  193. long seconds=(diff/1000-days*24*60*60-hours*60*60-minutes*60);//获取秒
  194. long addHour=hours+days*24;
  195. if(addHour>0){
  196. DateTimes=addHour + ":" + minutes + ":" + seconds;
  197. }else if(minutes>0){
  198. DateTimes="00"+":"+minutes + ":" + seconds;
  199. }else{
  200. DateTimes="00"+":"+"00"+":"+seconds;
  201. } return DateTimes;
  202. }
  203. public static String getCostTime(Date one, Date two) {
  204. long time1 = one.getTime();
  205. long time2 = two.getTime();
  206. long diff;
  207. if (time1 < time2) {
  208. diff = time2 - time1;
  209. } else {
  210. diff = time1 - time2;
  211. }
  212. long min = diff / (60 * 1000);
  213. return Long.toString(min);
  214. }
  215. public static Date getNextDay(Date date, int days) {
  216. Calendar calendar = Calendar.getInstance();
  217. calendar.setTime(date);
  218. calendar.add(Calendar.DAY_OF_MONTH, days);
  219. date = calendar.getTime();
  220. return date;
  221. }
  222. /**
  223. * 获取上月的第一天
  224. *
  225. * @param date
  226. * @param
  227. * @return 创建人:ZhangCaibao
  228. * 2018年2月1日 上午9:55:52
  229. * @Date
  230. */
  231. public static Date getLastMonthFirstDay(Date date) {
  232. Calendar calendar = Calendar.getInstance();
  233. calendar.setTime(date);
  234. calendar.add(Calendar.MONTH, -1);
  235. calendar.set(Calendar.DAY_OF_MONTH, 1);
  236. date = calendar.getTime();
  237. return date;
  238. }
  239. /**
  240. * 获取下月的第一天
  241. *
  242. * @param date
  243. * @param
  244. * @return 创建人:ZhangCaibao
  245. * 2018年2月1日 上午9:55:52
  246. * @Date
  247. */
  248. public static Date getNextMonthFirstDay(Date date) {
  249. Calendar calendar = Calendar.getInstance();
  250. calendar.setTime(date);
  251. calendar.add(Calendar.MONTH, 1);
  252. calendar.set(Calendar.DAY_OF_MONTH, 1);
  253. date = calendar.getTime();
  254. return date;
  255. }
  256. public static Date getTheMonthFirstDay() {
  257. Calendar calendar = Calendar.getInstance();
  258. calendar.add(Calendar.MONTH, 0);
  259. calendar.set(Calendar.DAY_OF_MONTH, 1);
  260. return calendar.getTime();
  261. }
  262. /**
  263. * 获取上月的最后一天
  264. *
  265. * @param date
  266. * @param
  267. * @return 创建人:ZhangCaibao
  268. * 2018年2月1日 上午9:55:52
  269. * @Date
  270. */
  271. public static Date getLastMonthLastDay(Date date) {
  272. Calendar calendar = Calendar.getInstance();
  273. calendar.setTime(date);
  274. calendar.set(Calendar.DAY_OF_MONTH, 0);
  275. date = calendar.getTime();
  276. return date;
  277. }
  278. public static int getMonth(Date date) {
  279. Calendar calendar = Calendar.getInstance();
  280. calendar.setTime(date);
  281. int month = calendar.get(Calendar.MONTH) + 1;
  282. return month;
  283. }
  284. /**
  285. * @param
  286. * @return 根据开学时间,和当前时间,相差几周。
  287. * @throws Exception
  288. * @Integer
  289. */
  290. public static Integer diffWeeks(String date) throws Exception {
  291. Date startDate = parseDate(date, "yyyy-MM-dd");
  292. Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
  293. Long weekNum = ((endDate.getTime() / 1000 - startDate.getTime() / 1000) % 604800) > 0 ? (endDate.getTime() / 1000 - startDate.getTime() / 1000) / 604800 + 1 : (endDate.getTime() / 1000 - startDate.getTime() / 1000) / 604800;
  294. return weekNum.intValue();
  295. }
  296. public static Integer diffWeeks(String date, Date start) throws Exception {
  297. Date startDate = parseDate(date, "yyyy-MM-dd");
  298. Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(start));
  299. Long weekNum = ((endDate.getTime() / 1000 - startDate.getTime() / 1000) % 604800) > 0 ? (endDate.getTime() / 1000 - startDate.getTime() / 1000) / 604800 + 1 : (endDate.getTime() / 1000 - startDate.getTime() / 1000) / 604800;
  300. return weekNum.intValue();
  301. }
  302. /**
  303. * 判断当前时间是不是大于活动结束时间
  304. *
  305. * @param
  306. * @param date
  307. * @return
  308. * @throws Exception
  309. */
  310. public static boolean isInDate(Date date, String strDateBegin,
  311. String strDateEnd) {
  312. try {
  313. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  314. Date beginDate = sdf.parse(strDateBegin);
  315. Date endDate = sdf.parse(strDateEnd);
  316. if (date.getTime() >= beginDate.getTime() && date.getTime() <= endDate.getTime()) {
  317. return true;
  318. }
  319. } catch (Exception e) {
  320. return false;
  321. }
  322. return false;
  323. }
  324. public static boolean isInDate(Date date, Date beginDate,
  325. Date endDate) {
  326. try {
  327. if (date.getTime() >= beginDate.getTime() && date.getTime() <= endDate.getTime()) {
  328. return true;
  329. }
  330. } catch (Exception e) {
  331. return false;
  332. }
  333. return false;
  334. }
  335. /**
  336. * 根据日期字符串判断周几
  337. *
  338. * @param
  339. * @return
  340. * @throws Exception
  341. */
  342. public static int getday(Date date) {
  343. Calendar calendar = Calendar.getInstance();
  344. calendar.setTime(date);
  345. //第几天,从周日开始
  346. int day = calendar.get(Calendar.DAY_OF_WEEK);
  347. return day;
  348. }
  349. public static String getWeekDays(Date date) {
  350. int day = getday(date);
  351. return weekDays[day - 1];
  352. }
  353. public static Integer getWeekDay(Date date) {
  354. int day = getday(date);
  355. return weekDay[day - 1];
  356. }
  357. /**
  358. * @param date 当前时间
  359. * @param week_of_year 第几周 下周(1),下下周(2)
  360. * @param day_of_week 周几,周日(1),周一(2),周六(7)
  361. * @return
  362. */
  363. public static Date getNextTuesday(Date date, int week_of_year, int day_of_week) {
  364. Calendar cal = Calendar.getInstance();
  365. cal.setTime(date);
  366. cal.add(Calendar.WEEK_OF_YEAR, week_of_year);
  367. cal.set(Calendar.DAY_OF_WEEK, day_of_week);
  368. return cal.getTime();
  369. }
  370. public static String getPercentage(int num1, int num2) {
  371. // 创建一个数值格式化对象
  372. NumberFormat numberFormat = NumberFormat.getInstance();
  373. // 设置精确到小数点后2位
  374. numberFormat.setMaximumFractionDigits(2);
  375. String result = numberFormat.format((float) num1 / (float) num2 * 100);
  376. return result + "%";
  377. }
  378. /**
  379. * 比较两个日期相差的天数
  380. *
  381. * @param
  382. * @param
  383. * @return
  384. * @throws Exception
  385. */
  386. public static int daysOfTwo(Date smdate, Date bdate) throws Exception {
  387. smdate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(smdate));
  388. bdate = new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(bdate));
  389. Calendar cal = Calendar.getInstance();
  390. cal.setTime(smdate);
  391. long time1 = cal.getTimeInMillis();
  392. cal.setTime(bdate);
  393. long time2 = cal.getTimeInMillis();
  394. long between_days = (time2 - time1) / (1000 * 3600 * 24);
  395. return daysOfTian(smdate,bdate);
  396. }
  397. public static LocalDate date2LocalDate(Date date) {
  398. Instant instant = date.toInstant();
  399. ZoneId zoneId = ZoneId.systemDefault();
  400. LocalDate localDate = instant.atZone(zoneId).toLocalDate();
  401. return localDate;
  402. }
  403. public static int daysOfTian(Date date1, Date date2) {
  404. if (date1 == null || date2 == null) {
  405. throw new RuntimeException("日期不能为空");
  406. }
  407. LocalDate localDate1 = date2LocalDate(date1);
  408. LocalDate localDate2 = date2LocalDate(date2);
  409. return (int) (localDate2.toEpochDay() - localDate1.toEpochDay());
  410. }
  411. /**
  412. * 获取相差的天数
  413. *
  414. * @param
  415. * @param
  416. * @return
  417. * @throws Exception 创建人:ZhangCaibao
  418. * 2017年4月19日 下午4:27:42
  419. * @int
  420. */
  421. public static int daysDiffer(String startdate, String enddate) throws Exception {
  422. Date smdate = new SimpleDateFormat("yyyy-MM-dd").parse(startdate);
  423. Date bdate = new SimpleDateFormat("yyyy-MM-dd").parse(enddate);
  424. return daysOfTian(smdate,bdate);
  425. }
  426. public static String dateFormatString(String dateStr, int days) throws Exception {
  427. Date smdate = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
  428. if (days > 90) {
  429. return format(smdate, "MM月dd日");
  430. } else {
  431. return format(smdate, "YYYY年MM月dd日");
  432. }
  433. }
  434. /**
  435. * 根据时间来拼接
  436. *
  437. * @param
  438. * @param hhmmss
  439. * @return
  440. */
  441. public static Date timeStitching(Date date, String hhmmss) throws Exception {
  442. String yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd").format(date);
  443. Date datefor = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(yyyyMMdd + " " + hhmmss);
  444. return datefor;
  445. }
  446. //判断选择的日期是否是本月
  447. public static boolean isThisMonth(Date date) {
  448. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  449. String param = sdf.format(date);//参数时间
  450. String now = sdf.format(new Date());//当前时间
  451. if (param.equals(now)) {
  452. return true;
  453. }
  454. return false;
  455. }
  456. public static Date getMonthDate(Date smdate, int month) {
  457. Calendar cal = Calendar.getInstance();
  458. cal.setTime(smdate);
  459. cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + month);
  460. return cal.getTime();
  461. }
  462. public static Date getYearDate(Date smdate, int year) {
  463. Calendar cal = Calendar.getInstance();
  464. cal.setTime(smdate);
  465. cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + year);
  466. return cal.getTime();
  467. }
  468. public static Map<String, Object> getDifferenceTimeHourMap(Date one, Date two) {
  469. Map<String, Object> map = new HashMap<String, Object>();
  470. long day = 0;
  471. long hour = 0;
  472. long min = 0;
  473. long sec = 0;
  474. long time1 = one.getTime();
  475. long time2 = two.getTime();
  476. long diff;
  477. if (time1 < time2) {
  478. diff = time2 - time1;
  479. } else {
  480. diff = time1 - time2;
  481. }
  482. day = diff / (24 * 60 * 60 * 1000);
  483. hour = (diff / (60 * 60 * 1000) - day * 24);
  484. min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
  485. sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
  486. map.put("day", day < 10 ? "0" + day : day);
  487. map.put("hour", hour < 10 ? "0" + hour : hour);
  488. map.put("min", min < 10 ? "0" + min : min);
  489. map.put("sec", sec < 10 ? "0" + sec : sec);
  490. map.put("millisecond", diff);
  491. return map;
  492. }
  493. //两时间差
  494. public static long getDifferenceTimeNum(Date one, Date two) {
  495. long time1 = one.getTime();
  496. long time2 = two.getTime();
  497. long diff;
  498. if (time1 < time2) {
  499. diff = time2 - time1;
  500. } else {
  501. diff = time1 - time2;
  502. }
  503. return diff;
  504. }
  505. public static boolean isSameDate(Date date1, Date date2) {
  506. Calendar cal1 = Calendar.getInstance();
  507. cal1.setTime(date1);
  508. Calendar cal2 = Calendar.getInstance();
  509. cal2.setTime(date2);
  510. boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
  511. .get(Calendar.YEAR);
  512. boolean isSameMonth = isSameYear
  513. && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
  514. boolean isSameDate = isSameMonth
  515. && cal1.get(Calendar.DAY_OF_MONTH) == cal2
  516. .get(Calendar.DAY_OF_MONTH);
  517. return isSameDate;
  518. }
  519. /**
  520. * @param sformat 时间格式
  521. * @param beforeDay 如果为负数即当前时间前beforeDay天,如果为正数则为当前时间后beforeDay天
  522. * @return 方法说明:
  523. * <p>
  524. * 创立日期:2018年5月16日 下午3:51:09
  525. * 创建人:yangcan
  526. */
  527. public static String getBeforeDate(String sformat, int beforeDay) {
  528. Calendar calendar = Calendar.getInstance(); //得到日历
  529. calendar.setTime(new Date());//把当前时间赋给日历
  530. calendar.add(Calendar.DAY_OF_MONTH, beforeDay); //设置为当前时间前beforeDay天
  531. SimpleDateFormat formatter = new SimpleDateFormat(sformat);
  532. String dateString = formatter.format(calendar.getTime());
  533. return dateString;
  534. }
  535. /**
  536. * @param date 传入时间
  537. * @param hour 传入时间加小时
  538. * @param minute 传入时间加分钟
  539. * @return 方法说明:返回传入时间加小时,分钟后的时间
  540. * <p>
  541. * 创立日期:2018年6月10日 下午3:19:32
  542. * 创建人:yangcan
  543. */
  544. public static Date getAddHour(Date date, int hour, int minute) {
  545. Calendar cal = Calendar.getInstance();
  546. cal.setTime(date);
  547. cal.add(Calendar.HOUR, hour);
  548. cal.add(Calendar.MINUTE, minute);
  549. return cal.getTime();
  550. }
  551. public static List<Map<String, Object>> nearWeek() {
  552. List<Map<String, Object>> mlist = new ArrayList<Map<String, Object>>();
  553. String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
  554. for (int i = -6; i <= 0; i++) {
  555. Map<String, Object> map = new HashMap<>();
  556. Calendar cal = Calendar.getInstance();
  557. cal.add(5, i);
  558. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  559. if (w < 0){
  560. w = 0;
  561. }
  562. if (i == 0) {
  563. map.put("dateTime", "今");
  564. } else {
  565. map.put("dateTime", new SimpleDateFormat("dd").format(cal.getTime()));
  566. }
  567. map.put("time", new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
  568. map.put("dateWeek", weekDays[w]);
  569. mlist.add(map);
  570. }
  571. return mlist;
  572. }
  573. public static int diffDate(java.util.Date endDate, java.util.Date startDate) {
  574. return (int) ((getMillis(endDate) - getMillis(startDate)) / 86400000L);
  575. }
  576. public static long getMillis(java.util.Date date) {
  577. Calendar c = Calendar.getInstance();
  578. c.setTime(date);
  579. return c.getTimeInMillis();
  580. }
  581. /**
  582. * 计算两个日期之间相差的天数
  583. *
  584. * @param smdate 较小的时间
  585. * @param bdate 较大的时间
  586. * @return 相差天数
  587. * @throws ParseException
  588. */
  589. public static int daysBetween(Date smdate, Date bdate) throws ParseException {
  590. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  591. smdate = sdf.parse(sdf.format(smdate));
  592. bdate = sdf.parse(sdf.format(bdate));
  593. Calendar cal = Calendar.getInstance();
  594. cal.setTime(smdate);
  595. long time1 = cal.getTimeInMillis();
  596. cal.setTime(bdate);
  597. long time2 = cal.getTimeInMillis();
  598. long between_days = (time2 - time1) / (1000 * 3600 * 24);
  599. return Integer.parseInt(String.valueOf(between_days));
  600. }
  601. /**
  602. * 计算两段日期重叠的天数
  603. *
  604. * 这里共有2个时间段(b1-----e1)【b2-----e2】,4个时间点;
  605. * 相当于两条线段(b代表起点,e代表端点,b<=e),4个端点。
  606. * 可分3种情况:
  607. * 1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1<b2||b1>e2)此时,重合天数为零。
  608. * 2.相交。
  609. * 情况一:(b1---【b2---e1)----e2】 if(b1<b2&&e1<e2&&e1>b2)
  610. * 情况二:【b2---(b1---e2】----e1) if(b1>b2&&b1<e2&&e2<e1)
  611. * 3.包含:计算较短的时间段日期长度。
  612. * (b1---【b2-----e2】--e1) if(b1<b2&&e1>e2)
  613. * 【b2---(b1-----e1)--e2】 if(b1>b2&&e1<e2)
  614. *
  615. * @param begindate1 开始日期
  616. * @param enddate1 结束日期
  617. * @param begindate2 开始日期
  618. * @param enddate2 结束日期
  619. * @return
  620. */
  621. public static Integer getDayCoincidence(Date begindate1, Date enddate1, Date begindate2, Date enddate2) throws ParseException {
  622. //转换为天判断
  623. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  624. begindate1 = sdf.parse(sdf.format(begindate1));
  625. enddate1 = sdf.parse(sdf.format(enddate1));
  626. begindate2 = sdf.parse(sdf.format(begindate2));
  627. enddate2 = sdf.parse(sdf.format(enddate2));
  628. long b1 = begindate1.getTime();
  629. long e1 = enddate1.getTime();
  630. long b2 = begindate2.getTime();
  631. long e2 = enddate2.getTime();
  632. assert (b1 < e1 && b2 < e2);
  633. Integer coincidenceDay;
  634. if (b1 <= b2 && e1 >= e2) {
  635. System.out.println("1包含2:(b1---【b2-----e2】--e1)");
  636. coincidenceDay = daysBetween(begindate2, enddate2) + 1;
  637. } else if (b1 >= b2 && e1 <= e2) {
  638. System.out.println("2包含1:【b2---(b1-----e1)--e2】");
  639. coincidenceDay = daysBetween(begindate1, enddate1) + 1;
  640. } else if (b1 >= b2 && b1 <= e2 && e2 <= e1) {
  641. System.out.println("相交:【b2---(b1---e2】----e1)");
  642. coincidenceDay = daysBetween(begindate1, enddate2) + 1;
  643. } else if (b1 <= b2 && e1 <= e2 && e1 >= b2) {
  644. System.out.println("相交:(b1---【b2---e1)----e2】");
  645. coincidenceDay = daysBetween(begindate2, enddate1) + 1;
  646. } else if (e1 < b2 || b1 > e2) {
  647. coincidenceDay = 0;
  648. } else {
  649. System.out.println("意料外的日期组合,无法计算重合天数!");
  650. coincidenceDay = null;
  651. }
  652. //取绝对值
  653. return coincidenceDay != null ? Math.abs(coincidenceDay) : null;
  654. }
  655. /**
  656. * 比较两个日期大小(只精确到日)
  657. *
  658. * @param smallDate 小日期
  659. * @param bigDate 大日期
  660. * @return
  661. */
  662. public static boolean compareTwoDate(Date smallDate, Date bigDate) {
  663. String smallString = DateFormatUtils.format(smallDate, "yyyy-MM-dd");
  664. String bigString = DateFormatUtils.format(bigDate, "yyyy-MM-dd");
  665. if (smallString.compareTo(bigString) <= 0) {
  666. return true;
  667. }
  668. return false;
  669. }
  670. /**
  671. * 获取当前日期是星期几
  672. *
  673. * @param date
  674. * @param style
  675. * @return 当前日期是星期几
  676. */
  677. public static String getWeekOfDate(Date date, int style) {
  678. String[] weekZhouDays = {"周天", "周一", "周二", "周三", "周四", "周五", "周六"};
  679. String[] weekXingqiDays = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
  680. Calendar cal = Calendar.getInstance();
  681. cal.setTime(date);
  682. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  683. if (w < 0) {
  684. w = 0;
  685. }
  686. if (1 == style) {
  687. return weekZhouDays[w];
  688. } else {
  689. return weekXingqiDays[w];
  690. }
  691. }
  692. /**
  693. * 给定月份,得到当月最后一天的零点
  694. */
  695. public static String getLastTime(String date) throws Exception{
  696. Calendar cal=Calendar.getInstance();
  697. cal.setTime(new SimpleDateFormat("yyyy-MM").parse(date));
  698. int lastDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  699. cal.set(Calendar.DAY_OF_MONTH,lastDay);
  700. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  701. String time=sdf.format(cal.getTime())+" 24:00:00";
  702. return time;
  703. }
  704. /**
  705. * 给定月份,得到当月第一天一天的零点
  706. */
  707. public static String getBeginTime(String date) throws Exception{
  708. Calendar cal=Calendar.getInstance();
  709. cal.setTime(new SimpleDateFormat("yyyy-MM").parse(date));
  710. cal.set(Calendar.DAY_OF_MONTH,1);
  711. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  712. String time=sdf.format(cal.getTime())+" 00:00:00";
  713. return time;
  714. }
  715. //得到日历列表,包含周几
  716. public static List<String[]> nextCalendarList() {
  717. List<String[]> calendarList=new ArrayList<String[]>();
  718. Date d = nextMonthFirstDate();
  719. Date date = getMonthStart(d);
  720. Date monthEnd = getMonthEnd(d);
  721. while (!date.after(monthEnd)) {
  722. String[] dataStrs={DateHelper.format(date, "yyyy-MM-dd"),dateToWeek(date)};
  723. calendarList.add(dataStrs);
  724. date = getNext(date);
  725. }
  726. return calendarList;
  727. }
  728. //取得上个月的1号
  729. public static Date theMonthFirstDate(Date date) {
  730. Calendar calendar = Calendar.getInstance();
  731. calendar.setTime(date);
  732. calendar.set(Calendar.DAY_OF_MONTH, 1);
  733. calendar.add(Calendar.MONTH, -1);
  734. return calendar.getTime();
  735. }
  736. //取得下个月的1号
  737. public static Date nextMonthFirstDate() {
  738. Calendar calendar = Calendar.getInstance();
  739. calendar.set(Calendar.DAY_OF_MONTH, 1);
  740. calendar.add(Calendar.MONTH, 1);
  741. return calendar.getTime();
  742. }
  743. //当月的开始日期
  744. public static Date getMonthStart(Date date) {
  745. Calendar calendar = Calendar.getInstance();
  746. calendar.setTime(date);
  747. int index = calendar.get(Calendar.DAY_OF_MONTH);
  748. calendar.add(Calendar.DATE, (1 - index));
  749. return calendar.getTime();
  750. }
  751. //当月的结束日期
  752. private static Date getMonthEnd(Date date) {
  753. Calendar calendar = Calendar.getInstance();
  754. calendar.setTime(date);
  755. calendar.add(Calendar.MONTH, 1);
  756. int index = calendar.get(Calendar.DAY_OF_MONTH);
  757. calendar.add(Calendar.DATE, (-index));
  758. return calendar.getTime();
  759. }
  760. //下一个日期
  761. private static Date getNext(Date date) {
  762. Calendar calendar = Calendar.getInstance();
  763. calendar.setTime(date);
  764. calendar.add(Calendar.DATE, 1);
  765. return calendar.getTime();
  766. }
  767. //周几
  768. public static String dateToWeek(Date date) {
  769. Calendar cal = Calendar.getInstance();
  770. cal.setTime(date);
  771. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  772. return String.valueOf(weekDay[w]) ;
  773. }
  774. //时间戳转换为标准日期
  775. public static String getStandTime(Date date){
  776. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  777. return format.format(date);
  778. }
  779. public static boolean dateQualsByMonth(Date dateOne, Date dateTwo) {
  780. Calendar calendarOne = Calendar.getInstance();
  781. calendarOne.setTime(dateOne);
  782. Calendar calendarTwo = Calendar.getInstance();
  783. calendarTwo.setTime(dateTwo);
  784. return calendarOne.get(Calendar.YEAR) == calendarTwo.get(Calendar.YEAR) && calendarOne.get(Calendar.MONTH) == calendarTwo.get(Calendar.MONTH);
  785. }
  786. public static boolean isLastDayOfMonth(Date curMonthDate,Date date) {
  787. Calendar calendar = Calendar.getInstance();
  788. calendar.setTime(date);
  789. Calendar maxCalendar = Calendar.getInstance();
  790. maxCalendar.setTime(curMonthDate);
  791. return calendar.get(Calendar.DAY_OF_MONTH) == maxCalendar
  792. .getActualMaximum(Calendar.DAY_OF_MONTH);
  793. }
  794. public static int getDifferMonth(Date start, Date end) {
  795. if(end.getTime()<=start.getTime()) {
  796. return 0;
  797. }
  798. int i=1;
  799. while (true) {
  800. Calendar startCalendar = Calendar.getInstance();
  801. startCalendar.setTime(start);
  802. startCalendar.add(Calendar.MONTH, i);
  803. if(startCalendar.getTime().getTime()>=end.getTime()) {
  804. break;
  805. }
  806. i=i+1;
  807. }
  808. return i;
  809. }
  810. public static void main(String[] args) throws ParseException {
  811. DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  812. System.out.println(getDifferMonth(format.parse("2020-12-01"), format.parse("2020-05-15")));
  813. }
  814. }