日期时间帮助类,提供日期时间常用方法封装

àì夳堔傛蜴生んèń 2022-04-03 15:48 368阅读 0赞
  1. package cn.wtu.broadcast.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. /**
  9. *
  10. * @ClassName: DateUtil
  11. * @Description: 日期时间帮助类,提供日期时间常用方法封装
  12. * @author huangjiakui
  13. * @date 2018年12月04日
  14. *
  15. */
  16. public class DateUtil {
  17. public final static SimpleDateFormat sdFormat1 = new SimpleDateFormat("yyyy-MM-dd");
  18. public final static SimpleDateFormat sdFormat2 = new SimpleDateFormat("yyyyMMdd");
  19. public final static SimpleDateFormat sdFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  20. public final static SimpleDateFormat sdFormatSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  21. private static Logger logger = LoggerFactory.getLogger(DateUtil.class);
  22. public static String format(Date date, SimpleDateFormat sdf) {
  23. return sdf.format(date);
  24. }
  25. public static String getNow() {
  26. return sdFormat3.format(new Date());
  27. }
  28. public static String getDate(Date date) {
  29. return sdFormat1.format(date);
  30. }
  31. public static String getDateForInt(long i) {
  32. if (i > 0) {
  33. return sdFormat3.format(new Date(i * 1000L));
  34. } else {
  35. return "";
  36. }
  37. }
  38. public static Date parse(String date) {
  39. try {
  40. return sdFormat2.parse(date);
  41. } catch (ParseException e) {
  42. logger.error(e.getMessage(),e);
  43. return null;
  44. }
  45. }
  46. public static Date parse(String date, String format) {
  47. try {
  48. SimpleDateFormat sdf = new SimpleDateFormat(format);
  49. return sdf.parse(date);
  50. } catch (ParseException e) {
  51. logger.error(e.getMessage(),e);
  52. return null;
  53. }
  54. }
  55. /**
  56. * 获取当前日期是星期几<br>
  57. *
  58. * @param dt
  59. * @return 当前日期是星期几
  60. */
  61. public static int getWeekOfDate(Date dt) {
  62. Calendar cal = Calendar.getInstance();
  63. cal.setTime(dt);
  64. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  65. if (w < 0)
  66. w = 0;
  67. return w == 0 ? 7 : w;
  68. }
  69. /**
  70. * 获得 当前 或者参数时间是今年的第几周
  71. *
  72. * @param dt
  73. * @return
  74. */
  75. public static int getWeekIndex(Date dt) {
  76. if (dt == null) {
  77. dt = new Date();
  78. }
  79. SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  80. long time = 0;
  81. try {
  82. // 今年 1月1号 0点
  83. time = sdf.parse(sdf.format(dt)).getTime();
  84. } catch (ParseException e) {
  85. logger.error(e.getMessage(),e);
  86. }
  87. if (time == 0) {
  88. return -1;
  89. }
  90. // 从今年1月1号到现在过去的时间
  91. long lapsed = System.currentTimeMillis() - time + getWeekOfDate(new Date(time)) * 86400000;
  92. // 这个星期已经过去的时间
  93. long remainder = lapsed % (86400000 * 7);
  94. return (int) ((lapsed - remainder) / (86400000 * 7) + (remainder == 0 ? 0 : 1));
  95. }
  96. /**
  97. * 获得 榜单批次号
  98. *
  99. * @param time
  100. * 如果time为空 则返回当期批次号 如果是今年第一周 则检查去年的最后一周
  101. * @return
  102. */
  103. public static int getBatchid(long time) {
  104. // 本周是今年的第几周
  105. SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  106. int index = DateUtil.getWeekIndex(time == 0 ? null : new Date(time));
  107. int year = Integer.valueOf(sdf.format(time == 0 ? new Date() : new Date(time)));
  108. // 如果是第一周 则检查去年的最后一周
  109. if (index == 1) {
  110. index = DateUtil.getWeekIndex(time == 0 ? null : new Date(time - (8640000 * 7)));
  111. year -= 1;
  112. }
  113. return year * 100 + index;
  114. }
  115. /**
  116. *
  117. * @param date
  118. * 开始时间
  119. * @param addTime
  120. * 增加时间数量
  121. * @param unit
  122. * 时间单位 时 分 秒 日 月 年 等
  123. * @return
  124. */
  125. public static Date addTime(Date date, int addTime, int unit) {
  126. Calendar cal = Calendar.getInstance();
  127. cal.setTime(date);
  128. cal.add(unit, addTime);
  129. return cal.getTime();
  130. }
  131. /**
  132. * 获取时间date1与date2相差的秒数
  133. *
  134. * @param date1
  135. * 起始时间
  136. * @param date2
  137. * 结束时间
  138. * @return 返回相差的秒数
  139. */
  140. public static int getOffsetSeconds(Date date1, Date date2) {
  141. int seconds = (int) ((date2.getTime() - date1.getTime()) / 1000);
  142. return seconds;
  143. }
  144. /**
  145. * 获取时间date1与date2相差的分钟数
  146. *
  147. * @param date1
  148. * 起始时间
  149. * @param date2
  150. * 结束时间
  151. * @return 返回相差的分钟数
  152. */
  153. public static int getOffsetMinutes(Date date1, Date date2) {
  154. return getOffsetSeconds(date1, date2) / 60;
  155. }
  156. /**
  157. * 获取时间date1与date2相差的小时数
  158. *
  159. * @param date1
  160. * 起始时间
  161. * @param date2
  162. * 结束时间
  163. * @return 返回相差的小时数
  164. */
  165. public static int getOffsetHours(Date date1, Date date2) {
  166. return getOffsetMinutes(date1, date2) / 60;
  167. }
  168. /**
  169. * 获取时间date1与date2相差的天数数
  170. *
  171. * @param date1
  172. * 起始时间
  173. * @param date2
  174. * 结束时间
  175. * @return 返回相差的天数
  176. */
  177. public static int getOffsetDays(Date date1, Date date2) {
  178. return getOffsetHours(date1, date2) / 24;
  179. }
  180. /**
  181. * 获取时间date1与date2相差的周数
  182. *
  183. * @param date1
  184. * 起始时间
  185. * @param date2
  186. * 结束时间
  187. * @return 返回相差的周数
  188. */
  189. public static int getOffsetWeeks(Date date1, Date date2) {
  190. return getOffsetDays(date1, date2) / 7;
  191. }
  192. }

发表评论

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

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

相关阅读

    相关 MySQL日期时间函数

    日期和时间函数 可能的需求:   当前时间是多少、下个月的今天是星期几、统计截止到当前日期前 3 天的收入总和…… 上述需求就需要使用日期和时间函数来实现: ![111

    相关 日期时间

    在Java原本当中是提供了基础的Date和Calendar来进行时间日期的一些处理,但是嘞,这俩玩意真的不好用,所以在Java8中是进行了改进,提供了一套全新的时间日期工具。