Java封装常用时间工具类

蔚落 2022-01-15 00:45 354阅读 0赞
  1. public class DateUtils {
  2. /** 系统默认 日期类型 yyyy-MM-dd */
  3. public static final String DATE_PATTERN_DEFAULT = "yyyy-MM-dd";
  4. /** 时间 日期类型 HH:mm:ss */
  5. public static final String DATE_PATTERN_TIME = "HH:mm:ss";
  6. /** 日期时间 日期类型 yyyy-MM-dd HH:mm:ss */
  7. public static final String DATE_PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss";
  8. /** 时期格式 yyyy-MM-dd */
  9. public static DateFormat dateformater;
  10. /** 时间格式 HH:mm:ss */
  11. public static DateFormat timeformater;
  12. /** 日期时间格式 yyyy-MM-dd HH:mm */
  13. public static DateFormat dateTimeformater;
  14. static {
  15. if (DateUtils.dateformater == null) {
  16. DateUtils.dateformater = new SimpleDateFormat(DateUtils.DATE_PATTERN_DEFAULT);
  17. }
  18. if (DateUtils.timeformater == null) {
  19. DateUtils.timeformater = new SimpleDateFormat(DateUtils.DATE_PATTERN_TIME);
  20. }
  21. if (DateUtils.dateTimeformater == null) {
  22. DateUtils.dateTimeformater = new SimpleDateFormat(DateUtils.DATE_PATTERN_DATETIME);
  23. }
  24. }
  25. /** 一天毫秒数 */
  26. public static final long DAY_IN_MILLISECOND = 1000 * 3600 * 24;
  27. /** 一小时毫秒数 */
  28. public static final long HOUR_IN_MILLISECOND = 1000 * 3600;
  29. /** 构造方法私有化 */
  30. private DateUtils() {
  31. }
  32. /**
  33. * 得到现在时间
  34. *
  35. * @return 字符串 yyyyMMdd HHmmss
  36. */
  37. public static String getStringToday() {
  38. final Date currentTime = new Date();
  39. final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
  40. final String dateString = formatter.format(currentTime);
  41. return dateString;
  42. }
  43. /**
  44. *
  45. * 根据 yyyy-MM-dd 字符串解析成相应的日期
  46. *
  47. * @param strDate
  48. * yyyy-MM-dd 格式的日期
  49. *
  50. * @return 转换后的日期
  51. *
  52. */
  53. public static Date parseDate(String strDate) {
  54. Date date = null;
  55. if (StringUtils.isNotBlank(strDate)) {
  56. try {
  57. date = DateUtils.dateformater.parse(strDate);
  58. }
  59. catch (final Exception e) {
  60. e.printStackTrace();
  61. return date;
  62. }
  63. }
  64. return date;
  65. }
  66. /**
  67. *
  68. * 根据传入的日期格式 将字符串解析成 日期类型
  69. *
  70. * @param strDate
  71. * 日期字符串
  72. *
  73. * @param pattern
  74. * 日期格式 如果传入格式为空,则为默认格式 yyyy-MM-dd
  75. *
  76. * @return 日期类型
  77. *
  78. */
  79. public static Date parseDate(String strDate, String pattern) {
  80. Date date = null;
  81. try {
  82. final DateFormat format = DateUtils.getDateFormat(pattern);
  83. date = format.parse(strDate);
  84. }
  85. catch (final Exception e) {
  86. e.printStackTrace();
  87. return date;
  88. }
  89. return date;
  90. }
  91. /**
  92. *
  93. * 根据 HH:mm:ss 字符串解析成相应的时间格式
  94. *
  95. * @param strTime
  96. * HH:mm:ss 格式的时间格式
  97. *
  98. * @return 转换后的时间
  99. *
  100. */
  101. public static Date parseTime(String strTime) {
  102. Date date = null;
  103. try {
  104. date = DateUtils.timeformater.parse(strTime);
  105. }
  106. catch (final Exception e) {
  107. e.printStackTrace();
  108. return date;
  109. }
  110. return date;
  111. }
  112. /**
  113. *
  114. * 根据yyyy-MM-dd HH:mm字符串解析成相应的日期时间
  115. * @param strDateTime 以"yyyy-MM-dd HH:mm:ss"为格式的时间字符串
  116. * @return 转换后的日期
  117. *
  118. */
  119. public static Date parseDateTime(String strDateTime) {
  120. Date date = new Date();
  121. try {
  122. date = DateUtils.dateTimeformater.parse(strDateTime);
  123. }
  124. catch (final Exception e) {
  125. e.printStackTrace();
  126. return date;
  127. }
  128. return date;
  129. }
  130. /**
  131. *
  132. * 获取系统当前时间
  133. *
  134. * @return 系统当前时间
  135. *
  136. */
  137. public static Date getCurrentDate() {
  138. final Calendar gc = Calendar.getInstance();
  139. return gc.getTime();
  140. }
  141. /**
  142. *
  143. * 得到日期的起始日期,例如2004-1-1 15:12,转换后为 2004-1-1 00:00
  144. *
  145. * @param date
  146. * 需要转换的日期
  147. *
  148. * @return 该日期的零点
  149. *
  150. */
  151. public static Date getTodayStart(Date date) {
  152. final Calendar gc = Calendar.getInstance();
  153. gc.setTime(date);
  154. gc.set(Calendar.HOUR_OF_DAY, 0);
  155. gc.set(Calendar.MINUTE, 0);
  156. gc.set(Calendar.SECOND, 0);
  157. gc.set(Calendar.MILLISECOND, 0);
  158. return gc.getTime();
  159. }
  160. /**
  161. *
  162. * 得到日期的结束日期,例如2004-1-1 15:12,转换后为2004-1-2 00:00,注意为第二天的0点整
  163. *
  164. * @param date
  165. * 所要转换的日期
  166. *
  167. * @return 为第二天的零点整
  168. *
  169. */
  170. public static Date getTodayEnd(Date date) {
  171. final Calendar gc = Calendar.getInstance();
  172. gc.setTime(DateUtils.dateDayAdd(date, 1));
  173. return DateUtils.getTodayStart(gc.getTime());
  174. }
  175. /**
  176. *
  177. * 得到日期所在月份的开始日期(第一天的开始日期),例如2004-1-15 15:10,转换后为2004-1-1 00:00
  178. *
  179. * @param date
  180. * 需要转换的日期
  181. *
  182. * @return 日期所在月份的开始日期
  183. *
  184. */
  185. public static Date getMonthBegin(Date date) {
  186. final Calendar gc = Calendar.getInstance();
  187. gc.setTime(date);
  188. final int year = gc.get(Calendar.YEAR);
  189. final int mon = gc.get(Calendar.MONTH);
  190. final Calendar gCal = new GregorianCalendar(year, mon, 1);
  191. return gCal.getTime();
  192. }
  193. /**
  194. *
  195. * 根据年、月返回由年、月构成的日期的月份开始日期
  196. *
  197. * @param year
  198. * 所在的年
  199. *
  200. * @param month
  201. * 所在的月份,从1月到12月
  202. *
  203. * @return 由年、月构成的日期的月份开始日期
  204. *
  205. */
  206. public static Date getMonthBegin(int year, int month) {
  207. if ((month <= 0) || (month > 12)) {
  208. throw new IllegalArgumentException("month must from 1 to 12");
  209. }
  210. final Calendar gc = new GregorianCalendar(year, month - 1, 1);
  211. return gc.getTime();
  212. }
  213. /**
  214. *
  215. * 根据日期所在的月份,得到下个月的第一天零点整
  216. *
  217. * @param date
  218. * 需要转换的日期
  219. *
  220. * @return 对应日期的下个月的第一天零点整
  221. *
  222. */
  223. public static Date getMonthEnd(Date date) {
  224. final Calendar cal = Calendar.getInstance();
  225. cal.setTime(date);
  226. final int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  227. cal.set(Calendar.DAY_OF_MONTH, lastDay);
  228. return DateUtils.getTodayEnd(cal.getTime());
  229. }
  230. /**
  231. *
  232. * 根据日期所在的星期,得到这个星期的开始日期,注意,每周从星期日开始计算
  233. *
  234. * @param date
  235. * 需要转换的日期
  236. *
  237. * @return 传入日期所在周的第一天的零点整
  238. *
  239. */
  240. public static Date getWeekBegin(Date date) {
  241. final Calendar gCal = Calendar.getInstance();
  242. gCal.setTime(date);
  243. final int startDay = gCal.getActualMinimum(Calendar.DAY_OF_WEEK);
  244. gCal.set(Calendar.DAY_OF_WEEK, startDay);
  245. return gCal.getTime();
  246. }
  247. /**
  248. *
  249. * 根据日期所在的星期,得到下周开始第一天的零点整
  250. *
  251. * @param date
  252. * 需要转换的日期
  253. *
  254. * @return 传入日期的下周开始第一天的零点整
  255. *
  256. */
  257. public static Date getWeekEnd(Date date) {
  258. final Calendar gCal = Calendar.getInstance();
  259. gCal.setTime(date);
  260. final int lastDay = gCal.getActualMaximum(Calendar.DAY_OF_WEEK);
  261. gCal.set(Calendar.DAY_OF_WEEK, lastDay);
  262. return DateUtils.getTodayEnd(gCal.getTime());
  263. }
  264. /**
  265. *
  266. * 根据年、月返回由年、月构成的日期的下一个月第一天零点整
  267. *
  268. * @param year
  269. * 所在的年
  270. *
  271. * @param month
  272. * 所在的月份,从1月到12月
  273. *
  274. * @return 下一个月第一天零点整
  275. *
  276. */
  277. public static Date getMonthEnd(int year, int month) {
  278. final Date start = DateUtils.getMonthBegin(year, month);
  279. return DateUtils.getMonthEnd(start);
  280. }
  281. /**
  282. *
  283. * 根据日期所在的年份,得到当年的开始日期,为每年的1月1日零点整
  284. *
  285. * @param date
  286. * 需要转换的日期
  287. *
  288. * @return 当年的开始日期,为每年的1月1日零点整
  289. *
  290. */
  291. public static Date getYearBegin(Date date) {
  292. final Calendar gCal = Calendar.getInstance();
  293. gCal.setTime(date);
  294. gCal.set(Calendar.DAY_OF_YEAR, 1);
  295. return DateUtils.getTodayStart(gCal.getTime());
  296. }
  297. /**
  298. *
  299. * 根据日期所在的年份,得到当年的结束日期,为来年的1月1日零点整
  300. *
  301. * @param date
  302. * 需要转换的日期
  303. *
  304. * @return 来年的1月1日零点整
  305. *
  306. */
  307. public static Date getYearEnd(Date date) {
  308. final Calendar cal = Calendar.getInstance();
  309. cal.setTime(date);
  310. final int lastday = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
  311. cal.set(Calendar.DAY_OF_YEAR, lastday);
  312. return DateUtils.getTodayEnd(cal.getTime());
  313. }
  314. /**
  315. *
  316. * 转换日期为 yyyy-MM-dd 格式的字符串
  317. *
  318. * @param date
  319. * 需要转换的日期
  320. *
  321. * @return 转换后的日期字符串
  322. *
  323. */
  324. public static String formatDate(Date date) {
  325. String str = "";
  326. if (date != null) {
  327. str = DateUtils.dateformater.format(date);
  328. }
  329. return str;
  330. }
  331. /**
  332. *
  333. * 转换指定日期成时间格式 HH:mm:ss 格式的字符串
  334. *
  335. * @param date
  336. * 指定的时间
  337. *
  338. * @return 转换后的字符串
  339. *
  340. */
  341. public static String formatTime(Date date) {
  342. return DateUtils.timeformater.format(date);
  343. }
  344. /**
  345. *
  346. * 转换指定日期成 yyyy-MM-dd HH:mm:ss 格式的字符串
  347. *
  348. * @param date
  349. * 需要转换的日期
  350. *
  351. * @return 转换后的字符串
  352. *
  353. */
  354. public static String formatDateTime(Date date) {
  355. return DateUtils.dateTimeformater.format(date);
  356. }
  357. /**
  358. *
  359. * 根据指定的转化模式,转换日期成字符串
  360. *
  361. * @param date
  362. * 需要转换的日期
  363. *
  364. * @param pattern
  365. * 日期格式 如果传入格式为空,则为默认格式 yyyy-MM-dd
  366. *
  367. * @return 转换后的字符串
  368. *
  369. */
  370. public static String formatDate(Date date, String pattern) {
  371. final DateFormat formater = DateUtils.getDateFormat(pattern);
  372. return formater.format(date);
  373. }
  374. /**
  375. *
  376. * 在指定日期的基础上,增加或是减少月份信息,如1月31日,增加一个月后,则为2月28日(非闰年)
  377. *
  378. * @param date
  379. * 指定的日期
  380. *
  381. * @param months
  382. * 增加或是减少的月份数,正数为增加,负数为减少
  383. *
  384. * @return 增加或是减少后的日期
  385. *
  386. */
  387. public static Date dateMonthAdd(Date date, int months) {
  388. final Calendar cal = Calendar.getInstance();
  389. cal.setTime(date);
  390. int m = cal.get(Calendar.MONTH) + months;
  391. if (m < 0) {
  392. m += -12;
  393. }
  394. cal.roll(Calendar.YEAR, m / 12);
  395. cal.roll(Calendar.MONTH, months);
  396. return cal.getTime();
  397. }
  398. /**
  399. *
  400. * 在指定的日期基础上,增加或是减少天数
  401. *
  402. * @param date
  403. * 指定的日期
  404. *
  405. * @param days
  406. * 需要增加或是减少的天数,正数为增加,负数为减少
  407. *
  408. * @return 增加或是减少后的日期
  409. *
  410. */
  411. public static Date dateDayAdd(Date date, int days) {
  412. final long now = date.getTime() + (days * DateUtils.DAY_IN_MILLISECOND);
  413. return new Date(now);
  414. }
  415. /**
  416. *
  417. * 在指定的日期基础上,增加或是减少年数
  418. *
  419. * @param date
  420. * 指定的日期
  421. *
  422. * @param year
  423. * 需要增加或是减少的年数,正数为增加,负数为减少
  424. *
  425. * @return 增加或是减少后的日期
  426. *
  427. */
  428. public static Date dateYearAdd(Date date, int year) {
  429. final Calendar cal = Calendar.getInstance();
  430. cal.setTime(date);
  431. cal.roll(Calendar.YEAR, year);
  432. return cal.getTime();
  433. }
  434. /**
  435. *
  436. * 得到日期的年数
  437. *
  438. * @param date
  439. * 指定的日期
  440. *
  441. * @return 返回指定日期的年数
  442. *
  443. */
  444. public static int getDateYear(Date date) {
  445. final Calendar cal = Calendar.getInstance();
  446. cal.setTime(date);
  447. return cal.get(Calendar.YEAR);
  448. }
  449. /**
  450. *
  451. * 得到指定日期的月份数
  452. *
  453. * @param date
  454. * 指定的日期
  455. *
  456. * @return 返回指定日期的月份数
  457. *
  458. */
  459. public static int getDateMonth(Date date) {
  460. final Calendar cal = Calendar.getInstance();
  461. cal.setTime(date);
  462. return cal.get(Calendar.MONDAY);
  463. }
  464. /**
  465. *
  466. * 得到制定日期在当前天数,例如2004-5-20日,返回141
  467. *
  468. * @param date
  469. * 指定的日期
  470. *
  471. * @return 返回的天数
  472. *
  473. */
  474. public static int getDateYearDay(Date date) {
  475. final Calendar cal = Calendar.getInstance();
  476. cal.setTime(date);
  477. return cal.get(Calendar.DAY_OF_YEAR);
  478. }
  479. /**
  480. *
  481. * 得到制定日期在当前月中的天数,例如2004-5-20日,返回20
  482. *
  483. * @param date
  484. * 指定的日期
  485. *
  486. * @return 返回天数
  487. *
  488. */
  489. public static int getDateMonthDay(Date date) {
  490. final Calendar cal = Calendar.getInstance();
  491. cal.setTime(date);
  492. return cal.get(Calendar.DAY_OF_MONTH);
  493. }
  494. /**
  495. *
  496. * 得到指定日期的年份
  497. *
  498. * @param date
  499. * 指定的日期
  500. *
  501. * @return 日期的年份
  502. *
  503. */
  504. public static int getYear(Date date) {
  505. final Calendar cal = Calendar.getInstance();
  506. cal.setTime(date);
  507. return cal.get(Calendar.YEAR);
  508. }
  509. /**
  510. *
  511. * 得到指定日期在当在一年中的月份数,从1开始
  512. *
  513. * @param date
  514. * 指定的日期
  515. *
  516. * @return 指定日期在当在一年中的月份数
  517. *
  518. */
  519. public static int getMonth(Date date) {
  520. final Calendar cal = Calendar.getInstance();
  521. cal.setTime(date);
  522. return cal.get(Calendar.MONTH) + 1;
  523. }
  524. /**
  525. *
  526. * 得到指定日期在当在一月中的号数,从1开始
  527. *
  528. * @param date
  529. * 指定的日期
  530. *
  531. * @return 日期在当在一月中的号数
  532. *
  533. */
  534. public static int getDay(Date date) {
  535. final Calendar cal = Calendar.getInstance();
  536. cal.setTime(date);
  537. return cal.get(Calendar.DAY_OF_MONTH);
  538. }
  539. /**
  540. *
  541. * 得到指定日期在当前星期中的天数,例如2004-5-20日,返回5,
  542. *
  543. * 每周以周日为开始按1计算,所以星期四为5
  544. *
  545. * @param date
  546. * 指定的日期
  547. *
  548. * @return 返回天数
  549. *
  550. */
  551. public static int getDateWeekDay(Date date) {
  552. final Calendar cal = Calendar.getInstance();
  553. cal.setTime(date);
  554. return cal.get(Calendar.DAY_OF_WEEK) - 1;
  555. }
  556. /**
  557. *
  558. * 得到指定日期在当前周内是第几天 (周一开始)
  559. *
  560. * @param date
  561. * 指定日期
  562. *
  563. * @return 周内天书
  564. *
  565. */
  566. public static int getWeek(Date date) {
  567. final Calendar cal = Calendar.getInstance();
  568. cal.setTime(date);
  569. return ((cal.get(Calendar.DAY_OF_WEEK) - 1) + 7) % 7;
  570. }
  571. /**
  572. *
  573. * 根据传入的格式,获取日期格式化实例,如果传入格式为空,则为默认格式 yyyy-MM-dd
  574. * @param pattern 日期格式
  575. * @return 格式化实例
  576. *
  577. */
  578. public static DateFormat getDateFormat(String pattern) {
  579. if (StringUtils.isBlank(pattern.trim())) {
  580. return new SimpleDateFormat(DateUtils.DATE_PATTERN_DEFAULT);
  581. } else {
  582. return new SimpleDateFormat(pattern);
  583. }
  584. }
  585. /**
  586. *
  587. * 计算两个时间之间的时间差
  588. *
  589. * @param from
  590. * 开始
  591. *
  592. * @param to
  593. * 结束
  594. *
  595. * @return 时间差
  596. *
  597. */
  598. public static long calculateTimeInMillis(Date from, Date to) {
  599. final Calendar fromCal = DateUtils.getCalendar(from);
  600. final Calendar toCal = DateUtils.getCalendar(to);
  601. if (fromCal.after(toCal)) {
  602. fromCal.setTime(to);
  603. toCal.setTime(from);
  604. }
  605. return toCal.getTimeInMillis() - fromCal.getTimeInMillis();
  606. }
  607. /**
  608. *
  609. * 获取Calendar实例
  610. *
  611. * @param date
  612. * 日期类型
  613. *
  614. * @return
  615. *
  616. */
  617. public static Calendar getCalendar(Date date) {
  618. final Calendar gc = Calendar.getInstance();
  619. gc.setTime(date);
  620. return gc;
  621. }
  622. /**
  623. *
  624. * 根据 yyyyMMdd HH:mm 日期格式,转换成数据库使用的时间戳格式
  625. *
  626. * @param strTimestamp
  627. * 以 yyyyMMdd HH:mm 格式的时间字符串
  628. *
  629. * @return 转换后的时间戳
  630. *
  631. */
  632. public static java.sql.Timestamp parseTimestamp(String strTimestamp) {
  633. return new java.sql.Timestamp(DateUtils.parseDateTime(strTimestamp).getTime());
  634. }
  635. /**
  636. *
  637. * 根据出生时间计算岁数
  638. * Date birthday
  639. * @return 年龄
  640. *
  641. */
  642. public static int getAgeByBirth(Date birthday) {
  643. int age = 0;
  644. try {
  645. final Calendar now = Calendar.getInstance();
  646. now.setTime(new Date());// 当前时间
  647. final Calendar birth = Calendar.getInstance();
  648. birth.setTime(birthday);
  649. if (birth.after(now)) {// 如果传入的时间,在当前时间的后面,返回0岁
  650. age = 0;
  651. } else {
  652. age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
  653. if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
  654. age += 1;
  655. }
  656. }
  657. return age;
  658. }
  659. catch (final Exception e) {// 兼容性更强,异常后返回数据
  660. return 0;
  661. }
  662. }
  663. }

发表评论

表情:
评论列表 (有 0 条评论,354人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java工具

    异常处理 什么是异常? 有异于常态,和正常状态不一样,有错误出现,阻止当前方法或作用域等等都称之为异常。 有什么常见异常? ![70][] 处理异常