123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- package com.persagy.utils;
- import com.alibaba.fastjson.JSONObject;
- import lombok.extern.slf4j.Slf4j;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
- import java.time.format.DateTimeFormatter;
- import java.time.temporal.ChronoUnit;
- import java.time.temporal.TemporalUnit;
- import java.util.Date;
- /**
- * Description: 基于Java8的时间工具类 Company: Persagy
- *
- * @author luoguangyi
- * @version 1.0
- * @since: 2019年8月5日: 下午4:44:21 Update By luoguangyi 2019年8月5日: 下午4:44:21
- */
- @Slf4j
- public class DateUtils {
- private final static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
- /**
- * 东八区时区偏移量
- */
- private final static int ASIA_SHANGHAI = 8;
- public static final String sdfDay = "yyyyMMdd";
- public static final String sdfMonth = "yyyyMM";
- public static final String sdfTime = "yyyyMMddHHmmss";
- public static final String sdfTimeNotDate = "HHmmss";
- public static final String sdfHour = "yyyyMMddHH";
- public static final String sdfMinute = "yyyyMMddHHmm";
- // 时间格式-显示
- public final static String date_format_show = "yyyy-MM-dd HH:mm:ss";
- public final static String date_format_show_minute = "yyyy-MM-dd HH:mm";
- /**
- * LocalDate类型转为Date
- *
- * @param localDate LocalDate object
- * @return Date object
- */
- public static Date localDate2Date(LocalDate localDate) {
- ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
- return Date.from(zonedDateTime.toInstant());
- }
- /**
- * LocalDateTime类型转为Date
- *
- * @param localDateTime LocalDateTime object
- * @return Date object
- */
- public static Date localDateTime2Date(LocalDateTime localDateTime) {
- if (localDateTime == null) {
- return null;
- }
- return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
- }
- /**
- * Description: Date转换为LocalDateTime
- *
- * @param date
- * @return LocalDateTime
- * @author luoguangyi
- * @since 2019年8月5日: 下午5:25:27 Update By luoguangyi 2019年8月5日: 下午5:25:27
- */
- public static LocalDateTime date2LocalDateTime(Date date) {
- return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
- }
- // 获取指定日期的毫秒
- public static Long getMilliByLocalDateTime(LocalDateTime time) {
- return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
- }
- /**
- * 获取时间戳
- *
- * @param dateTime
- * @return
- */
- public static Long getMilliByLocalDateTime(String dateTime) {
- return parse(dateTime).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
- }
- // 获取指定日期的秒
- public static Long getSecondsByLocalDateTime(LocalDateTime time) {
- return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
- }
- /**
- * 格式化LocalDateTime为指定格式 字符串
- *
- * @param pattern 格式
- * @return 日期字符串
- */
- public static String format(LocalDateTime time, String pattern) {
- return time.format(DateTimeFormatter.ofPattern(pattern));
- }
- public static String format(LocalDateTime time, DateTimeFormatter pattern) {
- return time.format(pattern);
- }
- public static String format(LocalDateTime time) {
- return time.format(FORMATTER);
- }
- public static String formatDate(Date date) {
- return format(date2LocalDateTime(date));
- }
- /**
- * 解析字符串日期为Date
- *
- * @param dateStr 日期字符串
- * @param pattern 格式
- * @return Date
- */
- public static LocalDateTime parse(String dateStr, String pattern) {
- return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
- }
- public static LocalDateTime parse(String dateStr, DateTimeFormatter pattern) {
- return LocalDateTime.parse(dateStr, pattern);
- }
- public static LocalDateTime parse(String dateStr) {
- return LocalDateTime.parse(dateStr, FORMATTER);
- }
- public static Date parseDate(String dateStr) {
- return localDateTime2Date(LocalDateTime.parse(dateStr, FORMATTER));
- }
- // 获取当前时间的指定格式
- public static String formatNow(String pattern) {
- return format(LocalDateTime.now(), pattern);
- }
- // 日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
- public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
- return time.plus(number, field);
- }
- // 日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
- public static LocalDateTime minus(LocalDateTime time, long number, TemporalUnit field) {
- return time.minus(number, field);
- }
- /**
- * 获取两个日期的差 field参数为ChronoUnit.* 默认累加的方式 重要说明: 计算月份差 计算的是否满月,年份同理,
- * 区别1:Period只考虑到日期,ChronoUnit.between()计算到具体时分秒
- * 区别2:Period只计算月份差,不考虑年份是否一样,ChronoUnit.between()计算考虑年份
- * :Period只考虑到日期,field.between(startTime, endTime)会计算到具体时间 period.getMonths();
- * field.between 比如:6月6号8点到8月6号7点: 2 1 比如:6月6号8点到8月6号9点: 2 2 比如:6月5号到8月6号: 2 2
- * 比如:6月7号到8月6号: 1 1
- *
- * @param startTime
- * @param endTime
- * @param field 单位(年月日时分秒)
- * @return
- */
- public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
- // Period period = Period.between(LocalDate.from(startTime),
- // LocalDate.from(endTime));
- // startTime : 1993-10-19
- // endTime : 2019-08-06
- // 年龄 : 25 年 9 月 18 日
- // period.getYears(); period.getMonths(); period.getDays();
- // 25 年 9 月 18 日
- // field.between(startTime, endTime);
- // 25 年 25*12+9 ( 25*12+9)个月+18 日
- return field.between(startTime, endTime);
- }
- /**
- * 获取间隔时间
- *
- * @param startTime
- * @param endTime
- * @return
- */
- public static long betweenTwoTime(String startTime, String endTime, ChronoUnit field) {
- return betweenTwoTime(parse(startTime), parse(endTime), field);
- }
- /**
- * 获取间隔秒数
- *
- * @param startTime
- * @param endTime
- * @return
- */
- public static long betweenTwoTimeSecond(String startTime, String endTime) {
- return betweenTwoTime(startTime, endTime, ChronoUnit.SECONDS);
- }
- /**
- * Description:获得当前时间的字符串.
- *
- * @return String 当前时间的字符串格式.格式是yyyyMMddHHmmss
- * @author lijie
- */
- public static String getNowTimeStr() {
- return LocalDateTime.now().format(FORMATTER);
- }
- public static String getTimeStr(LocalDateTime date) {
- return date.format(FORMATTER);
- }
- public static String getMinusHour(String dateTime, long hour) {
- LocalDateTime parse = parse(dateTime).minusHours(hour);
- return format(parse);
- }
- public static String getSdfTimeNotDate(String dateTime) {
- return dateTime.substring(8, 14);
- }
- public static String getDate(String dateTime) {
- return dateTime.substring(0, 8);
- }
- public static String getPlusHour(String dateTime, long hour) {
- LocalDateTime parse = parse(dateTime).plusHours(hour);
- return format(parse);
- }
- public static String getPlusDay(String dateTime, long days) {
- LocalDateTime parse = parse(dateTime).plusDays(days);
- return format(parse);
- }
- public static String getPlusHourNow(long hour) {
- LocalDateTime parse = LocalDateTime.now().plusHours(hour);
- return format(parse);
- }
- /**
- * 获取年
- *
- * @return 年
- */
- public static int getYear(LocalDateTime ldt) {
- return ldt.getYear();
- }
- /**
- * 获取月份
- */
- public static int getMonth(LocalDateTime ldt) {
- return ldt.getMonthValue();
- }
- /**
- * 获取月份差 不算满月的月份差(比如7月31号到8月1号仍然算一个月份差)
- */
- public static int getBetweenMonth(LocalDateTime startLdt, LocalDateTime endLdt) {
- int minusMonth = (endLdt.getYear() - startLdt.getYear()) * 12 - endLdt.getMonthValue()
- - startLdt.getMonthValue();
- return minusMonth;
- }
- /**
- * 获取月份最大天数
- */
- public static int getDaylengthOfMonth() {
- LocalDateTime localTime = LocalDateTime.now();
- return localTime.toLocalDate().lengthOfMonth();
- }
- /**
- * 获取年份最大天数
- */
- public static int getDaylengthOfYear() {
- LocalDateTime localTime = LocalDateTime.now();
- return localTime.toLocalDate().lengthOfYear();
- }
- /**
- * Description: 日期格式转换
- *
- * @param srcDateStr 源日期字符串
- * @param srcDatePattern 源日期字符串格式
- * @param destDatePattern 目标日期字符串格式
- * @return String
- * @author luoguangyi
- * @since 2019年10月9日: 下午2:29:23 Update By luoguangyi 2019年10月9日: 下午2:29:23
- */
- public static String transferDateFormat(String srcDateStr, String srcDatePattern, String destDatePattern) {
- DateTimeFormatter srcFommater = DateTimeFormatter.ofPattern(srcDatePattern);
- DateTimeFormatter destFommater = DateTimeFormatter.ofPattern(destDatePattern);
- return LocalDateTime.parse(srcDateStr, srcFommater).format(destFommater);
- }
- @SuppressWarnings("deprecation")
- public static void main(String[] args) {
- // System.out.println(getSdfTimeNotDate(sdfTime));
- // LocalDateTime parse1 = DateUtils.parse("20150612152611");
- // LocalDateTime parse2 = DateUtils.parse("20150613152610");
- // System.out.println(betweenTwoTime(parse1, parse2, ChronoUnit.HOURS));
- // test1();
- String time = "20210913162100";
- Date date = parseDate(time);
- System.out.println(date);
- }
- private static void test1() {
- String dateTime = "20201026113042";
- 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";
- JSONObject c = JSONObject.parseObject(condition).getJSONObject("condition");
- JSONObject effectTime = c.getJSONObject("effectTime");
- System.out.println(effectTime.getJSONObject("period").getString("startTime"));
- System.out.println(effectTime.getJSONObject("period").getString("startTime").substring(0, 6));
- if (
- "period".equals(effectTime.getString("type"))
- && "000000".equals(effectTime.getJSONObject("period").getString("startTime").substring(0, 6))
- && "235959".equals(effectTime.getJSONObject("period").getString("endTime").substring(0, 6))) {
- //架构师w确认 00:00-23:59的生效时间为一值生效,23:59分不过期
- }
- if ("period".equals(effectTime.getString("type"))
- && effectTime.getJSONObject("period").getString("startTime").compareTo(DateUtils.getSdfTimeNotDate(dateTime)) <= 0
- && effectTime.getJSONObject("period").getString("endTime").compareTo(DateUtils.getSdfTimeNotDate(dateTime)) >= 0) {
- //在生效时间
- } else {
- log.info("[{}]不在设置的生效时间段", dateTime);
- }
- }
- }
|