java工具类------日期工具类

不念不忘少年蓝@ 2022-06-18 08:13 529阅读 0赞

1.不多说直接上代码,小伙伴们可以看注释,代码如下:

  1. import java.text.DateFormat;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. /**
  7. * @ClassName: DateUtil
  8. * @Description: 日期工具类
  9. * @author wangXi
  10. * @date
  11. */
  12. public class DateUtil
  13. {
  14. /**
  15. * @Title: formatDate
  16. * @Description: 格式化日期
  17. * @param date
  18. * @param formatString
  19. * @return String
  20. */
  21. public static String formatDate(Date date, String formatString)
  22. {
  23. SimpleDateFormat format = new SimpleDateFormat(formatString);
  24. return format.format(date);
  25. }
  26. /**
  27. * @Title: fromStringToDate
  28. * @Description: 自定义日期,注意 日期字符串需和fomat保持一致
  29. * @param fomat
  30. * 例:yyyy-MM-dd
  31. * @param dateStr
  32. * 例:2015-01-01
  33. * @return Date
  34. * @throws ParseException
  35. */
  36. public static Date fromStringToDate(String fomat, String dateStr)
  37. throws ParseException
  38. {
  39. Date date = null;
  40. date = new SimpleDateFormat(fomat).parse(dateStr);
  41. return date;
  42. }
  43. /**
  44. * @Title: currentDayMinus
  45. * @Description: 根据系统参数对当前日期进行相减操作
  46. * @param formatString 日期格式
  47. * @param days
  48. * @return String 当前日期相减后的日期
  49. */
  50. @SuppressWarnings("static-access")
  51. public static String currentDayMinus(int days, String formatString)
  52. {
  53. DateFormat format = new SimpleDateFormat(formatString);
  54. Date date = new Date();
  55. Calendar c = Calendar.getInstance();
  56. c.setTime(date);
  57. c.add(c.DATE, -days);
  58. Date tempDate = c.getTime();
  59. String currentDayMinus = format.format(tempDate);
  60. return currentDayMinus;
  61. }
  62. /**
  63. * @Title: currentDayMinus
  64. * @Description: 根据系统参数对当前日期进行相加操作
  65. * @param formatString
  66. * 日期格式
  67. * @param days
  68. * @return String 当前日期相加后的日期
  69. */
  70. @SuppressWarnings("static-access")
  71. public static String currentDayAddition(int days, String formatString)
  72. {
  73. DateFormat format = new SimpleDateFormat(formatString);
  74. Date date = new Date();
  75. Calendar c = Calendar.getInstance();
  76. c.setTime(date);
  77. c.add(c.DATE, +days);
  78. Date tempDate = c.getTime();
  79. String currentDayMinus = format.format(tempDate);
  80. return currentDayMinus;
  81. }
  82. /**
  83. * @Title: currentDayAdditionReturnDate
  84. * @Description: 当前日期加n天,返回Date类型
  85. * @param days
  86. * @return Date
  87. */
  88. public static Date currentDayAdditionReturnDate(Date date, int days)
  89. {
  90. // Date date = new Date();
  91. Calendar c = Calendar.getInstance();
  92. c.setTime(date);
  93. c.add(Calendar.DATE, +days);
  94. Date tempDate = c.getTime();
  95. // String currentDayMinus = format.format(tempDate);
  96. return tempDate;
  97. }
  98. /**
  99. * @Title: currentSecondPlus
  100. * @Description: 对当前日期进行秒的加减运算
  101. * @param second
  102. * 与当前时间的差
  103. * @param formatString
  104. * @return String
  105. */
  106. @SuppressWarnings("static-access")
  107. public static String currentSecondPlus(int second, String formatString)
  108. {
  109. DateFormat format = new SimpleDateFormat(formatString);
  110. Date date = new Date();
  111. Calendar c = Calendar.getInstance();
  112. c.setTime(date);
  113. c.add(c.SECOND, second);
  114. Date tempDate = c.getTime();
  115. String restul = format.format(tempDate);
  116. return restul;
  117. }
  118. /**
  119. * @Title: currentDateMinusMin
  120. * @Description: 当前日期
  121. * @param minutes
  122. * @return Date
  123. */
  124. public static Date currentDateMinusMin(int minutes)
  125. {
  126. Date date = new Date();
  127. Calendar c = Calendar.getInstance();
  128. c.setTime(date);
  129. c.add(Calendar.MINUTE, -minutes);
  130. Date tempDate = c.getTime();
  131. return tempDate;
  132. }
  133. /**
  134. * @Title: getCurrentDate
  135. * @Description: 获取当前日期,不带时间
  136. * @return
  137. * @throws ParseException
  138. */
  139. public static Date getCurrentDate() throws ParseException
  140. {
  141. Date today = new Date();
  142. String todayStr = DateUtil.formatDate(today, "yyyy.MM.dd");
  143. Date currentDate = DateUtil.fromStringToDate("yyyy.MM.dd", todayStr);
  144. return currentDate;
  145. }
  146. /**
  147. * @Title: removeTimeInDate
  148. * @Description: 删除日期中的时间
  149. * @param date
  150. * @return Date
  151. * @throws ParseException
  152. */
  153. public static Date removeTimeInDate(Date date) throws ParseException
  154. {
  155. String dateStr = DateUtil.formatDate(date, "yyyy.MM.dd");
  156. Date newDate = DateUtil.fromStringToDate("yyyy.MM.dd", dateStr);
  157. return newDate;
  158. }
  159. /**
  160. * @Title: dateMinus
  161. * @Description: 两日期相减
  162. * @param endDate
  163. * @param currentDate
  164. * @return double
  165. */
  166. public static double dateMinus(Date endDate, Date currentDate)
  167. {
  168. double longTime = endDate.getTime() - currentDate.getTime();
  169. double day = longTime / 1000 / 60 / 60 / 24;
  170. return day;
  171. }
  172. /**
  173. * @Title: getCurrentDateTime
  174. * @Description: 获取当前时间
  175. * @return String
  176. */
  177. public static String getCurrentDateTime()
  178. {
  179. DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  180. return format.format(new Date());
  181. }
  182. /**
  183. * @Title: getCurrentDateStr
  184. * @Description: 获取当前日期串
  185. * @return
  186. */
  187. public static String getCurrentDateStr()
  188. {
  189. DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  190. return format.format(new Date());
  191. }
  192. /**
  193. * @Title: getCurrentDateTime
  194. * @Description: 获取当前时间
  195. * @return String
  196. */
  197. public static String getCurrentDateTimeByFomat(String fomat)
  198. {
  199. DateFormat format = new SimpleDateFormat(fomat);
  200. return format.format(new Date());
  201. }
  202. /**
  203. * @Title: getFirstDayOfLastMonth
  204. * @Description: 获取某日期 前n个月的第一天
  205. * @return
  206. * @throws ParseException
  207. */
  208. public static Date getFirstDayOfLastMonth(Date date, int n)
  209. throws ParseException
  210. {
  211. // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  212. // 获取前月的第一天
  213. Calendar cal_1 = Calendar.getInstance();// 获取当前日期
  214. cal_1.setTime(date);
  215. cal_1.add(Calendar.MONTH, -n);
  216. cal_1.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
  217. Date result = cal_1.getTime();
  218. // String firstDay = format.format(cal_1.getTime());
  219. return removeTimeInDate(result);
  220. }
  221. }

至此,日期方法总结完毕了,该工具类基本上能够满足小伙伴们在开发中遇到的日期问题,希望对大家有帮助!

发表评论

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

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

相关阅读

    相关 Java 日期工具

            项目中的订单统计需求,分别是按日,周,月,年统计订单数量及订单总额。其中,遇到了各种时间的获取,下面总结了一些较全的Java获取不同时间以及不同时间格式的相互