java 时间工具类

朱雀 2022-06-12 09:54 327阅读 0赞
  1. ackage com.lion.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.GregorianCalendar;
  7. /**
  8. * 时间操作工具
  9. * @author ven
  10. *
  11. */
  12. public class TimeUtil {
  13. private static final String DATETIME = "yyyy-MM-dd HH:mm:ss";
  14. private static final String DATE = "yyyy-MM-dd";
  15. private static final String TIME = "HH:mm:dd";
  16. private static final String YEAR = "yyyy";
  17. private static final String MONTH = "MM";
  18. private static final String DAY = "dd";
  19. private static final String HOUR = "HH";
  20. private static final String MINUTE = "mm";
  21. private static final String SEC = "ss";
  22. private static final String DATETIMECHINESE = "yyyy年MM月dd日 HH时mm分ss秒";
  23. private static final String DATECHINESE = "yyyy年MM月dd日";
  24. private static final String SIMPLEDATECHINESE = "MM月dd日";
  25. /**
  26. * 判断一个字符串日期是否过期
  27. *
  28. * @param dateTime
  29. * @return (int) 过期返回1,不过期返回0
  30. * @throws ParseException
  31. */
  32. public static int isOutOfDate(String dateTime) throws ParseException {
  33. long nowTimeLong = new Date().getTime();
  34. long ckTimeLong = new SimpleDateFormat(DATETIME).parse(dateTime)
  35. .getTime();
  36. if (nowTimeLong - ckTimeLong > 0) {// 过期
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. /**
  42. * 判断是否在一个起止日期内<br/>
  43. * 例如:2012-04-05 00:00:00~2012-04-15 00:00:00
  44. *
  45. * @param start_time
  46. * @param over_time
  47. * @return (int) 在这个时间段内返回1,不在返回0
  48. * @throws ParseException
  49. */
  50. public static int isOutOfDate(String start_time, String over_time)
  51. throws ParseException {
  52. long nowTimeLong = new Date().getTime();
  53. long ckStartTimeLong = new SimpleDateFormat(DATETIME).parse(start_time)
  54. .getTime();
  55. long ckOverTimeLong = new SimpleDateFormat(DATETIME).parse(over_time)
  56. .getTime();
  57. if (nowTimeLong > ckStartTimeLong && nowTimeLong < ckOverTimeLong) {
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. /**
  63. * 判断一个自定义日期是否在一个起止日期内<br/>
  64. * 例如:判断2012-01-05 00:00:00是否在2012-04-05 00:00:00~2012-04-15 00:00:00
  65. *
  66. * @param start_time
  67. * @param over_time
  68. * @return (int) 在这个时间段内返回1,不在返回0
  69. * @throws ParseException
  70. */
  71. public static int isOutOfDate(String time, String start_time,
  72. String over_time) throws ParseException {
  73. long timeLong = new SimpleDateFormat(DATETIME).parse(time).getTime();
  74. long ckStartTimeLong = new SimpleDateFormat(DATETIME).parse(start_time)
  75. .getTime();
  76. long ckOverTimeLong = new SimpleDateFormat(DATETIME).parse(over_time)
  77. .getTime();
  78. if (timeLong > ckStartTimeLong && timeLong < ckOverTimeLong) {
  79. return 1;
  80. }
  81. return 0;
  82. }
  83. /**
  84. * 判断是否在一个时间段内<br/>
  85. * 例如:8:00~10:00
  86. *
  87. * @param time_limit_start
  88. * @param time_limit_over
  89. * @return (int) 1在这个时间段内,0不在这个时间段内
  90. * @throws ParseException
  91. */
  92. public static int isInTime(String time_limit_start, String time_limit_over)
  93. throws ParseException {
  94. // 获取当前日期
  95. String nowDate = new SimpleDateFormat(DATE).format(new Date());
  96. return isOutOfDate(nowDate + " " + time_limit_start, nowDate + " "
  97. + time_limit_over);
  98. }
  99. /**
  100. * 判断一个自定义时间是否在一个时间段内<br/>
  101. * 例如:判断02:00是否在08:00~10:00时间段内
  102. *
  103. * @param time_limit_start
  104. * @param time_limit_over
  105. * @return (int) 1在这个时间段内,0不在这个时间段内
  106. * @throws ParseException
  107. */
  108. public static int isInTime(String time, String time_limit_start,
  109. String time_limit_over) throws ParseException {
  110. String nowDate = new SimpleDateFormat(DATE).format(new Date());
  111. return isOutOfDate(nowDate + " " + time, nowDate + " "
  112. + time_limit_start, nowDate + " " + time_limit_over);
  113. }
  114. /**
  115. * 取得自定义月份后的日期,如13个月以后的时间
  116. *
  117. * @param monthNum
  118. * 往后几个月
  119. * @return 时间字符串
  120. */
  121. public static String crateTimeFromNowTimeByMonth(int monthNum) {
  122. Calendar calendar = new GregorianCalendar(Integer.parseInt(getYear()),
  123. Integer.parseInt(getMonth()) - 1, Integer.parseInt(getDay()),
  124. Integer.parseInt(getHour()), Integer.parseInt(getMinute()),
  125. Integer.parseInt(getSec()));
  126. calendar.add(Calendar.MONTH, monthNum);
  127. return new SimpleDateFormat(DATETIME).format(calendar.getTime());
  128. }
  129. /**
  130. * 取得自定义天数后的日期,如13天以后的时间
  131. *
  132. * @param dayNum
  133. * 往后几天
  134. * @return 时间字符串(DateTime)
  135. */
  136. public static String crateTimeFromNowTimeByDay(int dayNum) {
  137. Calendar calendar = new GregorianCalendar(Integer.parseInt(getYear()),
  138. Integer.parseInt(getMonth()) - 1, Integer.parseInt(getDay()),
  139. Integer.parseInt(getHour()), Integer.parseInt(getMinute()),
  140. Integer.parseInt(getSec()));
  141. calendar.add(Calendar.DATE, dayNum);
  142. return new SimpleDateFormat(DATETIME).format(calendar.getTime());
  143. }
  144. /**
  145. * 取得自定义天数后的日期,如13天以后的时间
  146. *
  147. * @param dayNum
  148. * 往后几天
  149. * @return 时间字符串(Date)
  150. */
  151. public static String crateTimeFromNowDayByDay(int dayNum) {
  152. Calendar calendar = new GregorianCalendar(Integer.parseInt(getYear()),
  153. Integer.parseInt(getMonth()) - 1, Integer.parseInt(getDay()),
  154. Integer.parseInt(getHour()), Integer.parseInt(getMinute()),
  155. Integer.parseInt(getSec()));
  156. calendar.add(Calendar.DATE, dayNum);
  157. return new SimpleDateFormat(DATE).format(calendar.getTime());
  158. }
  159. /**
  160. * 取得自定义时间后再过几分钟的时间,如12:05以后5分钟的时间
  161. *
  162. * @param dayNum
  163. * 往后几天
  164. * @return 时间字符串(Date)
  165. */
  166. public static String crateTimeFromNowDayByTime(int timeNum) {
  167. Calendar calendar = new GregorianCalendar(Integer.parseInt(getYear()),
  168. Integer.parseInt(getMonth()) - 1, Integer.parseInt(getDay()),
  169. Integer.parseInt(getHour()), Integer.parseInt(getMinute()),
  170. Integer.parseInt(getSec()));
  171. calendar.add(Calendar.MINUTE, timeNum);
  172. return new SimpleDateFormat(DATETIME).format(calendar.getTime());
  173. }
  174. /**
  175. * 计算两个时间间隔(精确到分钟)
  176. *
  177. * @param startDay
  178. * 开始日(整型):0表示当日,1表示明日
  179. * @param startTime
  180. * 开始时间(24h):00:00
  181. * @param endDay
  182. * 结束日(整型):0表示当日,1表示明日,限制:大于等于 startDay
  183. * @param endTime
  184. * 结束时间(24h):23:50
  185. * @return 格式化的日期格式:DD天HH小时mm分
  186. */
  187. public static String calculateIntervalTime(int startDay, String startTime,
  188. int endDay, String endTime) {
  189. int day = endDay - startDay;
  190. int hour = 0;
  191. int mm = 0;
  192. if (day < 0) {
  193. return null;
  194. } else {
  195. int sh = Integer.valueOf(startTime.split(":")[0]);
  196. int eh = Integer.valueOf(endTime.split(":")[0]);
  197. int sm = Integer.valueOf(startTime.split(":")[1]);
  198. int em = Integer.valueOf(endTime.split(":")[1]);
  199. hour = eh - sh;
  200. if (hour > 0) {
  201. mm = em - sm;
  202. if (mm < 0) {
  203. hour--;
  204. mm = 60 + mm;
  205. }
  206. } else {
  207. day = day - 1;
  208. hour = 24 + hour;
  209. mm = em - sm;
  210. if (mm < 0) {
  211. hour--;
  212. mm = 60 + mm;
  213. }
  214. }
  215. }
  216. if (hour == 24) {
  217. day++;
  218. hour = 0;
  219. }
  220. if (day != 0) {
  221. return day + "天" + hour + "小时" + mm + "分";
  222. } else {
  223. return hour + "小时" + mm + "分";
  224. }
  225. }
  226. /**
  227. * 计算两个时间差
  228. *
  229. * @param startTime
  230. * @param endTime
  231. * @return long
  232. * @throws ParseException
  233. */
  234. public static long calculateIntervalTime(String startTime, String endTime)
  235. throws ParseException {
  236. return parseDateTime(endTime).getTime()
  237. - parseDateTime(startTime).getTime();
  238. }
  239. // 字符串转换成时间
  240. public static Date parseDateTime(String datetime) throws ParseException {
  241. SimpleDateFormat sdf = new SimpleDateFormat(DATETIME);
  242. return sdf.parse(datetime);
  243. }
  244. // 获取当前详细日期时间
  245. public static String getDateTime() {
  246. return new SimpleDateFormat(DATETIME).format(new Date());
  247. }
  248. // 转换为中文时间
  249. public static String getChineseDateTime() {
  250. return new SimpleDateFormat(DATETIMECHINESE).format(new Date());
  251. }
  252. // 转换为中文时间
  253. public static String getChineseDate() {
  254. return new SimpleDateFormat(DATECHINESE).format(new Date());
  255. }
  256. // 转换为中文时间
  257. public static String getSimpleChineseDate() {
  258. return new SimpleDateFormat(SIMPLEDATECHINESE).format(new Date());
  259. }
  260. // 转换为中文时间 如果num为-1表示前一天 1为后一天 0为当天
  261. public static String getSimpleChineseDate(int num) {
  262. Date d = new Date();
  263. try {
  264. d = parseDateTime(crateTimeFromNowTimeByDay(num));
  265. } catch (ParseException e) {
  266. e.printStackTrace();
  267. }
  268. return new SimpleDateFormat(SIMPLEDATECHINESE).format(d);
  269. }
  270. // 获取当前时间
  271. public static String getTime() {
  272. return new SimpleDateFormat(TIME).format(new Date());
  273. }
  274. // 获取当前年
  275. public static String getYear() {
  276. return new SimpleDateFormat(YEAR).format(new Date());
  277. }
  278. // 获取当前月
  279. public static String getMonth() {
  280. return new SimpleDateFormat(MONTH).format(new Date());
  281. }
  282. // 获取当前日
  283. public static String getDay() {
  284. return new SimpleDateFormat(DAY).format(new Date());
  285. }
  286. // 获取当前时
  287. public static String getHour() {
  288. return new SimpleDateFormat(HOUR).format(new Date());
  289. }
  290. // 获取当前分
  291. public static String getMinute() {
  292. return new SimpleDateFormat(MINUTE).format(new Date());
  293. }
  294. // 获取当前秒
  295. public static String getSec() {
  296. return new SimpleDateFormat(SEC).format(new Date());
  297. }
  298. // 获取昨天日期
  299. public static String getYestday() {
  300. Calendar cal = Calendar.getInstance();
  301. cal.add(Calendar.DATE, -1);
  302. Date d = cal.getTime();
  303. return new SimpleDateFormat(DATETIME).format(d);// 获取昨天日期
  304. }
  305. public static String getMonday() {
  306. Calendar calendar = new GregorianCalendar();
  307. // 取得本周一
  308. calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
  309. calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  310. calendar.setFirstDayOfWeek(Calendar.MONDAY);
  311. calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  312. return new SimpleDateFormat(DATETIME).format(calendar.getTime());// 获取昨天日期
  313. }
  314. }

发表评论

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

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

相关阅读

    相关 Java 时间工具

    借鉴网上资源与工作中经验整理的时间工具类(主要针对java.util.Date的使用,java8后的时间工具下期整理),欢迎大家一起完善补充。 import

    相关 Java时间工具

    该时间工具类功能:时间戳格式化至毫秒、时间戳格式化至秒、时间戳格式化至日、时间戳格式化成时和分、Date对象格式化至毫秒、Date对象格式化至秒、Date对象格式化至日、Da

    相关 java时间工具

    在项目中,很多地方需要根据时间获取相应的数据,将时间格式化,或者时间比较等相关操作。一个良好的工具类不仅可以减少代码冗余,还能促进业务处理,加快进度。 /