TimeExchange.java 23 KB

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