当前位置:   article > 正文

Java将毫秒(时间戳)转时分秒格式或者年月日,日期时间转时间戳,时间工具类、秒转时间,附实验结果_毫秒值转换为日期工具

毫秒值转换为日期工具

计算获得时间戳后转时分秒格式,附实验结果

  1. /**
  2. * @Author: Be.insighted
  3. * Description:
  4. * @date Create on 2020/7/14 16:35
  5. **/
  6. public class TimeUtils{  
  7. /**
  8.     * 把时间戳转换为:时分秒
  9.     *
  10.     * @param millisecond :毫秒,传入单位为毫秒
  11.     */
  12.        public static String getTimeString(final long millisecond) {
  13.        if (millisecond < 1000) {
  14.            return "0" + "秒";
  15.       }
  16.        long second = millisecond / 1000;
  17.        long seconds = second % 60;
  18.        long minutes = second / 60;
  19.        long hours = 0;
  20.        if (minutes >= 60) {
  21.            hours = minutes / 60;
  22.            minutes = minutes % 60;
  23.       }
  24.        String timeString = "";
  25.        String secondString = "";
  26.        String minuteString = "";
  27.        String hourString = "";
  28.        if (seconds < 10) {
  29.            secondString = "0" + seconds + "秒";
  30.       } else {
  31.            secondString = seconds + "秒";
  32.       }
  33.        if (minutes < 10 && hours < 1) {
  34.            minuteString = minutes + "分";
  35.       } else if (minutes < 10){
  36.            minuteString =  "0" + minutes + "分";
  37.       } else {
  38.            minuteString = minutes + "分";
  39.       }
  40.        if (hours < 10) {
  41.            hourString = hours + "时";
  42.       } else {
  43.            hourString = hours + "" + "时";
  44.       }
  45.        if (hours != 0) {
  46.            timeString = hourString + minuteString + secondString;
  47.       } else {
  48.            timeString = minuteString + secondString;
  49.       }
  50.        return timeString;
  51.   }
  52. public static Date convertTimeStamp2Date(long timeStamp) {
  53. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  54. Date date = new Date(timeStamp);
  55. try {
  56. date = df.parse(df.format(date));
  57. } catch (ParseException e) {
  58. log.error("时间格式化错误");
  59. }
  60. return date;
  61. }
  62. public static long dateToStamp(Date date, int hour, int minute, int second) {
  63. LocalDateTime timestamp = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
  64. return timestamp.withHour(hour).withMinute(minute).withSecond(second).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  65. }
  66. }

实验结果

