TimeExchange.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. package com.template.common.utils;
  2. import com.template.model.enumModel.eWeekStatu;
  3. import com.template.model.pojo.UnitTimeHelpModel;
  4. import com.template.model.vo.ClassSettingDateVo;
  5. import org.apache.commons.lang3.time.DateFormatUtils;
  6. import org.apache.commons.lang3.time.DateUtils;
  7. import java.math.BigDecimal;
  8. import java.sql.Timestamp;
  9. import java.text.DateFormat;
  10. import java.text.ParseException;
  11. import java.text.SimpleDateFormat;
  12. import java.time.DayOfWeek;
  13. import java.time.LocalDate;
  14. import java.time.LocalDateTime;
  15. import java.time.format.DateTimeFormatter;
  16. import java.time.temporal.ChronoUnit;
  17. import java.util.*;
  18. /**
  19. * 时间转化工具 date转为时间戳 时间戳转date 互相与String的转换
  20. * 所有出现的String time 格式都必须为(yyyy-MM-dd HH:mm:ss),否则出错
  21. *
  22. */
  23. public class TimeExchange {
  24. /**
  25. * String(yyyy-MM-dd HH:mm:ss) 转 Date
  26. *
  27. * @param time
  28. * @return
  29. * @throws ParseException
  30. */
  31. // String date = "2010/05/04 12:34:23";
  32. public static Date StringToDate(String time, String formatDate){
  33. Date date = new Date();
  34. // 注意format的格式要与日期String的格式相匹配
  35. DateFormat dateFormat = new SimpleDateFormat(formatDate);
  36. try {
  37. date = dateFormat.parse(time);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. return date;
  42. }
  43. /**
  44. * Date转为String(yyyy-MM-dd HH:mm:ss)
  45. *
  46. * @param time
  47. * @return
  48. */
  49. public static String DateToString(Date time) {
  50. String dateStr = "";
  51. Date date = new Date();
  52. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
  53. try {
  54. dateStr = dateFormat.format(time);
  55. System.out.println(dateStr);
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. return dateStr;
  60. }
  61. public static String chineseDateTime(Date time){
  62. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
  63. String dateStringParse = null;
  64. try {
  65. dateStringParse = sdf.format(time);
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. return dateStringParse;
  70. }
  71. /**
  72. * String(yyyy-MM-dd HH:mm:ss)转10位时间戳
  73. *
  74. * @param time
  75. * @return
  76. */
  77. public static Integer StringToTimestamp(String time) {
  78. int times = 0;
  79. try {
  80. times = (int) ((Timestamp.valueOf(time).getTime()) / 1000);
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }
  84. if (times == 0) {
  85. System.out.println("String转10位时间戳失败");
  86. }
  87. return times;
  88. }
  89. /**
  90. * 10位int型的时间戳转换为String(yyyy-MM-dd HH:mm:ss)
  91. *
  92. * @param time
  93. * @return
  94. */
  95. public static String timestampToString(Integer time) {
  96. //int转long时,先进行转型再进行计算,否则会是计算结束后在转型
  97. long temp = (long) time * 1000;
  98. Timestamp ts = new Timestamp(temp);
  99. String tsStr = "";
  100. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  101. try {
  102. //方法一
  103. tsStr = dateFormat.format(ts);
  104. System.out.println(tsStr);
  105. } catch (Exception e) {
  106. e.printStackTrace();
  107. }
  108. return tsStr;
  109. }
  110. /**
  111. * 10位时间戳转Date
  112. *
  113. * @param time
  114. * @return
  115. */
  116. public static Date TimestampToDate(Integer time) {
  117. long temp = (long) time * 1000;
  118. Timestamp ts = new Timestamp(temp);
  119. Date date = new Date();
  120. try {
  121. date = ts;
  122. //System.out.println(date);
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. }
  126. return date;
  127. }
  128. /**
  129. * 获取当前时间戳
  130. * @return
  131. */
  132. public static String DateNowTimeStamo(){
  133. Date date = new Date();//获取当前的日期
  134. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");//设置日期格式
  135. String str = df.format(date);//获取String类型的时间
  136. return str;
  137. }
  138. /**
  139. * Date类型转换为10位时间戳
  140. *
  141. * @param time
  142. * @return
  143. */
  144. public static Integer DateToTimestamp(Date time) {
  145. Timestamp ts = new Timestamp(time.getTime());
  146. return (int) ((ts.getTime()) / 1000);
  147. }
  148. // 当前时间减1小时
  149. public static String TimeDesH(Date time, int hour) {
  150. Calendar nowTime2 = Calendar.getInstance();
  151. nowTime2.setTime(time);
  152. nowTime2.add(Calendar.HOUR, -hour);
  153. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  154. return simpleDateFormat.format(nowTime2.getTime());
  155. }
  156. // 当前时间加5分钟
  157. public static String TimeRangeI(String time) throws ParseException {
  158. // 当前时间+5分钟
  159. Date endTime = DateUtils.addMinutes(StringToDate(time, "yyyy-MM-dd HH:mm:ss"), 300);
  160. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  161. return simpleDateFormat.format(endTime);
  162. }
  163. // 当前时间加2分钟
  164. public static String TimeRangeI10(String time, int m) {
  165. Calendar nowTime2 = Calendar.getInstance();
  166. nowTime2.setTime(StringToDate(time, "yyyy-MM-dd HH:mm:ss"));
  167. nowTime2.add(Calendar.SECOND, m);//10分钟前的时间
  168. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  169. return simpleDateFormat.format(nowTime2.getTime());
  170. }
  171. // 当前时间减5分钟
  172. public static String TimeRangeD(String time) throws ParseException {
  173. Calendar nowTime2 = Calendar.getInstance();
  174. nowTime2.setTime(StringToDate(time, "yyyy-MM-dd HH:mm:ss"));
  175. nowTime2.add(Calendar.MINUTE, -300);//5分钟前的时间
  176. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  177. return simpleDateFormat.format(nowTime2.getTime());
  178. }
  179. // 获取当天开始时间
  180. public static String getStartDate() {
  181. Calendar calendar = Calendar.getInstance();
  182. calendar.setTime(new Date());
  183. int year = calendar.get(Calendar.YEAR);
  184. int month = calendar.get(Calendar.MONTH);
  185. int day = calendar.get(Calendar.DATE);
  186. calendar.setTimeInMillis(0);
  187. calendar.set(year, month, day, 0, 0, 0);
  188. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
  189. }
  190. // 获取当天结束时间
  191. public static String getEndDate() {
  192. Calendar calendar = Calendar.getInstance();
  193. calendar.setTime(new Date());
  194. int year = calendar.get(Calendar.YEAR);
  195. int month = calendar.get(Calendar.MONTH);
  196. int day = calendar.get(Calendar.DATE);
  197. calendar.setTimeInMillis(0);
  198. calendar.set(year, month, day, 23, 59, 59);
  199. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
  200. }
  201. // 获取当前日期
  202. public static String getDate() {
  203. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
  204. return sp.format(new Date());
  205. }
  206. // 获取当前日期的年月
  207. public static String getDateMonth() {
  208. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-");
  209. return sp.format(new Date());
  210. }
  211. // 获取前天
  212. public static String getQianDay() throws ParseException {
  213. Calendar nowTime2 = Calendar.getInstance();
  214. nowTime2.setTime(StringToDate(getTime(), "yyyy-MM-dd HH:mm:ss"));
  215. nowTime2.add(Calendar.DATE, -5);//5分钟前的时间
  216. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  217. return simpleDateFormat.format(nowTime2.getTime());
  218. }
  219. // 获取当前时间
  220. public static String getTime() {
  221. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  222. return sp.format(new Date());
  223. }
  224. public static String getOnlyMM() {
  225. SimpleDateFormat sp = new SimpleDateFormat("HH:mm");
  226. return sp.format(new Date());
  227. }
  228. public static String getOnlyDesMM() throws ParseException {
  229. Calendar nowTime2 = Calendar.getInstance();
  230. nowTime2.setTime(StringToDate(getTime(), "yyyy-MM-dd HH:mm:ss"));
  231. nowTime2.add(Calendar.MINUTE, -5);//5分钟前的时间
  232. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
  233. return simpleDateFormat.format(nowTime2.getTime());
  234. }
  235. public static String getYear() {
  236. SimpleDateFormat sp = new SimpleDateFormat("yyyy");
  237. return sp.format(new Date());
  238. }
  239. public static String getMonth() {
  240. SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM");
  241. return sp.format(new Date());
  242. }
  243. public static String getNowMonth() {
  244. SimpleDateFormat sp = new SimpleDateFormat("MM");
  245. return sp.format(new Date());
  246. }
  247. // 获取当前时间
  248. public static String getOnlyTime() {
  249. SimpleDateFormat sp = new SimpleDateFormat("HH:mm:ss");
  250. return sp.format(new Date());
  251. }
  252. /**
  253. * 计算两个日期的时间差
  254. *
  255. * @param time1
  256. * @param time2
  257. * @return
  258. */
  259. public static double getTimeDifference(String time1, String time2) {
  260. SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  261. long t1 = 0L;
  262. long t2 = 0L;
  263. try {
  264. t1 = timeformat.parse(time1).getTime();
  265. } catch (ParseException e) {
  266. // TODO Auto-generated catch block
  267. e.printStackTrace();
  268. }
  269. try {
  270. t2 = timeformat.parse(time2).getTime();
  271. } catch (ParseException e) {
  272. // TODO Auto-generated catch block
  273. e.printStackTrace();
  274. }
  275. //因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理
  276. double hours = (double) ((t2 - t1) / 3600000);
  277. double minutes = (double) (((t2 - t1) / 1000 - hours * 3600) / 60 / 60);
  278. return hours + minutes;
  279. }
  280. public static double getOnlyTimeDifference(String time1, String time2) {
  281. SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
  282. long t1 = 0L;
  283. long t2 = 0L;
  284. try {
  285. t1 = timeformat.parse(time1).getTime();
  286. } catch (ParseException e) {
  287. // TODO Auto-generated catch block
  288. e.printStackTrace();
  289. }
  290. try {
  291. t2 = timeformat.parse(time2).getTime();
  292. } catch (ParseException e) {
  293. // TODO Auto-generated catch block
  294. e.printStackTrace();
  295. }
  296. //因为t1-t2得到的是毫秒级,所以要初3600000得出小时.算天数或秒同理
  297. double hours = (double) ((t2 - t1) / 3600000);
  298. double minutes = (double) (((t2 - t1) / 1000 - hours * 3600) / 60 / 60);
  299. return hours + minutes;
  300. }
  301. public static double getDiff(String str1, String str2) {
  302. return str2.compareTo(str1);
  303. }
  304. /**
  305. * 时间加减分钟数
  306. *
  307. * @param time 时间
  308. * @param FormatStr 时间格式
  309. * @param amount 要加减的时间(单位为分钟)
  310. * @return
  311. * @throws ParseException
  312. */
  313. public static String TimeRangeMinute(String time, int amount, String FormatStr) throws ParseException {
  314. Date endTime = DateUtils.addMinutes(ShortStringToDate(time, FormatStr), amount);
  315. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FormatStr);
  316. return simpleDateFormat.format(endTime);
  317. }
  318. /**
  319. * 时间加减小时数
  320. *
  321. * @param time 时间
  322. * @param FormatStr 时间格式
  323. * @param amount 要加减的时间(单位为小时)
  324. * @return
  325. * @throws ParseException
  326. */
  327. public static String TimeRangeHour(Date time, int amount, String FormatStr) {
  328. Date endTime = DateUtils.addHours(time, amount);
  329. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FormatStr);
  330. return simpleDateFormat.format(endTime);
  331. }
  332. /**
  333. * String 转 Date
  334. *
  335. * @param time 时间
  336. * @param formatStr 自定义时间格式
  337. * @return
  338. * @throws ParseException
  339. */
  340. public static Date ShortStringToDate(String time, String formatStr) throws ParseException {
  341. Date date = new Date();
  342. // 注意format的格式要与日期String的格式相匹配
  343. DateFormat dateFormat = new SimpleDateFormat(formatStr);
  344. try {
  345. date = dateFormat.parse(time);
  346. System.out.println(date.toString());
  347. } catch (Exception e) {
  348. e.printStackTrace();
  349. }
  350. return date;
  351. }
  352. /**
  353. * Date转为String
  354. *
  355. * @param time 时间
  356. * @param FormatStr 自定义时间格式
  357. * @return
  358. */
  359. public static String DateToString(Date time, String FormatStr) {
  360. String dateStr = "";
  361. DateFormat dateFormat = new SimpleDateFormat(FormatStr);
  362. try {
  363. dateStr = dateFormat.format(time);
  364. } catch (Exception e) {
  365. e.printStackTrace();
  366. }
  367. return dateStr;
  368. }
  369. /**
  370. * 日期路径 即年/月/日 如2018/08/08
  371. */
  372. public static final String datePath() {
  373. Date now = new Date();
  374. return DateFormatUtils.format(now, "yyyy/MM/dd");
  375. }
  376. /**
  377. * 比较时间1是否小于时间2
  378. * 如果时间1小于时间2,接口返回true
  379. * 如果时间1大于时间2,接口返回false
  380. *
  381. * @param dateOne 时间1
  382. * @param dateTwo 时间2
  383. * @param Forma 时间格式
  384. * @return
  385. * @throws ParseException
  386. */
  387. public static boolean CompareDate(String dateOne, String dateTwo, String Forma) throws ParseException {
  388. SimpleDateFormat df = new SimpleDateFormat(Forma);
  389. Date sd1 = df.parse(dateOne);
  390. Date sd2 = df.parse(dateTwo);
  391. return sd1.before(sd2);
  392. }
  393. public static LocalDateTime StringToLocalTime(String time, String formatStr){
  394. //1.具有转换功能的对象
  395. DateTimeFormatter df = DateTimeFormatter.ofPattern(formatStr);
  396. //3.LocalDate发动,将字符串转换成  df格式的LocalDateTime对象,的功能
  397. LocalDateTime LocalTime = LocalDateTime.parse(time,df);
  398. return LocalTime;
  399. }
  400. /**
  401. * 获取一周的开始时间和结束时间
  402. * 获取本周星期一作为一周的第一天的起始时间和结束时间
  403. *
  404. * @return 返回的数据中第一个是开始时间 第二个是结束时间
  405. */
  406. public static String[] getCurrentWeekTimeFrame() {
  407. Calendar calendar = Calendar.getInstance();
  408. calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
  409. //start of the week
  410. if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
  411. calendar.add(Calendar.DAY_OF_YEAR, -1);
  412. }
  413. calendar.add(Calendar.DAY_OF_WEEK, -(calendar.get(Calendar.DAY_OF_WEEK) - 2));
  414. //给0的时候查不出数据
  415. // calendar.set(Calendar.HOUR_OF_DAY, 0);
  416. // calendar.set(Calendar.MINUTE, 0);
  417. // calendar.set(Calendar.SECOND, 0);
  418. // calendar.set(Calendar.MILLISECOND, 0);
  419. String startTime = DateToString(calendar.getTime(), "yyyy-MM-dd");
  420. //end of the week
  421. calendar.add(Calendar.DAY_OF_WEEK, 6);
  422. calendar.set(Calendar.HOUR_OF_DAY, 23);
  423. calendar.set(Calendar.MINUTE, 59);
  424. calendar.set(Calendar.SECOND, 59);
  425. calendar.set(Calendar.MILLISECOND, 999);
  426. String endTime = DateToString(calendar.getTime(), "yyyy-MM-dd");
  427. return new String[]{startTime, endTime};
  428. }
  429. /**
  430. * 获取指定月份有多少天
  431. *
  432. * @param month
  433. * @return
  434. */
  435. public static int getMonthDays(String date, int month) {
  436. int year = Integer.valueOf(date.substring(0, 4));
  437. int[] arr = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  438. int day = arr[month - 1];//天数对应=数组-1
  439. if (month == 2 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  440. day = 29;
  441. }
  442. return day;
  443. }
  444. /**
  445. * 获取指定月份的第一天和最后一天
  446. *
  447. * @param DateStr 指定月份
  448. * @return 返回的数据中第一个是开始时间 第二个是结束时间
  449. */
  450. public static String[] getCurrentMonthTimeFrame(String DateStr) {
  451. Calendar c = Calendar.getInstance();//获取Calendar实例
  452. c.set(Calendar.YEAR, Integer.parseInt(DateStr.substring(0, 4)));
  453. int sss = Integer.parseInt(DateStr.substring(5, 7));
  454. c.set(Calendar.MONTH, Integer.parseInt(DateStr.substring(5, 7).replace("-", "")) - 1);
  455. c.set(Calendar.DAY_OF_MONTH, 1);
  456. String startDate = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  457. int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
  458. c.set(Calendar.DAY_OF_MONTH, lastDay);
  459. String endDate = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()) + " 23:59:59";
  460. return new String[]{startDate, endDate};
  461. }
  462. //region 获取某一天的开始时间
  463. public static String getStartOfDayStr(Date date) {
  464. Calendar calendar = Calendar.getInstance();
  465. calendar.setTime(date);
  466. int year = calendar.get(Calendar.YEAR);
  467. int month = calendar.get(Calendar.MONTH);
  468. int day = calendar.get(Calendar.DATE);
  469. calendar.setTimeInMillis(0);
  470. calendar.set(year, month, day, 0, 0, 0);
  471. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
  472. }
  473. //endregion
  474. //region 获取某一天的结束时间
  475. public static String getEndOfDayStr(Date date) {
  476. Calendar calendar = Calendar.getInstance();
  477. calendar.setTime(date);
  478. int year = calendar.get(Calendar.YEAR);
  479. int month = calendar.get(Calendar.MONTH);
  480. int day = calendar.get(Calendar.DATE);
  481. calendar.setTimeInMillis(0);
  482. calendar.set(year, month, day, 23, 59, 59);
  483. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
  484. }
  485. //endregion
  486. //region 获取指定月份的最后一天
  487. public static Date getToMonthEndDate(String DateStr) {
  488. Calendar c = Calendar.getInstance();
  489. c.clear();
  490. c.set(Calendar.YEAR, Integer.parseInt(DateStr.substring(0, 4)));
  491. c.set(Calendar.MONTH, Integer.parseInt(DateStr.substring(5, 7).replace("-", "")) - 1);
  492. c.roll(Calendar.DAY_OF_MONTH, -1);
  493. c.set(Calendar.HOUR_OF_DAY, 23);
  494. c.set(Calendar.MINUTE, 59);
  495. c.set(Calendar.SECOND, 59);
  496. c.set(Calendar.MILLISECOND, 999);
  497. return c.getTime();
  498. }
  499. //endregion
  500. //region 获取指定月份的第一天
  501. public static Date getToMonthBeginDate(String DateStr) {
  502. Calendar c = Calendar.getInstance();
  503. c.clear();
  504. c.set(Calendar.YEAR, Integer.parseInt(DateStr.substring(0, 4)));
  505. c.set(Calendar.MONTH, Integer.parseInt(DateStr.substring(5, 7).replace("-", "")) - 1);
  506. c.set(Calendar.HOUR_OF_DAY, 0);
  507. c.set(Calendar.MINUTE, 0);
  508. c.set(Calendar.SECOND, 0);
  509. c.set(Calendar.MILLISECOND, 000);
  510. return c.getTime();
  511. }
  512. //endregion
  513. //region 获取某一天的开始时间
  514. public static Date getStartOfDay(Date date) {
  515. Calendar calendar = Calendar.getInstance();
  516. calendar.setTime(date);
  517. int year = calendar.get(Calendar.YEAR);
  518. int month = calendar.get(Calendar.MONTH);
  519. int day = calendar.get(Calendar.DATE);
  520. calendar.set(year, month, day, 0, 0, 0);
  521. calendar.set(Calendar.MILLISECOND, 000);
  522. return calendar.getTime();
  523. }
  524. //endregion
  525. //region 获取某一天的结束时间
  526. public static Date getEndOfDay(Date date) {
  527. Calendar calendar = Calendar.getInstance();
  528. calendar.setTime(date);
  529. int year = calendar.get(Calendar.YEAR);
  530. int month = calendar.get(Calendar.MONTH);
  531. int day = calendar.get(Calendar.DATE);
  532. calendar.setTimeInMillis(0);
  533. calendar.set(year, month, day, 23, 59, 59);
  534. return calendar.getTime();
  535. }
  536. //endregion
  537. public static List<UnitTimeHelpModel> MonthArrayTime(Date StartDate, Date EndDate) {
  538. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化为年月
  539. Calendar StartTime = Calendar.getInstance();
  540. StartTime.setTime(StartDate);
  541. StartTime.set(StartTime.get(Calendar.YEAR), StartTime.get(Calendar.MONTH), StartTime.get(Calendar.DATE));
  542. Calendar EndTime = Calendar.getInstance();
  543. EndTime.setTime(EndDate);
  544. EndTime.set(EndTime.get(Calendar.YEAR), EndTime.get(Calendar.MONTH), EndTime.get(Calendar.DATE));
  545. int total = (EndTime.get(Calendar.YEAR) - StartTime.get(Calendar.YEAR)) * 12 + (EndTime.get(Calendar.MONTH) - StartTime.get(Calendar.MONTH));
  546. List<UnitTimeHelpModel> result = new ArrayList() {
  547. };
  548. for (int i = 0; i <= total; i++) {
  549. StartTime.add(Calendar.MONTH, i == 0 ? 0 : 1);
  550. Date MonthLastDay = getToMonthEndDate(sdf.format(StartTime.getTime()));
  551. UnitTimeHelpModel data = new UnitTimeHelpModel();
  552. data.setStartDate(i == 0 ? StartTime.getTime() : getStartOfDay(getToMonthBeginDate(sdf.format(StartTime.getTime()))));
  553. data.setEndDate(EndTime.getTime().before(MonthLastDay) ? getEndOfDay(EndTime.getTime()) : MonthLastDay);
  554. result.add(data);
  555. }
  556. return result;
  557. }
  558. public static List<ClassSettingDateVo> getDatesBetween(LocalDate startDate, LocalDate endDate) {
  559. List<ClassSettingDateVo> datas = new ArrayList<>();
  560. long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
  561. for (int i = 0; i <= numOfDaysBetween; i++) {
  562. ClassSettingDateVo data = new ClassSettingDateVo();
  563. LocalDate date = startDate.plusDays(i);
  564. // 获取当前日期对应的星期几
  565. DayOfWeek dow = date.getDayOfWeek();
  566. int dayOfWeek = dow.getValue(); // 获取星期几的数字表示,1表示周一,7表示周日
  567. data.setWeekStr(eWeekStatu.stringOf(dayOfWeek));
  568. data.setWeek(dayOfWeek);
  569. data.setDateStr(date.toString());
  570. datas.add(data);
  571. }
  572. return datas;
  573. }
  574. /**
  575. * 获取时间date1与date2相差的秒数
  576. *
  577. * @param date1 起始时间
  578. * @param date2 结束时间
  579. * @return 返回相差的秒数
  580. */
  581. public static int getOffsetSeconds(Date date1, Date date2) {
  582. int seconds = (int) ((date2.getTime() - date1.getTime()) / 1000);
  583. return seconds;
  584. }
  585. /**
  586. * 获取时间date1与date2相差的分钟数
  587. *
  588. * @param date1 起始时间
  589. * @param date2 结束时间
  590. * @return 返回相差的分钟数
  591. */
  592. public static int getOffsetMinutes(Date date1, Date date2) {
  593. return getOffsetSeconds(date1, date2) / 60;
  594. }
  595. /**
  596. * 获取时间date1与date2相差的小时数
  597. *
  598. * @param date1 起始时间
  599. * @param date2 结束时间
  600. * @return 返回相差的小时数
  601. */
  602. public static int getOffsetHours(Date date1, Date date2) {
  603. return getOffsetMinutes(date1, date2) / 60;
  604. }
  605. /**
  606. * 获取时间date1与date2相差的天数数
  607. *
  608. * @param date1 起始时间
  609. * @param date2 结束时间
  610. * @return 返回相差的天数
  611. */
  612. public static int getOffsetDays(Date date1, Date date2) {
  613. return getOffsetHours(date1, date2) / 24+1;
  614. }
  615. public static boolean overlapping(String start1,String end1,String start2,String end2) throws ParseException {
  616. boolean one = CompareDate(start1,end2,"HH:mm");
  617. boolean two = CompareDate(start2,end1,"HH:mm");
  618. return (one && two);
  619. }
  620. public static String getWeek(String sdate) throws ParseException {
  621. // 再转换为时间
  622. Date date = StringToDate(sdate,"yyyy-MM-dd");
  623. Calendar c = Calendar.getInstance();
  624. c.setTime(date);
  625. // int hour=c.get(Calendar.DAY_OF_WEEK);
  626. // hour中存的就是星期几了,其范围 1~7
  627. // 1=星期日 7=星期六,其他类推
  628. return new SimpleDateFormat("EEEE").format(c.getTime());
  629. }
  630. public static void main(String[] args) throws ParseException {
  631. int total = 25;
  632. int pageSize = 20;
  633. int restData =(total + pageSize - 1) / pageSize;
  634. System.out.println("数据:"+restData);
  635. String sdsd ="";
  636. }
  637. }