LocalDateTimeUtils.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package com.emato.cus.supervise.util;
  2. import java.time.*;
  3. import java.time.format.DateTimeFormatter;
  4. import java.time.temporal.ChronoUnit;
  5. import java.time.temporal.TemporalUnit;
  6. import java.util.Date;
  7. /**
  8. * @author Scott Chen
  9. * @version 1.0
  10. * 2017-10-28 10:47
  11. */
  12. public class LocalDateTimeUtils {
  13. public static final String DATA_TIME_HYPHEN = "yyyy-MM-dd HH:mm:ss";
  14. private LocalDateTimeUtils() { }
  15. //获取当前时间的LocalDateTime对象
  16. //LocalDateTime.now();
  17. //根据年月日构建LocalDateTime
  18. //LocalDateTime.of();
  19. //比较日期先后
  20. //LocalDateTime.now().isBefore(),
  21. //LocalDateTime.now().isAfter(),
  22. //Date转换为LocalDateTime
  23. public static LocalDateTime convertDateToLDT(Date date) {
  24. return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
  25. }
  26. //LocalDateTime转换为Date
  27. public static Date convertLDTToDate(LocalDateTime time) {
  28. return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
  29. }
  30. //获取指定日期的毫秒
  31. public static Long getMilliByTime(LocalDateTime time) {
  32. return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  33. }
  34. //获取指定日期的秒
  35. public static Long getSecondsByTime(LocalDateTime time) {
  36. return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
  37. }
  38. //获取指定时间的指定格式
  39. public static String formatTime(LocalDateTime time, String pattern) {
  40. return time.format(DateTimeFormatter.ofPattern(pattern));
  41. }
  42. //获取当前时间的指定格式
  43. public static String formatNow(String pattern) {
  44. return formatTime(LocalDateTime.now(), pattern);
  45. }
  46. //日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
  47. public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
  48. return time.plus(number, field);
  49. }
  50. //日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
  51. public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field) {
  52. return time.minus(number, field);
  53. }
  54. /**
  55. * 获取两个日期的差 field参数为ChronoUnit.*
  56. * @param startTime
  57. * @param endTime
  58. * @param field 单位(年月日时分秒)
  59. * @return
  60. */
  61. public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
  62. Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
  63. if (field == ChronoUnit.YEARS) {
  64. return period.getYears();
  65. }
  66. if (field == ChronoUnit.MONTHS) {
  67. return period.getYears() * 12 + period.getMonths();
  68. }
  69. return field.between(startTime, endTime);
  70. }
  71. //获取一天的开始时间,2017,7,22 00:00
  72. public static LocalDateTime getDayStart(LocalDateTime time) {
  73. return time.withHour(0)
  74. .withMinute(0)
  75. .withSecond(0)
  76. .withNano(0);
  77. }
  78. //获取一天的结束时间,2017,7,22 23:59:59.999999999
  79. public static LocalDateTime getDayEnd(LocalDateTime time) {
  80. return time.withHour(23)
  81. .withMinute(59)
  82. .withSecond(59)
  83. .withNano(999999999);
  84. }
  85. /**
  86. * String转LocalTime
  87. * @param src
  88. * @param formatter
  89. * @return
  90. */
  91. public static LocalTime stringToLocalTime(String src, DateTimeFormatter formatter) {
  92. return LocalTime.parse(src, formatter);
  93. }
  94. }