DateUtil_N.java 22 KB

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