TimeExchange.java 25 KB

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