DateUtil_N.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. package com.ydtech.utils;
  2. import com.github.pagehelper.util.StringUtil;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.util.ObjectUtils;
  6. import java.math.BigDecimal;
  7. import java.math.RoundingMode;
  8. import java.text.DecimalFormat;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Calendar;
  12. import java.util.Date;
  13. /**
  14. * 功能:对日期操作的公共方法类 作者:袁伟民 时间:2009-7-29 21:16 修改:
  15. */
  16. public class DateUtil_N {
  17. /**
  18. * log日志
  19. */
  20. private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
  21. private static final String DATEFORMAT = "yyyy-MM-dd";
  22. public static Date getNextNDate;
  23. private DateUtil_N() {
  24. }
  25. /***
  26. * 格式化日期的方法
  27. *
  28. * @param date
  29. * 带格式化的日期
  30. * @param pattern
  31. * 格式化的表达式 比如 yyyy-MM-dd
  32. * @return 格式化后的字符串
  33. */
  34. public static String formatDate(Date date, String pattern) {
  35. // 建立日期FORMAT的实例
  36. SimpleDateFormat df = new SimpleDateFormat(pattern);
  37. if (null != date) {
  38. return df.format(date);
  39. }
  40. return null;
  41. }
  42. /***
  43. * 格式化日期的方法
  44. *
  45. * @param date
  46. * 带格式化的日期,默认为yyyy-MM-dd
  47. * @return 格式化后的字符串
  48. */
  49. public static String formatDate(Date date) {
  50. return formatDate(date, DATEFORMAT);
  51. }
  52. /**
  53. * 格式化日期的方法
  54. *
  55. * @param object
  56. * @return
  57. */
  58. public static String getDate(Object object) {
  59. //小写的mm表示的是分钟
  60. SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
  61. return object != null ? sdf.format((Date) object) : null;
  62. }
  63. /***
  64. * 转化成中文日期的方法
  65. *
  66. * @param date
  67. * 带格式化的日期,默认为yyyy-MM-dd
  68. * @return 格式化后的字符串
  69. */
  70. public static String formatCDate(Date date) {
  71. if (null != date) {
  72. String dateNew = formatDate(date, DATEFORMAT);
  73. if (null != dateNew && StringUtil.isNotEmpty(dateNew)) {
  74. String[] dates = dateNew.split("-");
  75. if (!ObjectUtils.isEmpty(dates)) {
  76. return dates[0] + "年" + dates[1] + "月" + dates[2] + "日";
  77. } else {
  78. return dateNew;
  79. }
  80. }
  81. }
  82. return "";
  83. }
  84. /***
  85. * 转化成中文日期的方法(年、月)
  86. *
  87. * @param date
  88. * 带格式化的日期,默认为yyyy-MM
  89. * @return 格式化后的字符串
  90. */
  91. public static String formatCYearAndMonth(Date date) {
  92. if (null != date) {
  93. String dateNew = formatDate(date, "yyyy-MM");
  94. String[] dates = dateNew.split("-");
  95. if (dates != null) {
  96. return dates[0] + "年" + dates[1] + "月";
  97. } else {
  98. return dateNew;
  99. }
  100. }
  101. return null;
  102. }
  103. /***
  104. * 格式化日期的方法
  105. *
  106. * @param date
  107. * 带格式化的日期,默认为yyyy-MM
  108. * @return 格式化后的字符串
  109. */
  110. public static String formatYearAndMonth(Date date) {
  111. return formatDate(date, "yyyy-MM");
  112. }
  113. /**
  114. * 利用传入的日期,返回下个月最后一天的日期
  115. * guoyonggang
  116. *
  117. * @return
  118. */
  119. public static Date getNextMonthEnd(Date current) {
  120. Calendar lastDate = Calendar.getInstance();
  121. lastDate.setTime(current);
  122. // 加一个月
  123. lastDate.add(Calendar.MONTH, 1);
  124. // 把日期设置为当月第一天
  125. lastDate.set(Calendar.DATE, 1);
  126. // 日期回滚一天,也就是本月最后一天
  127. lastDate.roll(Calendar.DATE, -1);
  128. return lastDate.getTime();
  129. }
  130. /***
  131. * 获取传入日期下一年的日期对象
  132. *
  133. * @param date
  134. * 传入的日期对象
  135. * @return 下一年的日期对象
  136. */
  137. public static Date getNextYear(Date date) {
  138. Calendar calendar = Calendar.getInstance();
  139. calendar.setTime(date);
  140. calendar.add(Calendar.YEAR, 1);
  141. return calendar.getTime();
  142. }
  143. /***
  144. * 获取传入日期下n年的日期对象
  145. *
  146. * @param date
  147. * 传入的日期对象
  148. * @return 下n年的日期对象
  149. */
  150. public static Date getNextNYear(Date date, int n) {
  151. Calendar calendar = Calendar.getInstance();
  152. calendar.setTime(date);
  153. calendar.add(Calendar.YEAR, n);
  154. return calendar.getTime();
  155. }
  156. /***
  157. * 计算传入日期的往后顺延N天以后的日期对象
  158. *
  159. * @param date
  160. * 传入的日期对象
  161. * @param n
  162. * 往后顺延的天数
  163. * @return 顺延后的日期对象
  164. */
  165. public static Date getPreviousNDate(Date date, long n) {
  166. long time = date.getTime();
  167. // 用毫秒数来计算新的日期
  168. time = time - n * 1000 * 24 * 60 * 60;
  169. return new Date(time);
  170. }
  171. /***
  172. * 计算传入日期的往前推算N天的日期对象
  173. *
  174. * @param date
  175. * 传入的日期对象
  176. * @param n
  177. * 往后顺延的天数
  178. * @return 往前推算后的日期对象
  179. */
  180. public static Date getNextNDate(Date date, long n) {
  181. long time = date.getTime();
  182. // 用毫秒数来计算新的日期
  183. time = time + n * 1000 * 24 * 60 * 60;
  184. return new Date(time);
  185. }
  186. /***
  187. * 计算传入日期的前一天
  188. *
  189. * @param date
  190. * 传入日期
  191. * @return 传入日期的前一天的日期对象
  192. */
  193. public static Date getPreviousDate(Date date) {
  194. return getPreviousNDate(date, 1);
  195. }
  196. /***
  197. * 计算传入日期的前一天
  198. *
  199. * @param date
  200. * 传入日期
  201. * @return 传入日期的前一天的日期对象
  202. */
  203. public static String getPreviousDate(String date) throws Exception {
  204. SimpleDateFormat sdfm = new SimpleDateFormat("yyyy-MM-dd");
  205. Date curMonthStart_wj = getPreviousDate(sdfm.parse(date));
  206. return dateToStr(curMonthStart_wj);
  207. }
  208. /***
  209. * 计算传入日期的后一天
  210. *
  211. * @param date
  212. * 传入日期
  213. * @return 传入日期的后一天的日期对象
  214. */
  215. public static Date getNextDate(Date date) {
  216. return getNextNDate(date, 1);
  217. }
  218. /**
  219. * 日期转换成字符串
  220. *
  221. * @param date
  222. * @return str
  223. * @作者:滑立敏
  224. * @日期:2010-01-26
  225. */
  226. public static String dateToStr(Date date) {
  227. SimpleDateFormat format = new SimpleDateFormat(DATEFORMAT);
  228. return format.format(date);
  229. }
  230. /**
  231. * 字符串转换成日期
  232. *
  233. * @param str
  234. * @return date
  235. * @作者:滑立敏
  236. * @日期:2010-01-26
  237. */
  238. public static Date strToDate(String str) {
  239. SimpleDateFormat format = new SimpleDateFormat(DATEFORMAT);
  240. Date date = null;
  241. try {
  242. date = format.parse(str);
  243. } catch (ParseException e) {
  244. logger.info(e.getMessage());
  245. }
  246. return date;
  247. }
  248. /**
  249. * 根据传入的日期计算此日期与下一年该日相差的天数
  250. *
  251. * @param startDate 起始日期
  252. * @return 天数区间
  253. * @作者:李阳
  254. * @时间:2010-06-22
  255. */
  256. public static int getDaysCountForYear(Date startDate) {
  257. // 获取传入日期下一年的日期对象
  258. Date nextYear = getNextYear(startDate);
  259. // 计算天数
  260. return getDaysCount(startDate, nextYear);
  261. }
  262. /***
  263. * 根据起始日期、终止日期计算天数
  264. *
  265. * @param startDate
  266. * 起始日期
  267. * @param endDate
  268. * 终止日期
  269. * @return 天数区间
  270. */
  271. public static int getDaysCount(Date startDate, Date endDate) {
  272. return getDaysCount(startDate, 0, endDate, 0);
  273. }
  274. /**
  275. * 根据起始日期、终止日期,起始小时,终止小时计算天数
  276. *
  277. * @param startDate
  278. * @param endDate
  279. * @param startHour
  280. * @param endHour
  281. * @return
  282. */
  283. public static int getDaysCounts(Date startDate, Date endDate, int startHour, int endHour) {
  284. return getDaysCount(startDate, startHour, endDate, endHour);
  285. }
  286. /***
  287. * 根据年数返回所包含的天数
  288. *
  289. * @param year
  290. * 年数
  291. * @return 该年所包含的天数
  292. */
  293. public static int getDaysFromYear(int year) {
  294. // 判断是平年还是闰年
  295. boolean flag = (year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0);
  296. if (flag) {
  297. return 366;
  298. } else {
  299. return 365;
  300. }
  301. }
  302. /**
  303. * 判斷是否是閏年
  304. *
  305. * @param year
  306. */
  307. public static boolean isLeapYear(int year) {
  308. boolean flag1 = (year % 4 == 0);
  309. boolean flag2 = (year % 100 == 0);
  310. boolean flag3 = (year % 400 == 0);
  311. return (flag1 && !flag2) || (flag3);
  312. }
  313. /***
  314. * 根据起始日期、起始时间、终止日期、终止时间计算天数
  315. *
  316. * @param startDate
  317. * 起始日期
  318. * @param startHour
  319. * 起始小时
  320. * @param endDate
  321. * 终止日期
  322. * @param endHour
  323. * 终止小时
  324. * @return 天数区间
  325. */
  326. public static int getDaysCount(Date startDate, int startHour, Date endDate,
  327. int endHour) {
  328. // 根据起始日期计算起始的毫秒
  329. long startTime = startDate.getTime();
  330. // 根据终止日期计算终止的毫秒
  331. long endTime = endDate.getTime();
  332. // 通过起始毫秒和终止毫秒的差值,计算天数
  333. int dayCount = (int) ((endTime - startTime) / (24 * 60 * 60) + 1);
  334. if (endHour <= startHour) {
  335. if (startHour == 24 && endHour == 0) {
  336. dayCount = dayCount - 2;
  337. } else {
  338. dayCount = dayCount - 1;
  339. }
  340. }
  341. return dayCount;
  342. }
  343. /***
  344. * 根据起始日期、起始时间、终止日期、终止时间计算天数
  345. *
  346. * @param startDate
  347. * 起始日期
  348. * @param startHour
  349. * 起始小时
  350. * @param endDate
  351. * 终止日期
  352. * @param endHour
  353. * 终止小时
  354. * @return 天数区间
  355. */
  356. public static int getHour(Date startDate, int startHour, Date endDate,
  357. int endHour) {
  358. // 根据起始日期计算起始的毫秒
  359. long startTime = startDate.getTime();
  360. // 根据终止日期计算终止的毫秒
  361. long endTime = endDate.getTime();
  362. // 通过起始毫秒和终止毫秒的差值,计算天数
  363. int dayCount = (int) ((endTime - startTime) / (24 * 60 * 60) + 1);
  364. if (endHour <= startHour) {
  365. dayCount = dayCount - 2;
  366. } else {
  367. dayCount = dayCount - 1;
  368. }
  369. return dayCount;
  370. }
  371. /**
  372. * 根据日期获取总小时数
  373. *
  374. * @param date 日期对象
  375. * @return 总小时数
  376. */
  377. public static int getHoursCount(Date date) {
  378. return getHoursCount(date, 0);
  379. }
  380. /**
  381. * 根据日期和小时数,获取总小时数
  382. *
  383. * @param date 日期对象
  384. * @param hour 小时数
  385. * @return 总小时数
  386. */
  387. public static int getHoursCount(Date date, Integer hour) {
  388. long hourCount = hour;
  389. hourCount += date.getTime() / 3600 / 1000;
  390. return (int) hourCount;
  391. }
  392. /***
  393. * 比较两个日期的大小
  394. *
  395. * @param startDate
  396. * 开始日期
  397. * @param endDate
  398. * 结束日期
  399. * @return -1 : 开始日期小于结束日期 0 : 开始日期等于结束日期 1 : 开始日期大于结束日期
  400. */
  401. public static int compareDate(Date startDate, Date endDate) {
  402. long startTime = startDate.getTime();
  403. long endTime = endDate.getTime();
  404. if (startTime < endTime) {
  405. return -1;
  406. }
  407. if (startTime == endTime) {
  408. return 0;
  409. }
  410. return 1;
  411. }
  412. /***
  413. * 比较两个日期的大小
  414. *
  415. * @param startDate
  416. * 开始日期
  417. * @param endDate
  418. * 结束日期
  419. * @return -1 : 开始日期小于结束日期 0 : 开始日期等于结束日期 1 : 开始日期大于结束日期
  420. */
  421. public static int compareDate(String startDate, String endDate) {
  422. try {
  423. return compareDate(strToDate(startDate), strToDate(endDate));
  424. } catch (Exception e) {
  425. logger.info(e.getMessage());
  426. }
  427. return 0;
  428. }
  429. /**
  430. * 数字转中文数字
  431. *
  432. * @param src
  433. * @return
  434. */
  435. public static String intToChineseNum(int src) {
  436. final String[] num = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
  437. final String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
  438. StringBuilder dst = new StringBuilder();
  439. String s = String.valueOf(src);
  440. for (int i = 0; i < s.length(); i++) {
  441. String index = String.valueOf(s.charAt(i));
  442. dst = dst.append(num[Integer.parseInt(index)]);
  443. }
  444. String sss = String.valueOf(dst);
  445. int i = 0;
  446. for (int j = sss.length(); j > 0; j--) {
  447. dst = dst.insert(j, unit[i++]);
  448. }
  449. return dst.toString();
  450. }
  451. /**
  452. * 金额格式化,加上千分符
  453. *
  454. * @param bd
  455. * @return
  456. */
  457. public static String getFormatNumber(BigDecimal bd) {
  458. DecimalFormat df = new DecimalFormat(",###,##0.00");
  459. if (bd != null) {
  460. bd = bd.setScale(2, RoundingMode.HALF_UP);
  461. return df.format(bd);
  462. } else {
  463. return "0.00";
  464. }
  465. }
  466. public static String getDay(Date date) {
  467. return new SimpleDateFormat("dd").format(date);
  468. }
  469. public static String getMonth(Date date) {
  470. return new SimpleDateFormat("MM").format(date);
  471. }
  472. public static String getYear(Date date) {
  473. return new SimpleDateFormat("yyyy").format(date);
  474. }
  475. public static String getHour(Date date) {
  476. return new SimpleDateFormat("HH").format(date);
  477. }
  478. /**
  479. * 利用传入的日期,获取本月的第一天
  480. *
  481. * @return
  482. */
  483. public static String getMonthStart(Date current) {
  484. Calendar lastDate = Calendar.getInstance();
  485. lastDate.setTime(current);
  486. lastDate.set(Calendar.DATE, 1);
  487. return formatDate(lastDate.getTime());
  488. }
  489. /**
  490. * 利用传入的日期,获取本月的最后一天
  491. *
  492. * @return
  493. */
  494. public static String getMonthEnd(Date current) {
  495. Calendar lastDate = Calendar.getInstance();
  496. lastDate.setTime(current);
  497. // 加一个月
  498. lastDate.add(Calendar.MONTH, 1);
  499. // 把日期设置为当月第一天
  500. lastDate.set(Calendar.DATE, 1);
  501. // 日期较少一天,也就不上个月的最后一天
  502. return formatDate(getPreviousDate(lastDate.getTime()));
  503. }
  504. /**
  505. * 利用传入的日期,获取去年当月的第一天
  506. *
  507. * @return
  508. */
  509. public static String getPreMonthStart(Date current) {
  510. Calendar lastDate = Calendar.getInstance();
  511. lastDate.setTime(current);
  512. // 减一年
  513. lastDate.add(Calendar.YEAR, -1);
  514. lastDate.set(Calendar.DATE, 1);
  515. return formatDate(lastDate.getTime());
  516. }
  517. /**
  518. * 利用传入的日期,获取上月的最后一天
  519. *
  520. * @return
  521. */
  522. public static String getPreMonthEnd(Date current) {
  523. Calendar c = Calendar.getInstance();
  524. c.setTime(current);
  525. // 把日期设置为当月第一天
  526. c.set(Calendar.DATE, 1);
  527. // 日期较少一天,也就不上个月的最后一天
  528. return formatDate(getPreviousDate(c.getTime()));
  529. }
  530. /**
  531. * 获取当前季度
  532. */
  533. public static int getQuarter(Date current) {
  534. Calendar c = Calendar.getInstance();
  535. c.setTime(current);
  536. int month = c.get(Calendar.MONTH) + 1;
  537. int quarter;
  538. if (month >= 1 && month <= 3) {
  539. quarter = 1;
  540. } else if (month >= 4 && month <= 6) {
  541. quarter = 2;
  542. } else if (month >= 7 && month <= 9) {
  543. quarter = 3;
  544. } else {
  545. quarter = 4;
  546. }
  547. return quarter;
  548. }
  549. /**
  550. * 根据传入的日期获取该季度的第一天和最后一天
  551. *
  552. * @param
  553. */
  554. public static String[] getCurrQuarter(Date current) {
  555. String[] s = new String[2];
  556. String str;
  557. // 设置本年的季
  558. int num = getQuarter(current);
  559. switch (num) {
  560. case 1: // 本年到现在经过了一个季度,在加上前4个季度
  561. Calendar quarter1 = Calendar.getInstance();
  562. quarter1.setTime(current);
  563. quarter1.set(Calendar.MONTH, 3);
  564. quarter1.set(Calendar.DATE, 1);
  565. quarter1.add(Calendar.DATE, -1);
  566. str = new SimpleDateFormat(DATEFORMAT).format(quarter1.getTime());
  567. s[0] = str.substring(0, str.length() - 5) + "01-01";
  568. s[1] = str;
  569. break;
  570. case 2: // 本年到现在经过了二个季度,在加上前三个季度
  571. Calendar quarter2 = Calendar.getInstance();
  572. quarter2.setTime(current);
  573. quarter2.set(Calendar.MONTH, 6);
  574. quarter2.set(Calendar.DATE, 1);
  575. quarter2.add(Calendar.DATE, -1);
  576. str = new SimpleDateFormat(DATEFORMAT).format(quarter2.getTime());
  577. s[0] = str.substring(0, str.length() - 5) + "04-01";
  578. s[1] = str;
  579. break;
  580. case 3:// 本年到现在经过了三个季度,在加上前二个季度
  581. Calendar quarter3 = Calendar.getInstance();
  582. quarter3.setTime(current);
  583. quarter3.set(Calendar.MONTH, 9);
  584. quarter3.set(Calendar.DATE, 1);
  585. quarter3.add(Calendar.DATE, -1);
  586. str = new SimpleDateFormat(DATEFORMAT).format(quarter3.getTime());
  587. s[0] = str.substring(0, str.length() - 5) + "07-01";
  588. s[1] = str;
  589. break;
  590. case 4:// 本年到现在经过了四个季度,在加上前一个季度
  591. Calendar quarter4 = Calendar.getInstance();
  592. quarter4.setTime(current);
  593. str = new SimpleDateFormat(DATEFORMAT).format(quarter4.getTime());
  594. s[0] = str.substring(0, str.length() - 5) + "10-01";
  595. s[1] = str.substring(0, str.length() - 5) + "12-31";
  596. break;
  597. default:
  598. break;
  599. }
  600. return s;
  601. }
  602. /**
  603. * 根据传入的日期获取当年的第一天和最后一天
  604. *
  605. * @param
  606. */
  607. public static String[] getCurrYear(Date current) {
  608. String[] s = new String[2];
  609. Calendar quarterCalendar = Calendar.getInstance();
  610. quarterCalendar.setTime(current);
  611. String str = new SimpleDateFormat(DATEFORMAT).format(quarterCalendar.getTime());
  612. s[0] = str.substring(0, str.length() - 5) + "01-01";
  613. s[1] = str.substring(0, str.length() - 5) + "12-31";
  614. return s;
  615. }
  616. /***
  617. * 获取上n日期yyyy-MM-dd
  618. */
  619. public static String getPreviousDateStr(Date date, int n) {
  620. long time = date.getTime();
  621. // 用毫秒数来计算新的日期
  622. time = time - n * 1000 * 24 * 60 * 60;
  623. return formatDate(new Date(time));
  624. }
  625. /**
  626. * 获取去年这个季度的第一天和最后一天
  627. */
  628. public static String[] getPreQuarter(Date current) {
  629. Calendar quarterCalendar = Calendar.getInstance();
  630. quarterCalendar.setTime(current);
  631. //将日期减少一年
  632. quarterCalendar.add(Calendar.YEAR, -1);
  633. quarterCalendar.set(Calendar.DATE, 1);
  634. //返回[当前]季度的第一天和最后一天
  635. return getCurrQuarter(quarterCalendar.getTime());
  636. }
  637. /**
  638. * 获取上年的第一天和最后一天
  639. */
  640. public static String[] getPreYear(Date current) {
  641. Calendar quarterCalendar = Calendar.getInstance();
  642. quarterCalendar.setTime(current);
  643. //将日期减少一年
  644. quarterCalendar.add(Calendar.YEAR, -1);
  645. quarterCalendar.set(Calendar.DATE, 1);
  646. //返回[当前]年份的第一天和最后一天
  647. return getCurrYear(quarterCalendar.getTime());
  648. }
  649. /**
  650. * 获取去年今日
  651. */
  652. public static String getPreYearDay(Date current) {
  653. Calendar c = Calendar.getInstance();
  654. c.setTime(current);
  655. //将日期减少一年
  656. c.add(Calendar.YEAR, -1);
  657. return formatDate(c.getTime());
  658. }
  659. public static String getPreYearDay1(Date current) {
  660. Calendar c = Calendar.getInstance();
  661. c.setTime(current);
  662. //将日期减少一年
  663. c.add(Calendar.YEAR, 0);
  664. return formatDate(c.getTime());
  665. }
  666. }