TimeExchange.java 23 KB

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