时间工具类

  1. public class DateUtils {
  2. public static final String DATE_FORMAT_DATETIME_SIMPLE = "yyMMddHHmmss";
  3. /**
  4. * 年-月-日
  5. */
  6. public static final String DATE_FORMAT_DATEONLY = "yyyy-MM-dd";
  7. /**
  8. * 年-月-日
  9. */
  10. public static final String DATE_FORMAT_DATEONLY_MONTH = "yyyyMM";
  11. /**
  12. * 年-月-日 时分秒
  13. */
  14. public static final String DATETIME_FORMAT_DATEONLY = "yyyy-MM-dd HH:mm:ss";
  15. /**
  16. * 年-月-日 时分
  17. */
  18. public static final String DATE_FORMAT_DATE = "yyyy-MM-dd HH:mm";
  19. /**
  20. * dateTime formatter
  21. */
  22. public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern(DATETIME_FORMAT_DATEONLY);
  23. /**
  24. * date formatter
  25. */
  26. public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT_DATEONLY);
  27. private static final ZoneId ZONE = ZoneId.systemDefault();
  28. public static String formatDateTimeString(long unixTime, String format) {
  29. if (unixTime == 0) {
  30. return "";
  31. }
  32. LocalDateTime dateTime = toLocalDateTime(unixTime);
  33. Date date = date(dateTime);
  34. SimpleDateFormat formator = new SimpleDateFormat(format);
  35. return formator.format(date);
  36. }
  37. public static String formatDateTimeString(long unixTime) {
  38. return formatDateTimeString(unixTime, DATETIME_FORMAT_DATEONLY);
  39. }
  40. /**
  41. * 毫秒转为时间 时:分:秒
  42. *
  43. * @param second
  44. * @return
  45. */
  46. public static String getGapTime(long second) {
  47. if (second != 0L) {
  48. second = second / 1000;
  49. }
  50. //转换天数
  51. long days = second / 86400;
  52. //剩余秒数
  53. second = second % 86400;
  54. //转换小时数
  55. long hours = second / 3600;
  56. //剩余秒数
  57. second = second % 3600;
  58. //转换分钟
  59. long minutes = second / 60;
  60. //剩余秒数
  61. second = second % 60;
  62. String s = days + "天" + hours + "时" + minutes + "分" + second + "秒";
  63. if (0 < days) {
  64. return s;
  65. } else {
  66. return hours + "时" + minutes + "分" + second + "秒";
  67. }
  68. }
  69. /**
  70. * 获取String类型时间
  71. *
  72. * @return
  73. */
  74. public static String convertCalender2String() {
  75. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
  76. Calendar calendar = Calendar.getInstance();
  77. return df.format(calendar.getTime());
  78. }
  79. /**
  80. * java.time.LocalDateTime --> java.util.Date
  81. *
  82. * @param localDateTime
  83. * @return
  84. */
  85. public static Date date(LocalDateTime localDateTime) {
  86. if (localDateTime == null) {
  87. return null;
  88. }
  89. Instant instant = localDateTime.atZone(ZONE).toInstant();
  90. return Date.from(instant);
  91. }
  92. /**
  93. * 将字符串转换成日期,只到年月日
  94. * @param str
  95. * @return
  96. */
  97. public static Date strToDate(String str, String format) {
  98. try {
  99. if (StringUtils.isBlank(format)) {
  100. format = DATETIME_FORMAT_DATEONLY;
  101. }
  102. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
  103. return simpleDateFormat.parse(str);
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. return null;
  108. }
  109. /**
  110. * 将Date日期转换成String
  111. *
  112. * @param dateDate
  113. * @return
  114. */
  115. public static String dateToStr(Date dateDate, String formatter) {
  116. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
  117. String dateString = simpleDateFormat.format(dateDate);
  118. return dateString;
  119. }
  120. /**
  121. * 将Date日期转换成String
  122. *
  123. * @param dateDate
  124. * @return
  125. */
  126. public static String dateToStr(Date dateDate) {
  127. SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_DATEONLY);
  128. String dateString = formatter.format(dateDate);
  129. return dateString;
  130. }
  131. public static LocalDateTime toLocalDateTime(Date date){
  132. Instant instant = date.toInstant();
  133. ZoneId zone = ZoneId.systemDefault();
  134. return LocalDateTime.ofInstant(instant, zone);
  135. }
  136. public static LocalDateTime toLocalDateTime(long dateLong){
  137. return toLocalDateTime(new Date(dateLong));
  138. }
  139. public static Date toDate(LocalDateTime localDateTime){
  140. return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
  141. }
  142. public static Date toDate(LocalDate localDate){
  143. return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
  144. }
  145. public static Date toDate(LocalTime localTime) {
  146. LocalDate localDate = LocalDate.now();
  147. LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
  148. ZoneId zone = ZoneId.systemDefault();
  149. Instant instant = localDateTime.atZone(zone).toInstant();
  150. return Date.from(instant);
  151. }
  152. public static long toLong(LocalDateTime localDateTime){
  153. return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  154. }
  155. public static long toLong(LocalTime localTime){
  156. LocalDate localDate = LocalDate.now();
  157. LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
  158. return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  159. }
  160. public static long toLong(LocalDate localDate){
  161. LocalDateTime localDateTime = localDate.atStartOfDay();
  162. return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  163. }
  164. public static boolean isToday(LocalDateTime datetime) {
  165. LocalDateTime now = LocalDateTime.now();
  166. return (now.getYear() == datetime.getYear()) && (now.getMonthValue() == datetime.getMonthValue()) && (now.getDayOfMonth() == datetime.getDayOfMonth());
  167. }
  168. }

秒转时间

  1. public static String secondToTime(int seconds) {
  2. if (seconds < 0) {
  3. throw new IllegalArgumentException("Seconds must be a positive number!");
  4. } else {
  5. int hour = seconds / 3600;
  6. int other = seconds % 3600;
  7. int minute = other / 60;
  8. int second = other % 60;
  9. StringBuilder sb = new StringBuilder();
  10. if (hour < 10) {
  11. sb.append("0");
  12. }
  13. sb.append(hour);
  14. sb.append(":");
  15. if (minute < 10) {
  16. sb.append("0");
  17. }
  18. sb.append(minute);
  19. sb.append(":");
  20. if (second < 10) {
  21. sb.append("0");
  22. }
  23. sb.append(second);
  24. return sb.toString();
  25. }
  26. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/72288
推荐阅读
相关标签
  

闽ICP备14008679号