JAVA 常用时间工具类

我不是女神ヾ 2022-06-01 23:00 326阅读 0赞

不说了,直接看代码吧

  1. package com.poly.rbl.utils;
  2. import java.text.DateFormat;
  3. import java.text.ParseException;
  4. import java.text.ParsePosition;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. /**
  9. * 时间工具类
  10. *
  11. * @author gogym
  12. * @version 2017年8月30日
  13. * @see DateTimeUtil
  14. * @since
  15. */
  16. public class DateTimeUtil
  17. {
  18. /**
  19. * 获取当前系统时间
  20. *
  21. * @return
  22. * @author gogym
  23. * @date 2016-4-28 上午10:07:54
  24. * @comment
  25. */
  26. public static String getCurrentTime()
  27. {
  28. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
  29. String time = df.format(new Date());// new Date()为获取当前系统时间
  30. return time;
  31. }
  32. /**
  33. * 获取当前系统时间
  34. *
  35. * @return
  36. * @author gogym
  37. * @date 2016-4-28 上午10:07:54
  38. * @comment
  39. */
  40. public static Long getCurrentLongTime()
  41. {
  42. Long time = new Date().getTime();// new Date()为获取当前系统时间
  43. return time;
  44. }
  45. /**
  46. * date类型转String类型
  47. *
  48. * @param date
  49. * @return
  50. * @author gogym
  51. * @date 2016-4-28 上午10:10:25
  52. * @comment
  53. */
  54. public static String convertDateToString(Date date)
  55. {
  56. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
  57. return df.format(date);
  58. }
  59. /**
  60. * long类型转String类型
  61. *
  62. * @param date
  63. * @return
  64. * @author gogym
  65. * @date 2016-4-28 上午10:10:25
  66. * @comment
  67. */
  68. public static String convertLongToString(Long date)
  69. {
  70. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
  71. return df.format(new Date(date));
  72. }
  73. /**
  74. * String类型转date类型
  75. *
  76. * @param date
  77. * @return
  78. * @author gogym
  79. * @date 2016-4-28 上午10:10:25
  80. * @comment
  81. */
  82. public static Date convertStringToDate(String time)
  83. {
  84. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
  85. try
  86. {
  87. return df.parse(time);
  88. }
  89. catch (ParseException e)
  90. {
  91. e.printStackTrace();
  92. }
  93. return null;
  94. }
  95. /**
  96. * 获得指定时间的前一天
  97. *
  98. * @param specifiedDay
  99. * @return
  100. * @throws Exception
  101. */
  102. public static String getSpecifiedDayBefore(String specifiedDay)
  103. {
  104. Calendar c = Calendar.getInstance();
  105. Date date = null;
  106. try
  107. {
  108. date = new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(specifiedDay);
  109. }
  110. catch (ParseException e)
  111. {
  112. e.printStackTrace();
  113. }
  114. c.setTime(date);
  115. int day = c.get(Calendar.DATE);
  116. c.set(Calendar.DATE, day - 1);
  117. String dayBefore = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
  118. return dayBefore;
  119. }
  120. /**
  121. * 获得指定时间的后一天
  122. *
  123. * @param specifiedDay
  124. * @return
  125. */
  126. public static String getSpecifiedDayAfter(String specifiedDay)
  127. {
  128. Calendar c = Calendar.getInstance();
  129. Date date = null;
  130. try
  131. {
  132. date = new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(specifiedDay);
  133. }
  134. catch (ParseException e)
  135. {
  136. e.printStackTrace();
  137. }
  138. c.setTime(date);
  139. int day = c.get(Calendar.DATE);
  140. c.set(Calendar.DATE, day + 1);
  141. String dayAfter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
  142. return dayAfter;
  143. }
  144. /**
  145. * 获得指定时间的前一天
  146. *
  147. * @param specifiedDay
  148. * @return
  149. * @throws Exception
  150. */
  151. public static String getSpecifiedDayBefore(Date specifiedDay)
  152. {
  153. Calendar c = Calendar.getInstance();
  154. Date date = specifiedDay;
  155. c.setTime(date);
  156. int day = c.get(Calendar.DATE);
  157. c.set(Calendar.DATE, day - 1);
  158. String dayBefore = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
  159. return dayBefore;
  160. }
  161. /**
  162. * 获得指定时间的后一天
  163. *
  164. * @param specifiedDay
  165. * @return
  166. */
  167. public static String getSpecifiedDayAfter(Date specifiedDay)
  168. {
  169. Calendar c = Calendar.getInstance();
  170. Date date = specifiedDay;
  171. c.setTime(date);
  172. int day = c.get(Calendar.DATE);
  173. c.set(Calendar.DATE, day + 1);
  174. String dayAfter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
  175. return dayAfter;
  176. }
  177. /**
  178. * 获得当天0点时间
  179. *
  180. * @author:gj
  181. * @date: 2017/3/10
  182. * @time: 12:29
  183. **/
  184. public static Long getTimesmorning()
  185. {
  186. Calendar cal = Calendar.getInstance();
  187. cal.set(Calendar.HOUR_OF_DAY, 0);
  188. cal.set(Calendar.SECOND, 0);
  189. cal.set(Calendar.MINUTE, 0);
  190. cal.set(Calendar.MILLISECOND, 0);
  191. return cal.getTimeInMillis();
  192. }
  193. /**
  194. * 获取星期几
  195. *
  196. * @author:gj
  197. * @date: 2017/3/10
  198. * @time: 14:19
  199. **/
  200. public static String getWeekOfDate(Date dt)
  201. {
  202. String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
  203. Calendar cal = Calendar.getInstance();
  204. cal.setTime(dt);
  205. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  206. if (w < 0) w = 0;
  207. return weekDays[w];
  208. }
  209. /**
  210. * 获取时间点,类似微信
  211. *
  212. * @author:gj
  213. * @date: 2017/3/10
  214. * @time: 14:49
  215. **/
  216. public static String getTimePoint(String time)
  217. {
  218. Long Tmp = Long.valueOf(time);
  219. return getTimePoint(Tmp);
  220. }
  221. public static String getTimePoint(Long time)
  222. {
  223. // 现在时间
  224. Long now = new Date().getTime();
  225. DateFormat df;
  226. int day = (60 * 60 * 24) * 1000;
  227. String pointText = "1970-01-01";
  228. // 时间点比当天零点早
  229. if (time <= now && time != null)
  230. {
  231. Date date = new Date(time);
  232. if (time < getTimesmorning())
  233. {
  234. if (time >= getTimesmorning() - day)
  235. {// 比昨天零点晚
  236. pointText = "昨天";
  237. return pointText;
  238. }
  239. else
  240. {// 比昨天零点早
  241. if (time >= getTimesmorning() - 6 * day)
  242. {// 比七天前的零点晚,显示星期几
  243. return getWeekOfDate(date);
  244. }
  245. else
  246. {// 显示具体日期
  247. df = new SimpleDateFormat("yyyy-MM-dd");
  248. pointText = df.format(date);
  249. return pointText;
  250. }
  251. }
  252. }
  253. else
  254. {// 无日期时间,当天内具体时间
  255. df = new SimpleDateFormat("HH:mm");
  256. pointText = df.format(date);
  257. return pointText;
  258. }
  259. }
  260. return pointText;
  261. }
  262. /**
  263. * 获取时间间隔提示,类似微博
  264. *
  265. * @author:gj
  266. * @date: 2017/6/12
  267. * @time: 13:08
  268. **/
  269. public static String getInterval(Long t)
  270. {
  271. String interval = null;
  272. // 用现在距离1970年的时间间隔new Date().getTime()减去以前的时间距离1970年的时间间隔d1.getTime()得出的就是以前的时间与现在时间的时间间隔
  273. long time = new Date().getTime() - t;// 得出的时间间隔是毫秒
  274. if (time / 1000 < 10 && time / 1000 >= 0)
  275. {
  276. // 如果时间间隔小于10秒则显示“刚刚”time/10得出的时间间隔的单位是秒
  277. interval = "刚刚";
  278. }
  279. else if (time / 1000 < 60 && time / 1000 > 0)
  280. {
  281. // 如果时间间隔小于60秒则显示多少秒前
  282. int se = (int)((time % 60000) / 1000);
  283. interval = se + "秒前";
  284. }
  285. else if (time / 60000 < 60 && time / 60000 > 0)
  286. {
  287. // 如果时间间隔小于60分钟则显示多少分钟前
  288. int m = (int)((time % 3600000) / 60000);// 得出的时间间隔的单位是分钟
  289. interval = m + "分钟前";
  290. }
  291. else if (time / 3600000 < 24 && time / 3600000 >= 0)
  292. {
  293. // 如果时间间隔小于24小时则显示多少小时前
  294. int h = (int)(time / 3600000);// 得出的时间间隔的单位是小时
  295. interval = h + "小时前";
  296. }
  297. else if (time / 3600000 / 24 < 30 && time / 3600000 / 24 >= 0)
  298. {
  299. // 如果时间小于30天
  300. int h = (int)(time / 3600000 / 24);
  301. interval = h + "天前";
  302. }
  303. else
  304. {
  305. // 大于30天,则显示正常的时间,但是不显示秒
  306. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  307. ParsePosition pos2 = new ParsePosition(0);
  308. // Date d2 = (Date) sdf.parse(creattetime, pos2);
  309. Date d2 = new Date(t);
  310. interval = sdf.format(d2);
  311. }
  312. return interval;
  313. }
  314. public static String getInterval(String createtime)
  315. { // 传入的时间格式必须类似于2012-8-21 17:53:20这样的格式
  316. String interval = null;
  317. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  318. ParsePosition pos = new ParsePosition(0);
  319. Date d1 = (Date)sd.parse(createtime, pos);
  320. return getInterval(d1.getTime());
  321. }
  322. }

发表评论

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

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

相关阅读

    相关 JAVA工具

    引言 说起工具类,大家都不会陌生。常用的工具类有Apache 的Commons、 Google 的Guava、以及处理时间日期的Joda扩展包。那么本文主要来讲这几个工具

    相关 Java工具

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