使用joda-time封装的日期工具类

深碍√TFBOYSˉ_ 2023-06-30 09:14 76阅读 0赞

1、Joda-time简介
Joda-Time这个专门处理日期时间的库
2、类说明
Instant:不可变类,代表时间线上的一个瞬时的时间点
DateTime:不可变类,它以毫秒级的精度封装时间上的某个瞬间时刻,用来替换JDK的Calendar类
LocalDate:不可变类,该类封装了一个年/月/日的组合。没有时区信息
LocalTime:不可变类,表示一个本地的时间,而不包含日期部分。没有时区信息
LocalDateTime:不可变类,该类封装了一个年/月/日 时:分:秒的组合。没有时区信息
详细代码如下:
封装了常见的日期处理方式

  1. public class DateUtils {
  2. public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
  3. public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  4. public static final String FORMAT_CHINESE = "yyyy年M月d日";
  5. public static final String FORMAT_HH_MM = "HH:mm";
  6. public static final String FORMAT_YMDHMS = "yyyyMMdd hh:mm:ss";
  7. public static final String START_TIME = "00:00:00";
  8. public static final String END_TIME = "23:59:59";
  9. public static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(FORMAT_YYYY_MM_DD_HH_MM_SS);
  10. /** * 获取系统当前时间 返回 yyyy-MM-dd HH:mm:ss * @return */
  11. public static String getCurrentTime() {
  12. DateTime dt = new DateTime();
  13. String time = dt.toString(FORMAT_YYYY_MM_DD_HH_MM_SS);
  14. return time;
  15. }
  16. /** * 获取系统当前时间按照指定格式返回 * @param pattern * @return */
  17. public static String getCurrentTimePattern(String pattern) {
  18. DateTime dt = new DateTime();
  19. String time = dt.toString(pattern);
  20. return time;
  21. }
  22. /** * 获取当前日期 * @return yyyy-MM-dd */
  23. public static String getCurrentDate() {
  24. DateTime dt = new DateTime();
  25. String date = dt.toString(FORMAT_YYYY_MM_DD);
  26. return date;
  27. }
  28. /** * 获取当前日期按照指定格式 * @param pattern * @return */
  29. public static String getCurrentDatePattern(String pattern) {
  30. DateTime dt = new DateTime();
  31. String date = dt.toString(pattern);
  32. return date;
  33. }
  34. /** * 按照时区转换时间 * @param date * @param timeZone 时区 * @param pattern * @return */
  35. public static String format(Date date, TimeZone timeZone, String pattern) {
  36. if (date == null) {
  37. return null;
  38. }
  39. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  40. sdf.setTimeZone(timeZone);
  41. return sdf.format(date);
  42. }
  43. /** * 获取指定时间 * @param year 年 * @param month 月 * @param day 天 * @param hour 小时 * @param minute 分钟 * @param seconds 秒 * @return yyyy-MM-dd HH:mm:ss */
  44. public static String getPointTime(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer seconds) {
  45. DateTime dt = new DateTime(year, month, day, hour, minute, seconds);
  46. String date = dt.toString(FORMAT_YYYY_MM_DD_HH_MM_SS);
  47. return date;
  48. }
  49. /** * 获取指定时间 指定返回格式 * @param year 年 * @param month 月 * @param day 天 * @param hour 小时 * @param minute 分钟 * @param seconds 秒 * @param pattern 自定义格式 * @return pattern */
  50. public static String getPointTimePattern(Integer year, Integer month, Integer day, Integer hour,
  51. Integer minute, Integer seconds, String pattern) {
  52. DateTime dt = new DateTime(year, month, day, hour, minute, seconds);
  53. String date = dt.toString(pattern);
  54. return date;
  55. }
  56. /** * 获取指定日期 * @param year * @param month * @param day * @return */
  57. public static String getPointDate(Integer year, Integer month, Integer day) {
  58. LocalDate dt = new LocalDate(year, month, day);
  59. String date = dt.toString(FORMAT_YYYY_MM_DD);
  60. return date;
  61. }
  62. /** * 获取指定日期 返回指定格式 * @param year * @param month * @param day * @param pattern * @return */
  63. public static String getPointDatPattern(Integer year, Integer month, Integer day, String pattern) {
  64. LocalDate dt = new LocalDate(year, month, day);
  65. String date = dt.toString(pattern);
  66. return date;
  67. }
  68. /** * 获取当前是一周星期几 * @return */
  69. public static String getWeek() {
  70. DateTime dts = new DateTime();
  71. return DayWeekEnum.getNameByCode(dts.getDayOfWeek());
  72. }
  73. /** * 获取指定时间是一周的星期几 * @param year * @param month * @param day * @return */
  74. public static String getWeekPoint(Integer year, Integer month, Integer day) {
  75. LocalDate dts = new LocalDate(year, month, day);
  76. return DayWeekEnum.getNameByCode(dts.getDayOfWeek());
  77. }
  78. /** * 格式化日期 日期转为字符串 * @param date * @return yyyy-MM-dd HH:mm:ss */
  79. public static String format(Date date) {
  80. if (date == null) {
  81. return null;
  82. }
  83. SimpleDateFormat format = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
  84. return format.format(date);
  85. }
  86. /** * 日期转为字符串 指定格式 * @param date 日期 * @param pattern 日期格式 * @return */
  87. public static String format(Date date, String pattern) {
  88. if (date == null) {
  89. return null;
  90. }
  91. SimpleDateFormat format = new SimpleDateFormat(pattern);
  92. return format.format(date);
  93. }
  94. /** * 字符串转为日期 指定格式 * @param date 日期字符串 * @param pattern 日期格式 * @return */
  95. public static Date parse(String date, String pattern) {
  96. if (date == null) {
  97. return null;
  98. }
  99. Date resultDate = null;
  100. try {
  101. resultDate = new SimpleDateFormat(pattern).parse(date);
  102. } catch (ParseException e) {
  103. throw new GenericBusinessException(e);
  104. }
  105. return resultDate;
  106. }
  107. /** * 字符串转为日期 * @param date 日期字符串 * @return */
  108. public static Date parse(String date) {
  109. if (date == null) {
  110. return null;
  111. }
  112. try {
  113. return new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS).parse(date);
  114. } catch (ParseException e) {
  115. throw new GenericBusinessException(e);
  116. }
  117. }
  118. /** * 毫秒数转为字符串 按照指定格式转换 * @param timestamp * @return */
  119. public static String format(Long timestamp, String pattern) {
  120. String dateStr = "";
  121. if (null == timestamp || timestamp.longValue() < 0) {
  122. return dateStr;
  123. }
  124. try {
  125. Date date = new Date(timestamp);
  126. SimpleDateFormat format = new SimpleDateFormat(pattern);
  127. dateStr = format.format(date);
  128. } catch (Exception e) {
  129. throw new GenericBusinessException(e);
  130. }
  131. return dateStr;
  132. }
  133. /** *获取当前时间前几天时间,按指定格式返回 * @param days * @return */
  134. public static String forwardDay(Integer days, String format) {
  135. DateTime dt = new DateTime();
  136. DateTime y = dt.minusDays(days);
  137. return y.toString(format);
  138. }
  139. /** * 获取当前时间前几天时间 * @param days * @return */
  140. public static Date forwardDay(Integer days) {
  141. DateTime dt = new DateTime();
  142. DateTime y = dt.minusDays(days);
  143. return y.toDate();
  144. }
  145. /** * 计算两个时间相差多少天 * @param startDate * @param endDate * @return */
  146. public static Integer diffDay(Date startDate, Date endDate) {
  147. if (startDate == null || endDate == null) {
  148. return null;
  149. }
  150. DateTime dt1 = new DateTime(startDate);
  151. DateTime dt2 = new DateTime(endDate);
  152. int day = Days.daysBetween(dt1, dt2).getDays();
  153. return Math.abs(day);
  154. }
  155. /** * 获取指定间隔天数的日期 * @param date * @param offset * @return */
  156. public static Date addDay(Date date, int offset) {
  157. DateTime dt1;
  158. if (date == null) {
  159. dt1 = new DateTime().plusDays(offset);
  160. return dt1.toDate();
  161. }
  162. dt1 = new DateTime(date).plusDays(offset);
  163. return dt1.toDate();
  164. }
  165. /** * 获取日期的开始时间 * @param dateStr * @return */
  166. public static Date getDateStart(String dateStr) {
  167. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_YMDHMS);
  168. String subStr = dateStr.substring(0,8);
  169. try {
  170. Date date = simpleDateFormat.parse(subStr + " " + START_TIME);
  171. return date;
  172. } catch (ParseException e) {
  173. throw new GenericBusinessException(e);
  174. }
  175. }
  176. /** * 获取日期的结束时间 * @param dateStr * @return */
  177. public static Date getDateEnd(String dateStr) {
  178. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMAT_YMDHMS);
  179. String subStr = dateStr.substring(0,8);
  180. try {
  181. Date date = simpleDateFormat.parse(subStr + " " + END_TIME);
  182. return date;
  183. } catch (ParseException e) {
  184. throw new GenericBusinessException(e);
  185. }
  186. }

辛苦点赞
源码地址

发表评论

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

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

相关阅读

    相关 自己JDBC

    > 对JDBC进行一些简单的封装,可以方便在项目中调用,同时也减少了重复代码量,降低代码的冗余度,提高代码的可读性和美观。 package JdbcUtil