DateTimeUtil.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. package com.ruoyi.common.utils;
  2. import org.apache.commons.lang3.StringUtils;
  3. import java.text.SimpleDateFormat;
  4. import java.time.DayOfWeek;
  5. import java.time.Duration;
  6. import java.time.Instant;
  7. import java.time.LocalDate;
  8. import java.time.LocalDateTime;
  9. import java.time.ZoneId;
  10. import java.time.format.DateTimeFormatter;
  11. import java.time.temporal.ChronoUnit;
  12. import java.time.temporal.TemporalAdjusters;
  13. import java.time.temporal.WeekFields;
  14. import java.util.ArrayList;
  15. import java.util.Calendar;
  16. import java.util.Date;
  17. import java.util.List;
  18. /**
  19. * The type Date time util.
  20. *
  21. * @author chen.cheng
  22. */
  23. public class DateTimeUtil {
  24. /**
  25. * Current date time string.
  26. *
  27. * @return the string
  28. * @author chen.cheng
  29. */
  30. public static String currentDateTime() {
  31. return DateTimeUtil.currentDateTime(DateFormatter.yyyy_MM_dd_HHmmss);
  32. }
  33. /**
  34. * Parse date date.
  35. *
  36. * @param dateString the date string
  37. * @return the date
  38. * @author chen.cheng
  39. */
  40. public static Date parseDate(String dateString) {
  41. return DateTimeUtil.parseDate(dateString, DateFormatter.yyyy_MM_dd_HHmmss);
  42. }
  43. /**
  44. * Parse date date.
  45. *
  46. * @param dateString the date string
  47. * @param formatRegex the format regex
  48. * @return the date
  49. * @author chen.cheng
  50. */
  51. public static Date parseDate(String dateString, String formatRegex) {
  52. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegex);
  53. LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
  54. Instant instant = dateTime.atZone(ZoneId.of("GMT+08:00")).toInstant();
  55. return Date.from(instant);
  56. }
  57. /**
  58. * Minus day date.
  59. *
  60. * @param dayNum the day num
  61. * @return the date
  62. * @author chen.cheng
  63. */
  64. public static Date minusDay(Long dayNum) {
  65. LocalDateTime now = LocalDateTime.now();
  66. Instant instant = now.atZone(ZoneId.of("GMT+08:00")).toInstant();
  67. return Date.from(instant);
  68. }
  69. /**
  70. * Parse date string.
  71. *
  72. * @param localDateTime the local date time
  73. * @param formatRegex the format regex
  74. * @return the string
  75. * @author chen.cheng
  76. */
  77. public static String parseDate(LocalDateTime localDateTime, String formatRegex) {
  78. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegex);
  79. return localDateTime.format(formatter);
  80. }
  81. /**
  82. * Parse date string.
  83. *
  84. * @param localDate the local date
  85. * @param formatRegex the format regex
  86. * @return the string
  87. * @author chen.cheng
  88. */
  89. public static String parseDate(LocalDate localDate, String formatRegex) {
  90. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegex);
  91. return localDate.format(formatter);
  92. }
  93. /**
  94. * Current date time string.
  95. *
  96. * @param format the format
  97. * @return the string
  98. * @author chen.cheng
  99. */
  100. public static String currentDateTime(String format) {
  101. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
  102. LocalDateTime now = LocalDateTime.now();
  103. return now.format(formatter);
  104. }
  105. public static Integer getStepIndexFromMills(Integer step, long mills) {
  106. LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(mills), ZoneId.systemDefault());
  107. int hour = localDateTime.getHour();
  108. int second = localDateTime.getSecond();
  109. return hour * 60 * 60 / step + second / step;
  110. }
  111. public static String getDateFromMills(long mills, String formatRegex) {
  112. LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(mills), ZoneId.systemDefault());
  113. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegex);
  114. return localDateTime.format(formatter);
  115. }
  116. public static String getDateFromMills(long mills) {
  117. return getDateFromMills(mills, DateFormatter.yyyy_MM_dd_HHmmss);
  118. }
  119. /**
  120. * Parse local date local date time.
  121. *
  122. * @param dateString the date string
  123. * @param formatRegex the format regex
  124. * @return the local date time
  125. * @author chen.cheng
  126. */
  127. public static LocalDateTime parseLocalDateTime(String dateString, String formatRegex) {
  128. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegex);
  129. LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
  130. return dateTime;
  131. }
  132. public static long timestampMillis() {
  133. LocalDateTime localDateTime = LocalDateTime.now();
  134. // 将 LocalDateTime 转换为 Instant
  135. Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
  136. // 获取毫秒时间戳
  137. return instant.toEpochMilli();
  138. }
  139. public static Long parseDateStringToMills(String dateString, String formatRegex) {
  140. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegex);
  141. LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
  142. return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  143. }
  144. /**
  145. * Parse local date local date.
  146. *
  147. * @param dateString the date string
  148. * @param formatRegex the format regex
  149. * @return the local date
  150. * @author chen.cheng
  151. */
  152. public static LocalDate parseLocalDate(String dateString, String formatRegex) {
  153. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegex);
  154. LocalDate dateTime = LocalDate.parse(dateString, formatter);
  155. return dateTime;
  156. }
  157. /**
  158. * Parse date string string.
  159. *
  160. * @param dateString the date string
  161. * @param formatRegex the format regex
  162. * @return the string
  163. * @author chen.cheng
  164. */
  165. public static String parseDateString(String dateString, String formatRegex) {
  166. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateFormatter.yyyy_MM_dd_HHmmss);
  167. LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
  168. return parseDate(dateTime, formatRegex);
  169. }
  170. /**
  171. * Parse date string string.
  172. *
  173. * @param dateString the date string
  174. * @param srcFormatRegex the src format regex
  175. * @param formatRegex the format regex
  176. * @return the string
  177. * @author chen.cheng
  178. */
  179. public static String parseDateString(String dateString, String srcFormatRegex, String formatRegex) {
  180. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(srcFormatRegex);
  181. LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
  182. return parseDate(dateTime, formatRegex);
  183. }
  184. /**
  185. * Parse short date string string(yyyyMMdd).
  186. *
  187. * @param dateString the date string
  188. * @param srcFormatRegex the src format regex
  189. * @param formatRegex the format regex
  190. * @return the string
  191. * @author chen.cheng
  192. */
  193. public static String parseShortDateString(String dateString, String srcFormatRegex, String formatRegex) {
  194. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(srcFormatRegex);
  195. LocalDate date = LocalDate.parse(dateString, formatter);
  196. return parseDate(date, formatRegex);
  197. }
  198. /**
  199. * Days of range list.
  200. *
  201. * @param startDate the start date
  202. * @param endDate the end date DateTimeUtil.daysOfRange("2020-05-06",
  203. * "2020-09-02",DateFormatter.yyyy_MM_dd,DateFormatter.yyyyMMdd)
  204. * @return the list
  205. * @author chen.cheng
  206. */
  207. public static List<String> daysOfRange(String startDate, String endDate) {
  208. return daysOfRange(startDate, endDate, DateFormatter.yyyy_MM_dd, DateFormatter.yyyy_MM_dd);
  209. }
  210. /**
  211. * Minus month string.
  212. *
  213. * @param monthNum the month num
  214. * @param formatRegx the format regx
  215. * @return the string
  216. * @author chen.cheng
  217. */
  218. public static String minusMonth(Long monthNum, String formatRegx) {
  219. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegx);
  220. LocalDateTime now = LocalDateTime.now();
  221. now = now.minusMonths(monthNum);
  222. return now.format(formatter);
  223. }
  224. /**
  225. * Minus month string
  226. *
  227. * @param monthNum month num
  228. * @param formatRegx format regx
  229. * @return the string
  230. * @author chen.cheng
  231. */
  232. public static String minusMonth(Integer monthNum, String formatRegx) {
  233. return minusMonth(monthNum.longValue(), formatRegx);
  234. }
  235. /**
  236. * Yesterday string.
  237. *
  238. * @param formatRegx the format regx
  239. * @return the string
  240. * @author chen.cheng
  241. */
  242. public static String yesterday(String formatRegx) {
  243. return minusDay(1L, formatRegx);
  244. }
  245. /**
  246. * Minus month string.
  247. *
  248. * @param monthNum the month num
  249. * @param formatRegx the format regx
  250. * @return the string
  251. * @author chen.cheng
  252. */
  253. public static String minusMonth(String date, String formatRegx, Long monthNum) {
  254. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegx);
  255. LocalDateTime localDateTime = parseLocalDateTime(date, formatRegx);
  256. localDateTime = localDateTime.minusMonths(monthNum);
  257. return localDateTime.format(formatter);
  258. }
  259. /**
  260. * Date minus month string
  261. *
  262. * @param date date
  263. * @param formatRegx format regx
  264. * @param monthNum month num
  265. * @return the string
  266. * @author chen.cheng
  267. */
  268. public static String dateMinusMonth(String date, String formatRegx, Long monthNum) {
  269. return dateMinusMonth(date, DateFormatter.yyyyMMdd, formatRegx, monthNum);
  270. }
  271. /**
  272. * Date minus month string
  273. *
  274. * @param date date
  275. * @param srcFormatRegx src format regx
  276. * @param formatRegx format regx
  277. * @param monthNum month num
  278. * @return the string
  279. * @author chen.cheng
  280. */
  281. public static String dateMinusMonth(String date, String srcFormatRegx, String formatRegx, Long monthNum) {
  282. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegx);
  283. LocalDate localDateTime = parseLocalDate(date, srcFormatRegx);
  284. localDateTime = localDateTime.minusMonths(monthNum);
  285. return localDateTime.format(formatter);
  286. }
  287. /**
  288. * Minus day string.
  289. *
  290. * @param dayNum the day num
  291. * @param formatRegx the format regx
  292. * @return the string
  293. * @author chen.cheng
  294. */
  295. public static String minusDay(Long dayNum, String formatRegx) {
  296. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatRegx);
  297. LocalDateTime now = LocalDateTime.now();
  298. now = now.minusDays(dayNum);
  299. return now.format(formatter);
  300. }
  301. /**
  302. * Days of range list.
  303. *
  304. * @param startDate the start date
  305. * @param endDate the end date
  306. * @param paramFormat the param format
  307. * @param resultFormat the result format
  308. * @return the list
  309. * @author chen.cheng
  310. */
  311. public static List<String> daysOfRange(String startDate, String endDate, String paramFormat, String resultFormat) {
  312. DateTimeFormatter resultFormatter = DateTimeFormatter.ofPattern(resultFormat);
  313. DateTimeFormatter paramFormatter = DateTimeFormatter.ofPattern(paramFormat);
  314. LocalDate dateTimeOfStart = LocalDate.parse(startDate, paramFormatter);
  315. LocalDate dateTimeOfEnd = LocalDate.parse(endDate, paramFormatter);
  316. List<String> days = new ArrayList<>();
  317. while (!dateTimeOfStart.isAfter(dateTimeOfEnd)) {
  318. days.add(dateTimeOfStart.format(resultFormatter));
  319. dateTimeOfStart = dateTimeOfStart.plusDays(1L);
  320. }
  321. return days;
  322. }
  323. public static List<String> monthOfRange(String startDate, String endDate, String paramFormat, String resultFormat) {
  324. DateTimeFormatter resultFormatter = DateTimeFormatter.ofPattern(resultFormat);
  325. DateTimeFormatter paramFormatter = DateTimeFormatter.ofPattern(paramFormat);
  326. LocalDate dateTimeOfStart = LocalDate.parse(startDate, paramFormatter);
  327. LocalDate dateTimeOfEnd = LocalDate.parse(endDate, paramFormatter);
  328. List<String> month = new ArrayList<>();
  329. while (!dateTimeOfStart.isAfter(dateTimeOfEnd)) {
  330. month.add(dateTimeOfStart.format(resultFormatter));
  331. dateTimeOfStart = dateTimeOfStart.plusMonths(1L);
  332. }
  333. DateTimeFormatter ddFormatter = DateTimeFormatter.ofPattern(DateFormatter.DD);
  334. int startDd = Integer.parseInt(dateTimeOfStart.format(ddFormatter));
  335. int endDd = Integer.parseInt(dateTimeOfEnd.format(ddFormatter));
  336. if (startDd > endDd) {
  337. month.add(dateTimeOfEnd.format(resultFormatter));
  338. }
  339. return month;
  340. }
  341. /**
  342. * Days of range list.
  343. *
  344. * @param startDate the start date
  345. * @param endDate the end date
  346. * @param paramFormat the param format
  347. * @return the list
  348. * @author chen.cheng
  349. */
  350. public static List<String> daysOfRange(String startDate, String endDate, String paramFormat) {
  351. DateTimeFormatter resultFormatter = DateTimeFormatter.ofPattern(paramFormat);
  352. DateTimeFormatter paramFormatter = DateTimeFormatter.ofPattern(paramFormat);
  353. LocalDate dateTimeOfStart = LocalDate.parse(startDate, paramFormatter);
  354. LocalDate dateTimeOfEnd = LocalDate.parse(endDate, paramFormatter);
  355. List<String> days = new ArrayList<>();
  356. while (!dateTimeOfStart.isAfter(dateTimeOfEnd)) {
  357. days.add(dateTimeOfStart.format(resultFormatter));
  358. dateTimeOfStart = dateTimeOfStart.plusDays(1L);
  359. }
  360. return days;
  361. }
  362. /**
  363. * Gets time step of time index.
  364. *
  365. * @param tp the tp
  366. * @param gap the gap (now + gap minutes)
  367. * @return the time step of time index
  368. * @author chen.cheng
  369. */
  370. public static Integer getTimeStepOfTimeIndex(Integer tp, Integer gap) {
  371. return getTimeStepIndexOfNow(tp, LocalDateTime.now().plus(gap, ChronoUnit.MINUTES));
  372. }
  373. /**
  374. * Get time step index of now integer.
  375. *
  376. * @param tp the tp
  377. * @param toDateTime the to date time
  378. * @return the integer
  379. * @author chen.cheng
  380. */
  381. public static int getTimeStepIndexOfNow(Integer tp, LocalDateTime toDateTime) {
  382. LocalDateTime zero = LocalDateTime.of(toDateTime.getYear(), toDateTime.getMonth(), toDateTime.getDayOfMonth(), 0, 0, 0);
  383. Duration duration = Duration.between(zero, toDateTime);
  384. double minutes = duration.toMinutes();
  385. return (int) Math.floor(minutes / tp);
  386. }
  387. /**
  388. * Gets time from step index. tp is min the format hh:mm:00
  389. *
  390. * @param stepIndex the step index
  391. * @param tp the tp
  392. * @return the time from step index
  393. * @author chen.cheng
  394. */
  395. public static String getTimeFromStepIndex(Integer stepIndex, Integer tp) {
  396. Integer stepIndex2Min = stepIndex * tp;
  397. String hour = StringUtils.leftPad(String.valueOf(stepIndex2Min / 60), 2, "0");
  398. String min = StringUtils.leftPad(String.valueOf(stepIndex2Min % 60), 2, "0");
  399. return String.format("%s:%s:%s", hour, min, "00");
  400. }
  401. /**
  402. * Gets time from step index with out second.
  403. *
  404. * @param stepIndex the step index
  405. * @param tp the tp
  406. * @return the time from step index with out second
  407. * @author chen.cheng
  408. */
  409. public static String getTimeFromStepIndexWithOutSecond(Integer stepIndex, Integer tp) {
  410. Integer stepIndex2Min = stepIndex * tp;
  411. String hour = StringUtils.leftPad(String.valueOf(stepIndex2Min / 60), 2, "0");
  412. String min = StringUtils.leftPad(String.valueOf(stepIndex2Min % 60), 2, "0");
  413. return String.format("%s:%s", hour, min);
  414. }
  415. /**
  416. * Gets day of week.
  417. *
  418. * @param date the date
  419. * @param paramFormatter the param formatter
  420. * @return the day of week
  421. * @author chen.cheng
  422. */
  423. public static Integer getDayOfWeek(String date, String paramFormatter) {
  424. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(paramFormatter);
  425. LocalDate l_da1 = LocalDate.parse(date, formatter);
  426. DayOfWeek dayOfWeek = l_da1.getDayOfWeek();
  427. return dayOfWeek.getValue();
  428. }
  429. /**
  430. * Gets ali day of week.
  431. *
  432. * @param date the date
  433. * @return the ali day of week
  434. * @author chen.cheng
  435. */
  436. public static Integer getAliDayOfWeek(String date) {
  437. return getDayOfWeek(date, DateFormatter.yyyyMMdd) - 1;
  438. }
  439. /**
  440. * Cal week of year integer.
  441. *
  442. * @return the integer
  443. * @author chen.cheng
  444. */
  445. public static Integer calWeekOfYear() {
  446. return calWeekOfYear(currentDateTime(DateFormatter.yyyy_MM_dd));
  447. }
  448. public static Integer calLastWeekOfYear() {
  449. return calWeekOfYear(currentDateTime(DateFormatter.yyyy_MM_dd)) - 1;
  450. }
  451. /**
  452. * Cal week of year integer.
  453. *
  454. * @param date the date yyyy-MM-dd
  455. * @return the integer
  456. * @author chen.cheng
  457. */
  458. public static Integer calWeekOfYear(String date) {
  459. LocalDate localDate = LocalDate.parse(date);
  460. // 第一个参数:一周的第一天,不能为空
  461. // 第二个参数:第一周的最小天数,从1到7
  462. WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1);
  463. return localDate.get(weekFields.weekOfYear());
  464. }
  465. /**
  466. * Cal week of year integer.
  467. *
  468. * @param date the date
  469. * @param paramFormatter the param formatter
  470. * @return the integer
  471. * @author chen.cheng
  472. */
  473. public static Integer calWeekOfYear(String date, String paramFormatter) {
  474. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(paramFormatter);
  475. LocalDate localDate = LocalDate.parse(date, formatter);
  476. return calWeekOfYear(localDate.toString());
  477. }
  478. /**
  479. * Get week time section string [ ].
  480. *
  481. * @param weekIndex the week index
  482. * @return the string [ ]
  483. * @author chen.cheng
  484. */
  485. public static String[] getWeekTimeSection(int weekIndex) {
  486. return getWeekTimeSection(weekIndex, DateFormatter.yyyyMMdd);
  487. }
  488. /**
  489. * Get week time section string [ ]. 获取前几周内的日期范围
  490. *
  491. * @param weekIndex the week index
  492. * @return the string [ ]
  493. * @author chen.cheng
  494. */
  495. public static String[] getWeekTimeSection(int weekIndex, String dateFormat) {
  496. // weekIndex为前几周的周数,如:获取当前时间前第二周的时间 weekIndex=2
  497. // 获取本周第一天
  498. Calendar cal = Calendar.getInstance();
  499. cal.add(Calendar.WEEK_OF_MONTH, 0);
  500. cal.set(Calendar.DAY_OF_WEEK, 2);
  501. Date first = cal.getTime();
  502. Date last = cal.getTime();
  503. String firstString = new SimpleDateFormat("yyyy-MM-dd").format(first) + " 00:00:00";
  504. String lastString = new SimpleDateFormat("yyyy-MM-dd").format(last) + " 23:59:59";
  505. DateTimeFormatter formatPatten = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  506. LocalDateTime firstTime = LocalDateTime.parse(firstString, formatPatten);
  507. LocalDateTime lastTime = LocalDateTime.parse(lastString, formatPatten);
  508. DateTimeFormatter resultDateFormat = DateTimeFormatter.ofPattern(dateFormat);
  509. // 开始时间
  510. firstTime = firstTime.plusDays(-(weekIndex * 7L));
  511. // 结束时间
  512. lastTime = lastTime.plusDays(-1);
  513. return new String[]{firstTime.format(resultDateFormat), lastTime.format(resultDateFormat)};
  514. }
  515. public static String getFirstDayOfRecentYear(String dateFormat) {
  516. // 获取当前日期
  517. LocalDate today = LocalDate.now();
  518. // 计算近一年的年份
  519. int recentYear = today.getYear() - 1;
  520. LocalDate localDate = LocalDate.of(recentYear, today.getMonth(), 1);
  521. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
  522. return localDate.format(formatter);
  523. }
  524. public static String getFirstDayOfThisYear(String dateFormat) {
  525. // 获取当前日期
  526. LocalDate today = LocalDate.now();
  527. // 计算近一年的年份
  528. int recentYear = today.getYear();
  529. // 使用 TemporalAdjusters 来找到近一年的第一天
  530. LocalDate localDate = LocalDate.of(recentYear, 1, 1).with(TemporalAdjusters.firstDayOfYear());
  531. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
  532. return localDate.format(formatter);
  533. }
  534. public static String getFirstDayOfRecentYear() {
  535. return getFirstDayOfRecentYear(DateFormatter.yyyy_MM_dd);
  536. }
  537. public static String getFirstDayOfRecentMonth(String dateFormat) {
  538. // 获取当前日期
  539. LocalDate today = LocalDate.now();
  540. // 使用 TemporalAdjusters 来找到近一个月的第一天
  541. LocalDate with = today.minusMonths(1);
  542. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
  543. return with.format(formatter);
  544. }
  545. public static String getFirstDayOfRecentMonth() {
  546. return getFirstDayOfRecentMonth(DateFormatter.yyyy_MM_dd);
  547. }
  548. public static void main(String[] args) {
  549. System.out.println(DateTimeUtil.getStepIndexFromMills(10, System.currentTimeMillis()));
  550. }
  551. /**
  552. * The interface Date formatter.
  553. *
  554. * @author chen.cheng
  555. */
  556. public interface DateFormatter {
  557. /**
  558. * The constant yyyy_MM_dd_HHmmss.
  559. *
  560. * @author chen.cheng
  561. */
  562. String yyyy_MM_dd_HHmmss = "yyyy-MM-dd HH:mm:ss";
  563. /**
  564. * The constant yyyyMMddHHmmss.
  565. *
  566. * @author chen.cheng
  567. */
  568. String yyyyMMddHHmmss = "yyyyMMddHHmmss";
  569. String yyyyMMddHHmmssSSS= "yyyy-MM-dd HH:mm:ss.SSS";
  570. /**
  571. * The constant yyyy_MM_dd.
  572. *
  573. * @author chen.cheng
  574. */
  575. String yyyy_MM_dd = "yyyy-MM-dd";
  576. String DD = "dd";
  577. /**
  578. * The constant yyyyMMdd.
  579. *
  580. * @author chen.cheng
  581. */
  582. String yyyyMMdd = "yyyyMMdd";
  583. String yyyy_MM = "yyyy-MM";
  584. /**
  585. * The constant yyyyMM.
  586. *
  587. * @author chen.cheng
  588. */
  589. String yyyyMM = "yyyyMM";
  590. /**
  591. * The constant yyyy.
  592. *
  593. * @author chen.cheng
  594. */
  595. String yyyy = "yyyy";
  596. /**
  597. * To string string.
  598. *
  599. * @return the string
  600. * @author chen.cheng
  601. */
  602. @Override
  603. String toString();
  604. }
  605. }