TimeUtil.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package api.common.util;
  2. import java.sql.Timestamp;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.*;
  6. public class TimeUtil {
  7. private static String[] dateFmtArr = new String[]{"yyyyMMdd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HHmmssSS", "HH:mm:ss", "yyyyMMddHHmmss"};
  8. public static long getNow() {
  9. return System.currentTimeMillis();
  10. }
  11. public static long getNowLong() {
  12. return System.currentTimeMillis();
  13. }
  14. public static String getNowString() {
  15. return System.currentTimeMillis() + "";
  16. }
  17. public static Timestamp getNowForMysql() {
  18. return new Timestamp(System.currentTimeMillis());
  19. }
  20. public static long zeroOfToday() {
  21. return System.currentTimeMillis() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
  22. }
  23. //获取时间类型格式转化
  24. public static Integer getRq(Date date, int index) {
  25. SimpleDateFormat sdf = new SimpleDateFormat(dateFmtArr[index]);
  26. if (date == null) {
  27. date = new Date();
  28. }
  29. return Integer.valueOf(sdf.format(date));
  30. }
  31. //获取时间类型格式转化
  32. public static String getToStringDate(Date date, int index) {
  33. SimpleDateFormat sdf = new SimpleDateFormat(dateFmtArr[index]);
  34. if (date == null) {
  35. date = new Date();
  36. }
  37. return sdf.format(date);
  38. }
  39. //获取过去某天的起始日期
  40. public static Map<String, Object> getPastTime(Integer num) {
  41. Calendar c = Calendar.getInstance();
  42. c.setTime(new Date());
  43. c.add(Calendar.DATE, -num);
  44. Date d = c.getTime();
  45. String startDate = getToStringDate(d, 2) + " 00:00:00";
  46. String endDate = getToStringDate(d, 2) + " 23:59:59";
  47. Map<String, Object> map = new HashMap();
  48. map.put("startDate", startDate);
  49. map.put("endDate", endDate);
  50. map.put("toDate", getToStringDate(d, 2));
  51. return map;
  52. }
  53. //获取间隔 n 秒的时间
  54. public static Timestamp getPostTimestamp(int secends) {
  55. return new Timestamp(System.currentTimeMillis() + secends * 1000);
  56. }
  57. //获取两个时间间隔
  58. public static String getTimeDifference(Date dateStart, Date dateStop) {
  59. String message = "";
  60. try {
  61. //毫秒ms
  62. long diff = dateStop.getTime() - dateStart.getTime();
  63. long diffSeconds = diff / 1000 % 60;
  64. long diffMinutes = diff / (60 * 1000) % 60;
  65. long diffHours = diff / (60 * 60 * 1000) % 24;
  66. long diffDays = diff / (24 * 60 * 60 * 1000);
  67. if (diffDays > 0) {
  68. message = diffDays + "天" + diffHours + "小时" + diffMinutes + "分钟" + diffSeconds + "秒";
  69. } else if (diffDays == 0 && diffHours != 0) {
  70. message = diffHours + "小时" + diffMinutes + "分钟" + diffSeconds + "秒";
  71. } else if (diffDays == 0 && diffHours == 0 && diffMinutes != 0) {
  72. message = diffMinutes + "分钟" + diffSeconds + "秒";
  73. } else if (diffDays == 0 && diffHours == 0 && diffMinutes == 0 && diffSeconds != 0) {
  74. message = diffSeconds + "秒";
  75. }
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. return message;
  80. }
  81. }