DateUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package com.persagy.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import java.time.LocalDate;
  5. import java.time.LocalDateTime;
  6. import java.time.ZoneId;
  7. import java.time.ZonedDateTime;
  8. import java.time.format.DateTimeFormatter;
  9. import java.time.temporal.ChronoUnit;
  10. import java.time.temporal.TemporalUnit;
  11. import java.util.Date;
  12. /**
  13. * Description: 基于Java8的时间工具类 Company: Persagy
  14. *
  15. * @author luoguangyi
  16. * @version 1.0
  17. * @since: 2019年8月5日: 下午4:44:21 Update By luoguangyi 2019年8月5日: 下午4:44:21
  18. */
  19. @Slf4j
  20. public class DateUtils {
  21. private final static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
  22. /**
  23. * 东八区时区偏移量
  24. */
  25. private final static int ASIA_SHANGHAI = 8;
  26. public static final String sdfDay = "yyyyMMdd";
  27. public static final String sdfMonth = "yyyyMM";
  28. public static final String sdfTime = "yyyyMMddHHmmss";
  29. public static final String sdfTimeNotDate = "HHmmss";
  30. public static final String sdfHour = "yyyyMMddHH";
  31. public static final String sdfMinute = "yyyyMMddHHmm";
  32. // 时间格式-显示
  33. public final static String date_format_show = "yyyy-MM-dd HH:mm:ss";
  34. public final static String date_format_show_minute = "yyyy-MM-dd HH:mm";
  35. /**
  36. * LocalDate类型转为Date
  37. *
  38. * @param localDate LocalDate object
  39. * @return Date object
  40. */
  41. public static Date localDate2Date(LocalDate localDate) {
  42. ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
  43. return Date.from(zonedDateTime.toInstant());
  44. }
  45. /**
  46. * LocalDateTime类型转为Date
  47. *
  48. * @param localDateTime LocalDateTime object
  49. * @return Date object
  50. */
  51. public static Date localDateTime2Date(LocalDateTime localDateTime) {
  52. if (localDateTime == null) {
  53. return null;
  54. }
  55. return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
  56. }
  57. /**
  58. * Description: Date转换为LocalDateTime
  59. *
  60. * @param date
  61. * @return LocalDateTime
  62. * @author luoguangyi
  63. * @since 2019年8月5日: 下午5:25:27 Update By luoguangyi 2019年8月5日: 下午5:25:27
  64. */
  65. public static LocalDateTime date2LocalDateTime(Date date) {
  66. return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
  67. }
  68. // 获取指定日期的毫秒
  69. public static Long getMilliByLocalDateTime(LocalDateTime time) {
  70. return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  71. }
  72. /**
  73. * 获取时间戳
  74. *
  75. * @param dateTime
  76. * @return
  77. */
  78. public static Long getMilliByLocalDateTime(String dateTime) {
  79. return parse(dateTime).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
  80. }
  81. // 获取指定日期的秒
  82. public static Long getSecondsByLocalDateTime(LocalDateTime time) {
  83. return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
  84. }
  85. /**
  86. * 格式化LocalDateTime为指定格式 字符串
  87. *
  88. * @param pattern 格式
  89. * @return 日期字符串
  90. */
  91. public static String format(LocalDateTime time, String pattern) {
  92. return time.format(DateTimeFormatter.ofPattern(pattern));
  93. }
  94. public static String format(LocalDateTime time, DateTimeFormatter pattern) {
  95. return time.format(pattern);
  96. }
  97. public static String format(LocalDateTime time) {
  98. return time.format(FORMATTER);
  99. }
  100. public static String formatDate(Date date) {
  101. return format(date2LocalDateTime(date));
  102. }
  103. /**
  104. * 解析字符串日期为Date
  105. *
  106. * @param dateStr 日期字符串
  107. * @param pattern 格式
  108. * @return Date
  109. */
  110. public static LocalDateTime parse(String dateStr, String pattern) {
  111. return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
  112. }
  113. public static LocalDateTime parse(String dateStr, DateTimeFormatter pattern) {
  114. return LocalDateTime.parse(dateStr, pattern);
  115. }
  116. public static LocalDateTime parse(String dateStr) {
  117. return LocalDateTime.parse(dateStr, FORMATTER);
  118. }
  119. public static Date parseDate(String dateStr) {
  120. return localDateTime2Date(LocalDateTime.parse(dateStr, FORMATTER));
  121. }
  122. // 获取当前时间的指定格式
  123. public static String formatNow(String pattern) {
  124. return format(LocalDateTime.now(), pattern);
  125. }
  126. // 日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
  127. public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
  128. return time.plus(number, field);
  129. }
  130. // 日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
  131. public static LocalDateTime minus(LocalDateTime time, long number, TemporalUnit field) {
  132. return time.minus(number, field);
  133. }
  134. /**
  135. * 获取两个日期的差 field参数为ChronoUnit.* 默认累加的方式 重要说明: 计算月份差 计算的是否满月,年份同理,
  136. * 区别1:Period只考虑到日期,ChronoUnit.between()计算到具体时分秒
  137. * 区别2:Period只计算月份差,不考虑年份是否一样,ChronoUnit.between()计算考虑年份
  138. * :Period只考虑到日期,field.between(startTime, endTime)会计算到具体时间 period.getMonths();
  139. * field.between 比如:6月6号8点到8月6号7点: 2 1 比如:6月6号8点到8月6号9点: 2 2 比如:6月5号到8月6号: 2 2
  140. * 比如:6月7号到8月6号: 1 1
  141. *
  142. * @param startTime
  143. * @param endTime
  144. * @param field 单位(年月日时分秒)
  145. * @return
  146. */
  147. public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
  148. // Period period = Period.between(LocalDate.from(startTime),
  149. // LocalDate.from(endTime));
  150. // startTime : 1993-10-19
  151. // endTime : 2019-08-06
  152. // 年龄 : 25 年 9 月 18 日
  153. // period.getYears(); period.getMonths(); period.getDays();
  154. // 25 年 9 月 18 日
  155. // field.between(startTime, endTime);
  156. // 25 年 25*12+9 ( 25*12+9)个月+18 日
  157. return field.between(startTime, endTime);
  158. }
  159. /**
  160. * 获取间隔时间
  161. *
  162. * @param startTime
  163. * @param endTime
  164. * @return
  165. */
  166. public static long betweenTwoTime(String startTime, String endTime, ChronoUnit field) {
  167. return betweenTwoTime(parse(startTime), parse(endTime), field);
  168. }
  169. /**
  170. * 获取间隔秒数
  171. *
  172. * @param startTime
  173. * @param endTime
  174. * @return
  175. */
  176. public static long betweenTwoTimeSecond(String startTime, String endTime) {
  177. return betweenTwoTime(startTime, endTime, ChronoUnit.SECONDS);
  178. }
  179. /**
  180. * Description:获得当前时间的字符串.
  181. *
  182. * @return String 当前时间的字符串格式.格式是yyyyMMddHHmmss
  183. * @author lijie
  184. */
  185. public static String getNowTimeStr() {
  186. return LocalDateTime.now().format(FORMATTER);
  187. }
  188. public static String getTimeStr(LocalDateTime date) {
  189. return date.format(FORMATTER);
  190. }
  191. public static String getMinusHour(String dateTime, long hour) {
  192. LocalDateTime parse = parse(dateTime).minusHours(hour);
  193. return format(parse);
  194. }
  195. public static String getSdfTimeNotDate(String dateTime) {
  196. return dateTime.substring(8, 14);
  197. }
  198. public static String getDate(String dateTime) {
  199. return dateTime.substring(0, 8);
  200. }
  201. public static String getPlusHour(String dateTime, long hour) {
  202. LocalDateTime parse = parse(dateTime).plusHours(hour);
  203. return format(parse);
  204. }
  205. public static String getPlusDay(String dateTime, long days) {
  206. LocalDateTime parse = parse(dateTime).plusDays(days);
  207. return format(parse);
  208. }
  209. public static String getPlusHourNow(long hour) {
  210. LocalDateTime parse = LocalDateTime.now().plusHours(hour);
  211. return format(parse);
  212. }
  213. /**
  214. * 获取年
  215. *
  216. * @return 年
  217. */
  218. public static int getYear(LocalDateTime ldt) {
  219. return ldt.getYear();
  220. }
  221. /**
  222. * 获取月份
  223. */
  224. public static int getMonth(LocalDateTime ldt) {
  225. return ldt.getMonthValue();
  226. }
  227. /**
  228. * 获取月份差 不算满月的月份差(比如7月31号到8月1号仍然算一个月份差)
  229. */
  230. public static int getBetweenMonth(LocalDateTime startLdt, LocalDateTime endLdt) {
  231. int minusMonth = (endLdt.getYear() - startLdt.getYear()) * 12 - endLdt.getMonthValue()
  232. - startLdt.getMonthValue();
  233. return minusMonth;
  234. }
  235. /**
  236. * 获取月份最大天数
  237. */
  238. public static int getDaylengthOfMonth() {
  239. LocalDateTime localTime = LocalDateTime.now();
  240. return localTime.toLocalDate().lengthOfMonth();
  241. }
  242. /**
  243. * 获取年份最大天数
  244. */
  245. public static int getDaylengthOfYear() {
  246. LocalDateTime localTime = LocalDateTime.now();
  247. return localTime.toLocalDate().lengthOfYear();
  248. }
  249. /**
  250. * Description: 日期格式转换
  251. *
  252. * @param srcDateStr 源日期字符串
  253. * @param srcDatePattern 源日期字符串格式
  254. * @param destDatePattern 目标日期字符串格式
  255. * @return String
  256. * @author luoguangyi
  257. * @since 2019年10月9日: 下午2:29:23 Update By luoguangyi 2019年10月9日: 下午2:29:23
  258. */
  259. public static String transferDateFormat(String srcDateStr, String srcDatePattern, String destDatePattern) {
  260. DateTimeFormatter srcFommater = DateTimeFormatter.ofPattern(srcDatePattern);
  261. DateTimeFormatter destFommater = DateTimeFormatter.ofPattern(destDatePattern);
  262. return LocalDateTime.parse(srcDateStr, srcFommater).format(destFommater);
  263. }
  264. @SuppressWarnings("deprecation")
  265. public static void main(String[] args) {
  266. // System.out.println(getSdfTimeNotDate(sdfTime));
  267. // LocalDateTime parse1 = DateUtils.parse("20150612152611");
  268. // LocalDateTime parse2 = DateUtils.parse("20150613152610");
  269. // System.out.println(betweenTwoTime(parse1, parse2, ChronoUnit.HOURS));
  270. // test1();
  271. String time = "20210913162100";
  272. Date date = parseDate(time);
  273. System.out.println(date);
  274. }
  275. private static void test1() {
  276. String dateTime = "20201026113042";
  277. String condition = "{\"condition\":{\"effectTime\":{\"period\":{\"startTime\":\"090000\",\"endTime\":\"210000\"},\"type\":\"period\"},\"end\":\"supplyTemp < 0\",\"endUphold\":5,\"infoCode\":[\"supplyTemp\"],\"infoCodes\":[{\"meterId\":\"ACATFC_27_supplyTemp\",\"infoCode\":\"supplyTemp\",\"funcId\":\"901\"}],\"trigger\":\"supplyTemp > 0\",\"triggerUphold\":10},\"id\":\"01b7d0c5-c99e-4d4a-a11e-5961c6cb7b1e\",\"itemCode\":\"1051\",\"objId\":\"Eq1101050029058dda24eef14d1db12f398b5020877f\"}\n";
  278. JSONObject c = JSONObject.parseObject(condition).getJSONObject("condition");
  279. JSONObject effectTime = c.getJSONObject("effectTime");
  280. System.out.println(effectTime.getJSONObject("period").getString("startTime"));
  281. System.out.println(effectTime.getJSONObject("period").getString("startTime").substring(0, 6));
  282. if (
  283. "period".equals(effectTime.getString("type"))
  284. && "000000".equals(effectTime.getJSONObject("period").getString("startTime").substring(0, 6))
  285. && "235959".equals(effectTime.getJSONObject("period").getString("endTime").substring(0, 6))) {
  286. //架构师w确认 00:00-23:59的生效时间为一值生效,23:59分不过期
  287. }
  288. if ("period".equals(effectTime.getString("type"))
  289. && effectTime.getJSONObject("period").getString("startTime").compareTo(DateUtils.getSdfTimeNotDate(dateTime)) <= 0
  290. && effectTime.getJSONObject("period").getString("endTime").compareTo(DateUtils.getSdfTimeNotDate(dateTime)) >= 0) {
  291. //在生效时间
  292. } else {
  293. log.info("[{}]不在设置的生效时间段", dateTime);
  294. }
  295. }
  296. }