TimeExchange.java 25 KB

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