DateUtil.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. package nckd.pur.scp.common;
  2. import kd.bos.dataentity.entity.DynamicObject;
  3. import kd.bos.dataentity.utils.ObjectUtils;
  4. import kd.bos.entity.EntityMetadataCache;
  5. import kd.bos.exception.ErrorCode;
  6. import kd.bos.exception.KDBizException;
  7. import kd.bos.exception.KDException;
  8. import kd.bos.orm.query.QFilter;
  9. import kd.bos.servicehelper.BusinessDataServiceHelper;
  10. import kd.bos.util.StringUtils;
  11. import java.text.ParseException;
  12. import java.text.SimpleDateFormat;
  13. import java.time.*;
  14. import java.time.format.DateTimeFormatter;
  15. import java.time.temporal.ChronoUnit;
  16. import java.time.temporal.TemporalAdjusters;
  17. import java.util.Calendar;
  18. import java.util.Date;
  19. import java.util.Set;
  20. /**
  21. * 日期工具类
  22. *
  23. * @author xyc
  24. * @date 2021/8/4
  25. */
  26. public final class DateUtil {
  27. public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
  28. public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
  29. public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS_SSS = "yyyyMMddHHmmssSSS";
  30. public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
  31. public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
  32. public static final ZoneId UTC_PLUS_8 = ZoneId.systemDefault();
  33. private DateUtil() {
  34. }
  35. public static boolean isCurrentDateInRange(Date startDate, Date endDate) {
  36. Date currentDate = new Date();
  37. return isDateInRange(currentDate, startDate, endDate);
  38. }
  39. public static boolean isDateInRange(Date targetDate, Date startDate, Date endDate) {
  40. return !targetDate.before(startDate) && !targetDate.after(endDate);
  41. }
  42. /**
  43. * 日期转字符串
  44. *
  45. * @param date 指定日期
  46. * @param pattern 格式
  47. * @return 返回String格式 ( yyyy-MM-dd HH:mm:ss )
  48. */
  49. public static String date2str(Date date, String pattern) {
  50. if (null == date) {
  51. return null;
  52. }
  53. SimpleDateFormat format = null;
  54. if (StringUtils.isNotEmpty(pattern)) {
  55. format = new SimpleDateFormat(pattern);
  56. } else {
  57. format = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
  58. }
  59. return format.format(date);
  60. }
  61. /**
  62. * 获取当前时间字符串
  63. *
  64. * @param pattern 格式
  65. * @return String
  66. */
  67. public static String getCurrentDateTimeStr(String pattern) {
  68. return date2str(new Date(), pattern);
  69. }
  70. /**
  71. * 字符串转日期
  72. *
  73. * @param str 指定日期
  74. * @param pattern 格式
  75. * @return date
  76. */
  77. public static Date string2date(String str, String pattern) {
  78. SimpleDateFormat format = null;
  79. if (StringUtils.isNotEmpty(pattern)) {
  80. format = new SimpleDateFormat(pattern);
  81. } else {
  82. format = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
  83. }
  84. Date date = null;
  85. if (!StringUtils.isEmpty(str)) {
  86. try {
  87. date = format.parse(str);
  88. } catch (ParseException e) {
  89. throw new KDException(new ErrorCode("time convert error", "日期%s转换异常"), str);
  90. }
  91. }
  92. return date;
  93. }
  94. /**
  95. * 将指定日期的时分秒格式那天的 00:00:00 的时间
  96. *
  97. * @param date 指定日期
  98. * @return date
  99. */
  100. public static Date getDateStartTime(Date date) {
  101. if (null == date) {
  102. return null;
  103. }
  104. LocalDate localDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  105. LocalDateTime localDateTime = localDate.atStartOfDay();
  106. return localDateTime2date(localDateTime);
  107. }
  108. /**
  109. * 将指定日期的时分秒格式那天的 23:59:59 的时间
  110. *
  111. * @param date 指定日期
  112. * @return date
  113. */
  114. public static Date getDateEndTime(Date date) {
  115. LocalDate localDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  116. LocalDateTime localDateTime = localDate.atTime(23, 59, 59);
  117. return localDateTime2date(localDateTime);
  118. }
  119. /**
  120. * 获取指定日期当月最后一天的时间
  121. *
  122. * @param date 指定日期
  123. * @return 最后一天的时间 (时分秒:23:59:59)
  124. */
  125. public static Date getTimeEndOfMonth(Date date) {
  126. LocalDate localDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  127. LocalDateTime localDateTime = localDate.atTime(23, 59, 59).with(TemporalAdjusters.lastDayOfMonth());
  128. return localDateTime2date(localDateTime);
  129. }
  130. /**
  131. * 月结束日期
  132. *
  133. * @param date 指定日期
  134. * @return date
  135. */
  136. public static Date getDateEndOfMonth(Date date) {
  137. LocalDate localDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  138. localDate = localDate.withDayOfMonth(localDate.lengthOfMonth());
  139. return Date.from(localDate.atStartOfDay().atZone(UTC_PLUS_8).toInstant());
  140. }
  141. /**
  142. * 获取指定日期当月第一天的时间
  143. *
  144. * @param date 指定日期
  145. * @return 第一天的时间 (时分秒:00:00:00)
  146. */
  147. public static Date getDateStartOfMonth(Date date) {
  148. LocalDate localDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  149. LocalDateTime localDateTime = localDate.atStartOfDay().with(TemporalAdjusters.firstDayOfMonth());
  150. return localDateTime2date(localDateTime);
  151. }
  152. /**
  153. * 周开始日期
  154. *
  155. * @param date 指定日期
  156. * @param isChinaWeek 是否按中国的习惯(一个星期的第一天是星期一)
  157. * @return Date
  158. */
  159. public static Date getDateStartOfWeek(Date date, boolean isChinaWeek) {
  160. LocalDate localDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  161. int days = localDate.getDayOfWeek().getValue() % 7;
  162. if (isChinaWeek) {
  163. days = days - 1;
  164. }
  165. localDate = localDate.minusDays(days);
  166. return Date.from(localDate.atStartOfDay().atZone(UTC_PLUS_8).toInstant());
  167. }
  168. /**
  169. * 周结束日期
  170. *
  171. * @param date 指定日期
  172. * @param isChinaWeek 是否按中国的习惯(一个星期的第一天是星期一)
  173. * @return Date
  174. */
  175. public static Date getDateEndOfWeek(Date date, boolean isChinaWeek) {
  176. LocalDate localDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  177. int days = localDate.getDayOfWeek().getValue() % 7;
  178. if (isChinaWeek) {
  179. days = days - 1;
  180. }
  181. localDate = localDate.plusDays(7 - 1 - days);
  182. return Date.from(localDate.atStartOfDay().atZone(UTC_PLUS_8).toInstant());
  183. }
  184. /**
  185. * 获取指定日期当年第一天的时间
  186. *
  187. * @param date 指定日期
  188. * @return date
  189. */
  190. public static Date getDateStartOfYear(Date date) {
  191. LocalDate localDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  192. localDate = localDate.withMonth(1).withDayOfMonth(1);
  193. return Date.from(localDate.atStartOfDay().atZone(UTC_PLUS_8).toInstant());
  194. }
  195. /**
  196. * 获取指定日期当年最后一天的时间
  197. *
  198. * @param date 指定日期
  199. * @return date
  200. */
  201. public static Date getDateEndOfYear(Date date) {
  202. LocalDate localDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  203. localDate = localDate.withMonth(12);
  204. localDate = localDate.withDayOfMonth(localDate.lengthOfMonth());
  205. return Date.from(localDate.atStartOfDay().atZone(UTC_PLUS_8).toInstant());
  206. }
  207. /**
  208. * 获取年龄
  209. *
  210. * @param birthDate
  211. * @param currentDate
  212. * @return
  213. */
  214. public static int getAge(Date birthDate, Date currentDate) {
  215. if (ObjectUtils.isEmpty(birthDate)) {
  216. throw new KDBizException("生日不能为空");
  217. }
  218. Date date = currentDate;
  219. if (ObjectUtils.isEmpty(date)) {
  220. date = new Date();
  221. }
  222. LocalDate birthLocalDate = date2localDate(birthDate);
  223. LocalDate currentLocalDate = date2localDate(date);
  224. return (int) birthLocalDate.until(currentLocalDate, ChronoUnit.DAYS);
  225. }
  226. /**
  227. * 获取俩个日期间天数,如果入参为空时返回0
  228. *
  229. * @param firstDate 开始时间
  230. * @param secDate 结束时间
  231. * @return 返回俩个日期间天数的绝对值
  232. */
  233. public static int daysBetweenIfBlankReturnZero(Date firstDate, Date secDate) {
  234. if (firstDate == null || secDate == null) {
  235. return 0;
  236. }
  237. return daysBetween(firstDate, secDate);
  238. }
  239. /**
  240. * 获取俩个日期间天数 ( 正整数 )
  241. *
  242. * @param firstDate 开始时间
  243. * @param secDate 结束时间
  244. * @return 返回俩个日期间天数的绝对值
  245. */
  246. public static int daysBetween(Date firstDate, Date secDate) {
  247. LocalDate localDate1 = date2localDate(firstDate);
  248. LocalDate localDate2 = date2localDate(secDate);
  249. return localDate2.isAfter(localDate1) ? (int) localDate1.until(localDate2, ChronoUnit.DAYS) : (int) localDate2.until(localDate1, ChronoUnit.DAYS);
  250. }
  251. /**
  252. * 获得指定时间加减月数后的日期
  253. *
  254. * @param date 指定日期
  255. * @param month 月数,可正可负
  256. * @return 计算后的日期
  257. */
  258. public static Date addMonth(Date date, int month) {
  259. LocalDateTime localDateTime = date2localDateTime(date);
  260. return localDateTime2date(localDateTime.plusMonths(month));
  261. }
  262. /**
  263. * 获得指定时间加减星期后的日期
  264. *
  265. * @param date 指定日期
  266. * @param week 星期,可正可负
  267. * @return 计算后的日期
  268. */
  269. public static Date addWeek(Date date, int week) {
  270. LocalDateTime localDateTime = date2localDateTime(date);
  271. return localDateTime2date(localDateTime.plusWeeks(week));
  272. }
  273. /**
  274. * 获得指定时间第二天的日期
  275. *
  276. * @param date 指定时间
  277. * @return 计算后的日期
  278. */
  279. public static Date getNextDay(Date date) {
  280. LocalDateTime localDateTime = date2localDateTime(date);
  281. return localDateTime2date(localDateTime.plusDays(1L));
  282. }
  283. /**
  284. * 获得指定时间加减天数后的日期
  285. *
  286. * @param date 指定日期
  287. * @param days 天数,可正可负
  288. * @return 计算后的日期
  289. */
  290. public static Date addDay(Date date, int days) {
  291. LocalDateTime localDateTime = date2localDateTime(date);
  292. return localDateTime2date(localDateTime.plusDays(days));
  293. }
  294. /**
  295. * 获得指定时间加减小时后的时间
  296. *
  297. * @param date 指定日期
  298. * @param hours 小时数,可正可负
  299. * @return 计算后的日期
  300. */
  301. public static Date addHour(Date date, int hours) {
  302. LocalDateTime localDateTime = date2localDateTime(date);
  303. return localDateTime2date(localDateTime.plusHours(hours));
  304. }
  305. /**
  306. * 获得指定时间加减小时后的时间
  307. * @param date 指定日期
  308. * @param seconds 秒,可正可负
  309. * @return 计算后的日期
  310. */
  311. public static Date addSecond(Date date, int seconds) {
  312. LocalDateTime localDateTime = date2localDateTime(date);
  313. return localDateTime2date(localDateTime.plusSeconds(seconds));
  314. }
  315. public static String localDateTime2str(LocalDateTime localDateTime, String pattern) {
  316. return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
  317. }
  318. public static Date localDateTime2date(LocalDateTime localDateTime) {
  319. return Date.from(localDateTime.atZone(UTC_PLUS_8).toInstant());
  320. }
  321. public static LocalDateTime date2localDateTime(Date date) {
  322. return date.toInstant().atZone(UTC_PLUS_8).toLocalDateTime();
  323. }
  324. public static LocalDate date2localDate(Date date) {
  325. return date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  326. }
  327. /**
  328. * 获取某个时间月份的天数
  329. *
  330. * @param date 时间
  331. * @return 返回月份的总天数
  332. */
  333. public static int getDaysOfMonth(Date date) {
  334. Calendar calendar = Calendar.getInstance();
  335. calendar.setTime(date);
  336. return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  337. }
  338. /**
  339. * 获取某年某月的天数
  340. *
  341. * @param year 年份
  342. * @param month 月份
  343. * @return 返回月份的总天数
  344. */
  345. public static int getDaysOfMonth(int year, int month) {
  346. Calendar calendar = Calendar.getInstance();
  347. calendar.set(Calendar.MONTH, month - 1);
  348. calendar.set(Calendar.YEAR, year);
  349. return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  350. }
  351. public static boolean isSameDay(Date date1, Date date2) {
  352. LocalDate localDate1 = date2localDate(date1);
  353. LocalDate localDate2 = date2localDate(date2);
  354. return localDate1.equals(localDate2);
  355. }
  356. /**
  357. * <p> 描述 : 调整日期时间</p>
  358. * <p> 备注 : hours 整数为几个小时后,负数几个小时前</p>
  359. *
  360. * @param date
  361. * @param hours
  362. * @return java.util.Date
  363. */
  364. public static Date adjustDateTime(Date date, int hours) {
  365. LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  366. if (hours > 0) {
  367. localDateTime = localDateTime.plusHours(hours);
  368. } else {
  369. localDateTime = localDateTime.minusHours(Math.abs(hours));
  370. }
  371. return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
  372. }
  373. /**
  374. * @return
  375. * @Title 想要获取的日期与传入日期的差值 比如想要获取传入日期前四天的日期 day=-4即可
  376. * @Description
  377. * @author hang
  378. */
  379. public static Date getSomeDay(Date date, int day) {
  380. Calendar calendar = Calendar.getInstance();
  381. calendar.setTime(date);
  382. calendar.add(Calendar.DATE, day);
  383. return getStartOfDay(calendar.getTime());
  384. }
  385. /**
  386. * @return
  387. * @Title 想要获取几个月前后的日期与传入日期的差值 比如想要获取传入日期前四天的日期
  388. * @Description
  389. * @author hang
  390. */
  391. public static Date getMonthDiffDay(Date date, int day) {
  392. Calendar calendar = Calendar.getInstance();
  393. calendar.setTime(date);
  394. calendar.add(Calendar.MONTH, day);
  395. return getStartOfDay(calendar.getTime());
  396. }
  397. /**
  398. * @return
  399. * @Title 想要获取几个月前后的日期与传入日期的差值 比如想要获取传入日期前四天的日期
  400. * @Description
  401. * @author hang
  402. */
  403. public static Date getYearDiffDay(Date date, int day) {
  404. Calendar calendar = Calendar.getInstance();
  405. calendar.setTime(date);
  406. calendar.add(Calendar.YEAR, day);
  407. return getStartOfDay(calendar.getTime());
  408. }
  409. /**
  410. * @param date
  411. * @return
  412. * @description: 获得当天最小时间
  413. * @author: jiang
  414. * @date: 2021年06月21日
  415. */
  416. public static Date getStartOfDay(Date date) {
  417. if( date != null ){
  418. LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()),
  419. ZoneId.systemDefault());
  420. LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
  421. return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
  422. }
  423. return null;
  424. }
  425. /**
  426. * @param date
  427. * @return
  428. * @description: 获得当天最大时间
  429. * @author: Jeff
  430. * @date: 2021年06月21日
  431. */
  432. public static Date getEndOfDay(Date date) {
  433. LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()),
  434. ZoneId.systemDefault());
  435. LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
  436. return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
  437. }
  438. /**
  439. * @return
  440. * @Title 计算两个日期的天数
  441. * @Description
  442. * @author hang
  443. */
  444. public static int getDayDiffer(Date startDate, Date endDate) {
  445. try {
  446. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  447. long startDateTime = dateFormat.parse(dateFormat.format(startDate)).getTime();
  448. long endDateTime = dateFormat.parse(dateFormat.format(endDate)).getTime();
  449. return (int) ((endDateTime - startDateTime) / (1000 * 3600 * 24));
  450. } catch (ParseException e) {
  451. return 0;
  452. }
  453. }
  454. public static Date getMonthFirstDay(Date thisDay, int diff) throws ParseException {
  455. //时间字符串转 LocalDate 类型
  456. LocalDate today = thisDay.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  457. //当前月份+(-diff)
  458. today = today.minusMonths(diff);
  459. today = today.with(TemporalAdjusters.firstDayOfMonth());
  460. SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
  461. ZonedDateTime zonedDateTime = today.atStartOfDay(ZoneId.systemDefault());
  462. String newday = simple.format(Date.from(zonedDateTime.toInstant()));
  463. Date date = simple.parse(newday);
  464. return date;
  465. }
  466. /**
  467. * 获取上个月指定第几天的日期(包括跨年)
  468. *
  469. * @param isEndDay 是否是最后一天 true默认上个月最后一天的日期,dayOfMonth随便传什么,false指定上个月某一天的日期
  470. * @param dayOfMonth 指定要获取的上个月的日期
  471. * @return 获取上个月最后一天的日期(包括跨年)
  472. */
  473. public static Date getLastDayOfLastMonth(boolean isEndDay, int dayOfMonth) {
  474. // 获取当前日期
  475. LocalDate currentDate = LocalDate.now();
  476. // 获取上个月的年份和月份
  477. int lastMonthYear;
  478. int lastMonth;
  479. if (currentDate.getMonthValue() == 1) {
  480. // 如果当前月份是1月,则上个月的年份为去年,月份为12
  481. lastMonthYear = currentDate.getYear() - 1;
  482. lastMonth = 12;
  483. } else {
  484. // 否则上个月的年份和月份分别为当前年份和当前月份减1
  485. lastMonthYear = currentDate.getYear();
  486. lastMonth = currentDate.getMonthValue() - 1;
  487. }
  488. // 构造上个月的YearMonth对象
  489. YearMonth lastYearMonth = YearMonth.of(lastMonthYear, lastMonth);
  490. LocalDate lastDayOfLastMonth;
  491. if (isEndDay) {
  492. // 获取上个月的最后一天
  493. lastDayOfLastMonth = lastYearMonth.atEndOfMonth();
  494. } else {
  495. lastDayOfLastMonth = lastYearMonth.atDay(dayOfMonth);
  496. }
  497. return java.sql.Date.valueOf(lastDayOfLastMonth);
  498. }
  499. /**
  500. * 获取指定日期上个月指定第几天的日期(包括跨年)
  501. *
  502. * @param isEndDay 是否是最后一天 true默认上个月最后一天的日期,dayOfMonth随便传什么,false指定上个月某一天的日期
  503. * @param dayOfMonth 获取指定日期的上个月的日期
  504. * @param date 指定日期
  505. * @return 获取上个月最后一天的日期(包括跨年)
  506. */
  507. public static Date getLastDayOfLastMonth(boolean isEndDay, int dayOfMonth, Date date) {
  508. LocalDate currentDate = date.toInstant().atZone(UTC_PLUS_8).toLocalDate();
  509. // 获取上个月的年份和月份
  510. int lastMonthYear;
  511. int lastMonth;
  512. if (currentDate.getMonthValue() == 1) {
  513. // 如果当前月份是1月,则上个月的年份为去年,月份为12
  514. lastMonthYear = currentDate.getYear() - 1;
  515. lastMonth = 12;
  516. } else {
  517. // 否则上个月的年份和月份分别为当前年份和当前月份减1
  518. lastMonthYear = currentDate.getYear();
  519. lastMonth = currentDate.getMonthValue() - 1;
  520. }
  521. // 构造上个月的YearMonth对象
  522. YearMonth lastYearMonth = YearMonth.of(lastMonthYear, lastMonth);
  523. LocalDate lastDayOfLastMonth;
  524. if (isEndDay) {
  525. // 获取上个月的最后一天
  526. lastDayOfLastMonth = lastYearMonth.atEndOfMonth();
  527. } else {
  528. lastDayOfLastMonth = lastYearMonth.atDay(dayOfMonth);
  529. }
  530. return java.sql.Date.valueOf(lastDayOfLastMonth);
  531. }
  532. /**
  533. * 获取当月某天
  534. *
  535. * @param day
  536. * @return
  537. */
  538. public static Date getDayOfMonth(int day) {
  539. // 获取当前日期
  540. LocalDate currentDate = LocalDate.now();
  541. // 设置日期为当月的第15天
  542. LocalDate fifteenthDayOfMonth = LocalDate.of(currentDate.getYear(), currentDate.getMonth(), day);
  543. return java.sql.Date.valueOf(fifteenthDayOfMonth);
  544. }
  545. /**
  546. * 上个月月份
  547. *
  548. * @return 上个月月份
  549. */
  550. public static int getLastMonth() {
  551. // 获取当前日期
  552. LocalDate currentDate = LocalDate.now();
  553. // 获取上个月的日期
  554. LocalDate lastMonthDate = currentDate.minusMonths(1);
  555. // 获取上个月的月份
  556. int lastMonth = lastMonthDate.getMonthValue();
  557. return lastMonth;
  558. }
  559. /**
  560. * 判断当天是否是多少号
  561. *
  562. * @param mark 号
  563. * @return 判断当天是否是多少号
  564. */
  565. public static Boolean isMark(int mark) {
  566. Calendar calendar = Calendar.getInstance();
  567. // 获取日期的日
  568. int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
  569. // 判断当天是否是多少号
  570. return dayOfMonth == mark;
  571. }
  572. /**
  573. * 指定日期月份第一天
  574. *
  575. * @param date 指定日期
  576. * @return 指定日期月份第一天
  577. */
  578. public static Date getFirstDayOfMonth(Date date) {
  579. Calendar calendar = Calendar.getInstance();
  580. calendar.setTime(date);
  581. calendar.set(Calendar.DAY_OF_MONTH, 1);
  582. calendar.set(Calendar.HOUR_OF_DAY, 0);
  583. calendar.set(Calendar.MINUTE, 0);
  584. calendar.set(Calendar.SECOND, 0);
  585. calendar.set(Calendar.MILLISECOND, 0);
  586. return calendar.getTime();
  587. }
  588. /**
  589. * 指定日期月份最后一天
  590. *
  591. * @param date 指定日期
  592. * @return 指定日期月份最后一天
  593. */
  594. public static Date getLastDayOfMonth(Date date) {
  595. Calendar calendar = Calendar.getInstance();
  596. calendar.setTime(date);
  597. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  598. calendar.set(Calendar.HOUR_OF_DAY, 23);
  599. calendar.set(Calendar.MINUTE, 59);
  600. calendar.set(Calendar.SECOND, 59);
  601. calendar.set(Calendar.MILLISECOND, 999);
  602. return calendar.getTime();
  603. }
  604. /**
  605. * @param yearMonth 年月 2024-01
  606. * @return 获取月份天数
  607. */
  608. public static int getDaysInMonth(String yearMonth) {
  609. YearMonth ym = YearMonth.parse(yearMonth);
  610. Month month = ym.getMonth();
  611. if (month == Month.FEBRUARY && ym.isLeapYear()) {
  612. return 29;
  613. } else {
  614. return month.maxLength();
  615. }
  616. }
  617. public static DynamicObject getPeriodByDate(Date date) {
  618. QFilter filter = new QFilter("begindate", "<=", date).and("enddate", ">=", date);
  619. return BusinessDataServiceHelper.loadSingle("bd_period", filter.toArray());
  620. }
  621. public static String convertTimestamp(long timestamp) {
  622. // 转换为秒
  623. long totalSeconds = timestamp;
  624. // 计算小时
  625. long hours = totalSeconds / 3600;
  626. // 计算分钟
  627. long minutes = (totalSeconds % 3600) / 60;
  628. // 计算秒
  629. long seconds = totalSeconds % 60;
  630. return String.format("%02d:%02d:%02d", hours, minutes, seconds);
  631. }
  632. /**
  633. * 取最大值
  634. * @param d1 d1
  635. * @param d2 d2
  636. * @return 最大值
  637. */
  638. public static Date max(Date d1, Date d2) {
  639. if (d1 == null) {
  640. return d2;
  641. }
  642. if (d2 == null) {
  643. return d1;
  644. }
  645. if (d1.after(d2)) {
  646. return d1;
  647. }else {
  648. return d2;
  649. }
  650. }
  651. /**
  652. * 取最小值
  653. * @param d1 d1
  654. * @param d2 d2
  655. * @return 最小值
  656. */
  657. public static Date min(Date d1, Date d2) {
  658. if (d1 == null) {
  659. return d2;
  660. }
  661. if (d2 == null) {
  662. return d1;
  663. }
  664. if (d1.before(d2)) {
  665. return d1;
  666. }else {
  667. return d2;
  668. }
  669. }
  670. }