Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.13 KB | None | 0 0
  1. package com.whirly.utils;
  2.  
  3. import org.joda.time.DateTime;
  4.  
  5. import java.text.DateFormat;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.*;
  9.  
  10.  
  11. public class DateUtil {
  12.  
  13. /**
  14. * The Constant CM_LONG_DATE_FORMAT.
  15. */
  16. public static final String CM_LONG_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
  17.  
  18. /**
  19. * The Constant CM_SHORT_DATE_FORMAT.
  20. */
  21. public static final String CM_SHORT_DATE_FORMAT = "yyyyMMdd";
  22. public static final String CM_DATE_FORMAT = "yyyy-MM-dd";
  23.  
  24. /**
  25. * The Constant CM_SHORT_MONTH_FORMAT.
  26. */
  27. public static final String CM_SHORT_MONTH_FORMAT = "yyyy-MM";
  28.  
  29. /**
  30. * The Constant CM_SHORT_YEAR_FORMAT.
  31. */
  32. public static final String CM_SHORT_YEAR_FORMAT = "yyyy";
  33.  
  34. /**
  35. * The Constant YEAR_MONTH.
  36. */
  37. public static final String YEAR_MONTH = "yyyyMM";
  38.  
  39. /**
  40. * The Constant MONTH.
  41. */
  42. public final static String[] MONTH = {"January", "February", "March", "April", "May", "June", "July", "August", "September",
  43. "October", "November", "December"};
  44.  
  45. /**
  46. * The Constant DAY.
  47. */
  48. public final static String[] DAY = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  49.  
  50. /**
  51. * The date format.
  52. */
  53. public static DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
  54.  
  55. /**
  56. * 取两个时间相隔分钟数.
  57. *
  58. * @return the today
  59. */
  60. public static int getMinutesBetweenDates(Date startDate, Date endDate) {
  61. long between = (endDate.getTime() - startDate.getTime()) / 1000;
  62. long min = between / 60;
  63. return (int) min;
  64. }
  65.  
  66. /**
  67. * 取得今天的日期.
  68. *
  69. * @return the today
  70. */
  71. public static String getToday() {
  72. Date myDate = new Date();
  73. String today = DateUtil.DateToString(myDate, CM_SHORT_DATE_FORMAT);
  74. return today;
  75. }
  76.  
  77. /**
  78. * 取得今天的日期.
  79. *
  80. * @return timeformat
  81. */
  82. public static long getTodayInTimeFormat() {
  83. Date myDate = new Date();
  84. long today = myDate.getTime();
  85. return today;
  86. }
  87.  
  88. /**
  89. * 取得今年年份.
  90. *
  91. * @return the now year
  92. */
  93. public static String getNowYear() {
  94. Date myDate = new Date();
  95. String nowYear = DateUtil.DateToString(myDate, CM_SHORT_YEAR_FORMAT);
  96. return nowYear;
  97. }
  98.  
  99. public static String getNowMonth() {
  100. Calendar c = Calendar.getInstance();
  101. c.setTime(new Date());
  102. int result = c.get(Calendar.MONTH) + 1;
  103. if (result < 10) {
  104. return "0" + result;
  105. } else {
  106. return "" + result;
  107. }
  108.  
  109. }
  110.  
  111.  
  112. /**
  113. * 获得当前时间.
  114. *
  115. * @return String
  116. */
  117. public static java.sql.Timestamp getNowTime() {
  118. return new java.sql.Timestamp(System.currentTimeMillis());
  119. }
  120.  
  121. /**
  122. * 取得当月的月份.
  123. *
  124. * @return the month
  125. */
  126. public static String getMonth() {
  127. Date myDate = new Date();
  128. String month = DateUtil.DateToString(myDate, CM_SHORT_MONTH_FORMAT);
  129. return month;
  130. }
  131.  
  132. /**
  133. * 取得当月的年月.
  134. *
  135. * @param ymFormat the ym format
  136. * @return the month
  137. */
  138. public static String getMonth(String ymFormat) {
  139. Date myDate = new Date();
  140. String month = DateUtil.DateToString(myDate, ymFormat);
  141. return month;
  142. }
  143.  
  144. /**
  145. * 取得下月的月份,形式如yyyy-MM.
  146. *
  147. * @return the next month
  148. */
  149. public static String getNextMonth() {
  150. Date myDate = new Date();
  151. Calendar cal = Calendar.getInstance();
  152. cal.setTime(myDate);
  153. cal.add(Calendar.MONTH, 1);
  154.  
  155. String nextmonth = DateUtil.DateToString(cal.getTime(), CM_SHORT_MONTH_FORMAT);
  156. return nextmonth;
  157. }
  158.  
  159. /**
  160. * 取得下月的月份,形式如y.
  161. *
  162. * @param ymFormat 格式如:yyyyMM
  163. * @return the next month
  164. */
  165. public static String getNextMonth(String ymFormat) {
  166. Date myDate = new Date();
  167. Calendar cal = Calendar.getInstance();
  168. cal.setTime(myDate);
  169. cal.add(Calendar.MONTH, 1);
  170.  
  171. String nextmonth = DateUtil.DateToString(cal.getTime(), ymFormat);
  172. return nextmonth;
  173. }
  174.  
  175.  
  176. /**
  177. * 取得上月的月份.
  178. *
  179. * @return the up month
  180. */
  181. public static String getUpMonth() {
  182. Date myDate = new Date();
  183. Calendar cal = Calendar.getInstance();
  184. cal.setTime(myDate);
  185. cal.add(Calendar.MONTH, -1);
  186.  
  187. String nextmonth = DateUtil.DateToString(cal.getTime(), CM_SHORT_MONTH_FORMAT);
  188. return nextmonth;
  189. }
  190.  
  191. /**
  192. * 取得参数指定年月的上个月.
  193. *
  194. * @param year 指定年
  195. * @param month 指定月
  196. * @param format 指定格式 如: "yyyyMMdd"
  197. * @return the up month
  198. */
  199. public static String getUpMonth(String year, String month, String format) {
  200. Date myDate = DateUtil.getDate(year, month, "01");
  201. Calendar cal = Calendar.getInstance();
  202. cal.setTime(myDate);
  203. cal.add(Calendar.MONTH, -1);
  204.  
  205. String nextmonth = DateUtil.DateToString(cal.getTime(), format);
  206. return nextmonth;
  207. }
  208.  
  209. /**
  210. * 取得参数指定年月的下个月.
  211. *
  212. * @param year 指定年
  213. * @param month 指定月
  214. * @param format 指定格式 如: "yyyyMMdd"
  215. * @return the next month
  216. */
  217. public static String getNextMonth(String year, String month, String format) {
  218. Date myDate = DateUtil.getDate(year, month, "01");
  219. Calendar cal = Calendar.getInstance();
  220. cal.setTime(myDate);
  221. cal.add(Calendar.MONTH, 1);
  222.  
  223. String nextmonth = DateUtil.DateToString(cal.getTime(), format);
  224. return nextmonth;
  225. }
  226.  
  227. /**
  228. * 取得从某一时间开始的一段年份.
  229. *
  230. * @param currdate Date
  231. * @param len int
  232. * @return List
  233. */
  234. public static List<String> getPeroidYear(Date currdate, int len) {
  235. List<String> lists = new ArrayList<String>();
  236. Calendar cal = Calendar.getInstance();
  237. int ln = Math.abs(len);
  238. if (len >= 0) {
  239. for (int i = 0; i < len; i++) {
  240. cal.setTime(currdate);
  241. cal.add(Calendar.YEAR, i);
  242.  
  243. String year = DateUtil.DateToString(cal.getTime(), CM_SHORT_YEAR_FORMAT);
  244. lists.add(year);
  245. }
  246. } else {
  247. for (int i = 1; i <= ln; i++) {
  248. cal.setTime(currdate);
  249. cal.add(Calendar.YEAR, -i);
  250. String year = DateUtil.DateToString(cal.getTime(), CM_SHORT_YEAR_FORMAT);
  251. lists.add(year);
  252. }
  253.  
  254. }
  255. return lists;
  256. }
  257.  
  258. /**
  259. * 取得明日的日期.
  260. *
  261. * @return the tomorrow
  262. */
  263. public static String getTomorrow() {
  264. Date myDate = new Date();
  265. Calendar cal = Calendar.getInstance();
  266. cal.setTime(myDate);
  267. cal.add(Calendar.DATE, 1);
  268. String tomorrow = DateUtil.DateToString(cal.getTime(), CM_SHORT_DATE_FORMAT);
  269. return tomorrow;
  270. }
  271.  
  272. /**
  273. * 取得昨日的日期.
  274. *
  275. * @return the yesterday
  276. */
  277. public static String getYesterday() {
  278. Date myDate = new Date();
  279. Calendar cal = Calendar.getInstance();
  280. cal.setTime(myDate);
  281. cal.add(Calendar.DATE, -1);
  282. String yesterday = DateUtil.DateToString(cal.getTime(), CM_SHORT_DATE_FORMAT);
  283. return yesterday;
  284. }
  285.  
  286. /**
  287. * 取得日期的完整打印格式.
  288. *
  289. * @param date the date
  290. * @return the full date string
  291. */
  292. public static String getFullDateString(String date) {
  293. Date myDate = DateUtil.StringToDate(date);
  294. return dateFormat.format(myDate);
  295. }
  296.  
  297. /**
  298. * 日期变为字符串.
  299. *
  300. * @param date the date
  301. * @param iso the iso
  302. * @return the string
  303. */
  304. public static String DateToString(Date date, String iso) {
  305. SimpleDateFormat format = new SimpleDateFormat(iso);
  306. return format.format(date);
  307. }
  308.  
  309. /**
  310. * 字符串变为日期.
  311. *
  312. * @param date the date
  313. * @return the date
  314. */
  315. public static Date StringToDate(String date) {
  316. Date myDate = new Date();
  317. SimpleDateFormat format = new SimpleDateFormat(DateUtil.CM_SHORT_DATE_FORMAT);
  318. try {
  319. myDate = format.parse(date);
  320. } catch (ParseException e) {
  321. e.printStackTrace();
  322. }
  323. return myDate;
  324. }
  325.  
  326. /**
  327. * 字符串变为日期.
  328. *
  329. * @param date the date
  330. * @param formatStr the format str
  331. * @return the date
  332. */
  333. public static Date StringToDate(String date, String formatStr) {
  334. Date myDate = new Date();
  335. SimpleDateFormat format = new SimpleDateFormat(formatStr);
  336. try {
  337. myDate = format.parse(date);
  338. } catch (ParseException e) {
  339. e.printStackTrace();
  340. }
  341. return myDate;
  342. }
  343.  
  344. /**
  345. * 根据起始日期 及 间隔时间 得到结束日期.
  346. *
  347. * @param startDate 起始日期
  348. * @param offset 间隔时间
  349. * @return 结束日期
  350. */
  351. public static String getEndDate(String startDate, int offset) {
  352. Calendar cal = Calendar.getInstance();
  353. Date day = DateUtil.StringToDate(startDate);
  354. cal.setTime(day);
  355. cal.add(Calendar.DATE, offset);
  356.  
  357. return DateUtil.DateToString(cal.getTime(), CM_SHORT_DATE_FORMAT);
  358. }
  359.  
  360. /**
  361. * 根据起始日期 及 间隔时间 得到结束日期 得到的格式是yyyy-MM-dd.
  362. *
  363. * @param startDate 起始日期
  364. * @param offset 间隔时间
  365. * @return 结束日期
  366. */
  367.  
  368. public static String getEndDateForSQLDate(String startDate, int offset) {
  369. Calendar cal = Calendar.getInstance();
  370. Date day = DateUtil.StringToDateByFormat(startDate, CM_SHORT_DATE_FORMAT);
  371. cal.setTime(day);
  372. cal.add(Calendar.DATE, offset);
  373.  
  374. return DateUtil.DateToString(cal.getTime(), CM_SHORT_DATE_FORMAT);
  375. }
  376.  
  377. /**
  378. * 把指定格式的字符串变为日期型.
  379. *
  380. * @param date the date
  381. * @param iso the iso
  382. * @return the date
  383. */
  384. public static Date StringToDateByFormat(String date, String iso) {
  385. Date myDate = new Date();
  386. SimpleDateFormat format = new SimpleDateFormat(iso);
  387. try {
  388. myDate = format.parse(date);
  389. } catch (ParseException e) {
  390. e.printStackTrace();
  391. }
  392. return myDate;
  393. }
  394.  
  395. /**
  396. * 取得指定年月所有星期五的日期的集合.
  397. *
  398. * @param year 指定年
  399. * @param month 指定月
  400. * @return list 星期五的日期的集合
  401. */
  402. public static List<String> getEndWeekDayOfMonth(String year, String month) {
  403. List<String> list = new ArrayList<String>();
  404. int days = daysInMonth(year, month);
  405. int weekday = 0;
  406. for (int i = 1; i <= days; i++) {
  407. weekday = getWeekOfMonth(year, month, String.valueOf(i));
  408. if (weekday == 5) {
  409. if (i < 10) {
  410. list.add(year + month + "0" + String.valueOf(i));
  411. } else {
  412. list.add(year + month + String.valueOf(i));
  413. }
  414. }
  415. }
  416. return list;
  417. }
  418.  
  419. /**
  420. * 获得指定年月的天数.
  421. *
  422. * @param argYear the arg year
  423. * @param argMonth the arg month
  424. * @return int 天数
  425. */
  426. public static int daysInMonth(String argYear, String argMonth) {
  427. int year = Integer.parseInt(argYear);
  428. int month = Integer.parseInt(argMonth);
  429.  
  430. GregorianCalendar c = new GregorianCalendar(year, month, 0);
  431. int[] daysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  432. daysInMonths[1] += c.isLeapYear(c.get(GregorianCalendar.YEAR)) ? 1 : 0;
  433. return daysInMonths[c.get(GregorianCalendar.MONTH)];
  434. }
  435.  
  436. /**
  437. * 得到日期中是星期几.
  438. *
  439. * @param year String
  440. * @param month String
  441. * @param day String
  442. * @return String
  443. */
  444. public static int getWeekOfMonth(String year, String month, String day) {
  445. Date myDate = getDate(year, month, day);
  446. int index = 0;
  447. try {
  448. Calendar cal = Calendar.getInstance();
  449. cal.setTime(myDate);
  450. index = cal.get(Calendar.DAY_OF_WEEK);
  451. if (index <= 1) {
  452. index = 7;
  453. } else {
  454. index = index - 1;
  455. }
  456. } catch (Exception e) {
  457. e.printStackTrace();
  458. }
  459. return index;
  460.  
  461. }
  462.  
  463. /**
  464. * 根据年月日得到日期.
  465. *
  466. * @param year String 年 YYYY
  467. * @param month String 月MM
  468. * @param day String 日dd
  469. * @return Date
  470. */
  471. public static Date getDate(String year, String month, String day) {
  472. Date result = null;
  473. try {
  474. String str = year + (month.length() == 1 ? "0" + month : month) + (day.length() == 1 ? "0" + day : day);
  475. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  476. result = dateFormat.parse(str);
  477. } catch (Exception e) {
  478. System.out.println("Exception " + e);
  479. }
  480. return result;
  481. }
  482.  
  483. /**
  484. * 取得当前月的第一天.
  485. *
  486. * @return string format
  487. */
  488. public static String getFirstDayOfMonth() {
  489. StringBuffer buff = new StringBuffer();
  490. String today = DateUtil.getToday();
  491. buff.append(today.substring(0, 6)).append("01");
  492. return buff.toString();
  493. }
  494.  
  495. /**
  496. * 取得当前月的第一天.
  497. *
  498. * @return long format
  499. */
  500. public static long getFirstDayOfMonthInTimeFormat() {
  501. StringBuffer buff = new StringBuffer();
  502. String today = DateUtil.getToday();
  503. buff.append(today.substring(0, 6)).append("01");
  504. long time = (DateUtil.StringToDateByFormat(buff.toString(), "yyyyMMdd")).getTime();
  505.  
  506. return time;
  507. }
  508.  
  509. /**
  510. * 取得距离当前月月底的n个月的第一天.
  511. *
  512. * @param offSet the off set
  513. * @return the first day of offset month
  514. */
  515. public static String getFirstDayOfOffsetMonth(int offSet) {
  516. StringBuffer buff = new StringBuffer();
  517. Date myDate = new Date();
  518. Calendar cal = Calendar.getInstance();
  519. cal.setTime(myDate);
  520. cal.add(Calendar.MONTH, offSet);
  521.  
  522. String nextmonth = DateUtil.DateToString(cal.getTime(), YEAR_MONTH);
  523. buff.append(nextmonth).append("01");
  524.  
  525. return buff.toString();
  526. }
  527.  
  528. /**
  529. * 把字符型时间转化成长整型时间.
  530. *
  531. * @param argStr the arg str
  532. * @return the long
  533. */
  534. public static long StingToLong(String argStr) {
  535. return (DateUtil.StringToDateByFormat(argStr, DateUtil.CM_SHORT_DATE_FORMAT)).getTime();
  536. }
  537.  
  538. /**
  539. * 按日期格式formatStr,把字符型时间转化成长整型时间.
  540. *
  541. * @param argStr the arg str
  542. * @param formatStr the format str
  543. * @return the long
  544. */
  545. public static long StingToLong(String argStr, String formatStr) {
  546. return (DateUtil.StringToDateByFormat(argStr, formatStr)).getTime();
  547. }
  548.  
  549. /**
  550. * 取得参数日期上个月的最后一天.
  551. *
  552. * @param argDate the arg date
  553. * @return endDateOfUpMonth 格式如"yyyyMMdd"
  554. */
  555. public static String getEndDateOfUpMonth(Date argDate) {
  556. StringBuffer buff = new StringBuffer();
  557. // java.utils.Date --> String
  558. String date = DateUtil.DateToString(argDate, DateUtil.CM_SHORT_DATE_FORMAT);
  559. // 得到参数日期的上个年月
  560. String upMonth = DateUtil.getUpMonth(date.substring(0, 4), date.substring(4, 6), DateUtil.YEAR_MONTH);
  561. // 上个月的最后一天 = 上个月(yyyyMM) + 上个月总天数
  562. buff.append(upMonth).append(DateUtil.daysInMonth(upMonth.substring(0, 4), upMonth.substring(4)));
  563. date = buff.toString();
  564. buff = null;
  565. return date;
  566. }
  567.  
  568. /**
  569. * 取得参数日期下个月的最后一天.
  570. *
  571. * @param argDate the arg date
  572. * @return endDateOfUpMonth 格式如"yyyyMMdd"
  573. */
  574. public static String getEndDateOfNextMonth(Date argDate) {
  575. StringBuffer buff = new StringBuffer();
  576. // java.utils.Date --> String
  577. String date = DateUtil.DateToString(argDate, DateUtil.CM_SHORT_DATE_FORMAT);
  578. // 得到参数日期的上个年月
  579. String nextMonth = DateUtil.getNextMonth(date.substring(0, 4), date.substring(4, 6), DateUtil.YEAR_MONTH);
  580. // 上个月的最后一天 = 上个月(yyyyMM) + 上个月总天数
  581. buff.append(nextMonth).append(DateUtil.daysInMonth(nextMonth.substring(0, 4), nextMonth.substring(4)));
  582. date = buff.toString();
  583. buff = null;
  584. return date;
  585. }
  586.  
  587. /**
  588. * Adds the specified (signed) amount of time to the given time field, based
  589. * on the calendar's rules.
  590. *
  591. * @param date the date
  592. * @param field the field
  593. * @param amount the amount
  594. * @return the date
  595. */
  596. public static Date add(Date date, int field, long amount) {
  597. Calendar calendar = Calendar.getInstance();
  598. calendar.setTime(date);
  599. calendar.add(field, (int) amount);
  600. return calendar.getTime();
  601. }
  602.  
  603. /**
  604. * Gets the first day of offset month.
  605. *
  606. * @param date the date
  607. * @param formatStr the format str
  608. * @param offSet the off set
  609. * @return the first day of offset month
  610. */
  611. public static String getFirstDayOfOffsetMonth(String date, String formatStr, int offSet) {
  612. Date myDate = DateUtil.StringToDate(date, formatStr);
  613. Calendar cal = Calendar.getInstance();
  614. cal.setTime(myDate);
  615. cal.set(Calendar.DAY_OF_MONTH, 1);
  616. cal.add(Calendar.MONTH, offSet);
  617. String ret = DateUtil.DateToString(cal.getTime(), DateUtil.CM_SHORT_DATE_FORMAT);
  618. return ret;
  619. }
  620.  
  621. /**
  622. * Get the time several months ago.
  623. *
  624. * @param months
  625. * @return
  626. */
  627. public static Date getTimeMonthsAgo(int months) {
  628. Calendar cal = Calendar.getInstance();
  629. cal.setTime(new Date());
  630. cal.add(Calendar.MONTH, 0 - months);
  631.  
  632. return cal.getTime();
  633. }
  634.  
  635.  
  636. /**
  637. * 得到跟myDate相隔interval个月的日期
  638. *
  639. * @param myDate
  640. * @param interval
  641. * @return
  642. */
  643. public static Date getYear(Date myDate, int interval) {
  644. Calendar cal = Calendar.getInstance();
  645. cal.setTime(myDate);
  646. cal.add(Calendar.YEAR, interval);
  647. return cal.getTime();
  648. }
  649.  
  650. /**
  651. * 得到跟myDate相隔interval个月的日期
  652. *
  653. * @param myDate
  654. * @param interval
  655. * @return
  656. */
  657. public static Date getMonth(Date myDate, int interval) {
  658. Calendar cal = Calendar.getInstance();
  659. cal.setTime(myDate);
  660. cal.add(Calendar.MONTH, interval);
  661. return cal.getTime();
  662. }
  663.  
  664. /**
  665. * 得到跟myDate相隔interval日的日期
  666. *
  667. * @param myDate
  668. * @param interval
  669. * @return
  670. */
  671. public static Date getDay(Date myDate, int interval) {
  672. Calendar cal = Calendar.getInstance();
  673. cal.setTime(myDate);
  674. cal.add(Calendar.DATE, interval);
  675. return cal.getTime();
  676. }
  677.  
  678. /**
  679. * 得到跟myDate相隔interval小时前的日期
  680. *
  681. * @param myDate
  682. * @param interval
  683. * @return
  684. */
  685. public static String getDateTimeByHours(String myDate, int interval) {
  686. Calendar cal = Calendar.getInstance();
  687. cal.setTime(stringForceToDate(myDate));
  688. cal.add(Calendar.HOUR_OF_DAY, interval);
  689. return DateUtil.DateToString(cal.getTime(), CM_LONG_DATE_FORMAT);
  690. }
  691.  
  692. public static int compareDate(String date1, String date2){
  693. long d1 = stringForceToDate(date1).getTime();
  694. long d2 = stringForceToDate(date2).getTime();
  695. if (d1 < d2) return -1;
  696. if (d1 > d2) return 1;
  697. return 0;
  698. }
  699. /**
  700. * 用多种DateFormat强转成date,转不了返回null
  701. */
  702. public static Date stringForceToDate(String date) {
  703. Date d = null;
  704. try {
  705. SimpleDateFormat sdf = new SimpleDateFormat(CM_LONG_DATE_FORMAT);
  706. d = sdf.parse(date);
  707. } catch (Exception e) {
  708. try {
  709. SimpleDateFormat sdf = new SimpleDateFormat(CM_DATE_FORMAT);
  710. d = sdf.parse(date);
  711. } catch (ParseException e1) {
  712. try {
  713. SimpleDateFormat sdf = new SimpleDateFormat(CM_SHORT_MONTH_FORMAT);
  714. d = sdf.parse(date);
  715. } catch (ParseException e2) {
  716. try {
  717. SimpleDateFormat sdf = new SimpleDateFormat(CM_SHORT_DATE_FORMAT);
  718. d = sdf.parse(date);
  719. } catch (ParseException e3) {
  720. try {
  721. SimpleDateFormat sdf = new SimpleDateFormat(CM_SHORT_YEAR_FORMAT);
  722. d = sdf.parse(date);
  723. } catch (ParseException e4) {
  724. d = null;
  725. }
  726. }
  727. }
  728. }
  729. }
  730. return d;
  731. }
  732.  
  733.  
  734. public static void main(String[] args) {
  735. Date date = DateUtil.getDate("2013", "09", "2");
  736. Date date2 = getMonth(date, 1);
  737. Date date3 = getDay(date2, 1);
  738. System.out.println(DateUtil.DateToString(date, "yyyy-MM-dd HH:mm:ss"));
  739. System.out.println(DateUtil.DateToString(date2, "yyyy-MM-dd HH:mm:ss"));
  740. System.out.println(DateUtil.DateToString(date3, "yyyy-MM-dd HH:mm:ss"));
  741. System.out.println(DateUtil.getNowMonth());
  742. System.out.println(DateUtil.getDay(new Date(), -30));
  743.  
  744. final DateTime now = DateTime.now();
  745. // Date sTime = now.plusHours(-9).secondOfMinute().withMinimumValue().toDate();
  746. Date sTime = now.plusMinutes(-5).secondOfMinute().withMinimumValue().toDate();
  747. Date eTime = now.plusHours(0).secondOfMinute().withMaximumValue().toDate();
  748. String startTime = (DateUtil.DateToString(sTime, "yyyy-MM-dd HH:mm:ss"));
  749. String endTime = (DateUtil.DateToString(eTime, "yyyy-MM-dd HH:mm:ss"));
  750. System.out.println(getDateTimeByHours("2018-10-25 00:00:00",-8));
  751. System.out.println(startTime);
  752. System.out.println(endTime);
  753. }
  754. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement