java时间转化

public class TimeUtil {
    private final static String DATATIMEFORMATE = "yyyy-MM-dd HH:mm:ss";
    private final static String DATATIMEFORMATET = "yyyy-MM-dd'T'HH:mm:ss";
    private final static String DATATIMEFORMATETZ = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    private UmeTimeUtil() {
    }

    public static String timestamToDatetime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        String format = localDateTime + "Z";
        return format;
    }

    private static DateTimeFormatter getDateTimeFormat() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATATIMEFORMATE);
        return dateTimeFormatter;
    }

    public static String getNowDateTime() {
        LocalDateTime now = LocalDateTime.now();
        String NowDateTime = now.format(getDateTimeFormat());
        return NowDateTime;

    }

    public static String getNowDateTimeWithTAndZ() {
        LocalDateTime now = LocalDateTime.now();
        String NowDateTime = now + "Z";
        return NowDateTime;

    }

    public static String getNowDivDateTime(long day) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.minusDays(day);
        String NowDateTime = localDateTime.toString();
        return NowDateTime + "Z";

    }

    public static String getNowDivHourTime(long hours) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.minusHours(hours);
        String NowDateTime = localDateTime.toString();
        return NowDateTime + "Z";

    }

    public static String getZeroToNowTodayMinute() {
        LocalDateTime dateNow = LocalDateTime.now();
        LocalDateTime dateStart = LocalDateTime.of(LocalDateTime.now().getYear(), LocalDateTime.now().getMonth(), LocalDateTime.now().getDayOfMonth(), 0, 0);
        Duration between = Duration.between(dateStart, dateNow);
        long diffMinutes = between.toMinutes();
        if (diffMinutes == 0L) {
            diffMinutes = 1L;
        }
        return diffMinutes + "m";
    }

    public static String getFormatePrometheusDate(String dateTime) {
        LocalDateTime startDateFormate = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(DATATIMEFORMATE));
        String format = startDateFormate.format(DateTimeFormatter.ofPattern(DATATIMEFORMATETZ));
        return format;

    }

    public static String getDiffStartTimeAndEndDateTime(String startDate, String endDate) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startDateFormate = LocalDateTime.parse(startDate, DateTimeFormatter.ofPattern(DATATIMEFORMATE));
        LocalDateTime endDateFormate = LocalDateTime.parse(endDate, DateTimeFormatter.ofPattern(DATATIMEFORMATE));
        if (now.isBefore(endDateFormate)) {
            endDateFormate = now;
        }
        Duration between = Duration.between(startDateFormate, endDateFormate);
        long days = between.toDays();
        if (days > 90L) {
            return "1w";
        } else if (days > 30L) {
            return "1w";
        } else if (days > 6L) {
            return "1d";
        } else {
            return "1h";
        }
    }
}
    /**
     * 获取上周的前后时间
     * @return 今天周二(0314) => 20230306,20230313
     */
    public List<String> getCurrentWeek() {
        List<String> list = new ArrayList<>();
        DateTimeFormatter as = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime startMonth = LocalDateTime.now();
        DayOfWeek dayOfWeek = startMonth.getDayOfWeek();
        int value = dayOfWeek.getValue();
        LocalDateTime end = startMonth.minusDays(value-1);
        LocalDateTime start = startMonth.minusDays(value+6);
        String startTime = start.format(as);
        String endTime = end.format(as);
        list.add(0, startTime);
        list.add(1, endTime);
        log.info("the getCurrentWeek is :" + JSONObject.toJSONString(list));
        return list;
    }

    /**
     * 获取当月时间
     * @return [20230301,20230401]
     */
    public List<String> getCurrentMonth() {
        List<String> list = new ArrayList<>();
        DateTimeFormatter as = DateTimeFormatter.ofPattern("yyyyMM");
        LocalDateTime startMonth = LocalDateTime.now();
        LocalDateTime endMonth = startMonth.plusMonths(1L);
        String startTime = startMonth.format(as) + "01";
        String endTime = endMonth.format(as) + "01";
        list.add(0, startTime);
        list.add(1, endTime);
        log.info("the getCurrentMonth is :" + JSONObject.toJSONString(list));
        return list;
    }
    /**
     * 获取当天时间
     * @return 20230314
     */
    public String getCurrentDate() {
        DateTimeFormatter as = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(as);
        log.info("the getCurrentDate is :" + format);
        return format;
    }

    /**
     * 获取当前时间
     * @return 2023022813
     */
    private String getCurrentTime() {
        DateTimeFormatter as = DateTimeFormatter.ofPattern("yyyyMMddHH");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = now.minusHours(1L);
        String format = localDateTime.format(as);
        log.info("the getCurrentTime is :" + format);
        return format;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值