JodaTimeUtils time conversion tools long string type conversion or the like Date Format

package com.yjx.utils;

import com.yjx.verify.Asserts;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;


/**
 * JodaTime 时间工具
 *
 * @author yangjunxiong
 * @date 2020/2/19 11:02
 **/
public final class JodaTimeUtils {
    private static final Logger   logger   = LoggerFactory.getLogger(JodaTimeUtils.class);
    private static       String[] patterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm"};

    private JodaTimeUtils() {
    }

    /**
     * 根据当天日期,获取当天的时间临界点时间
     *
     * @param localDate
     * @return
     */
    public static ImmutablePair<LocalDateTime, LocalDateTime> criticalTime(LocalDate localDate) {
        Asserts.notNull(localDate);
        LocalDateTime beginTime = new LocalDateTime(localDate.getYear(), localDate.getMonthOfYear(), localDate.getDayOfMonth(), 0, 0, 0, 0);
        LocalDateTime endTime = new LocalDateTime(localDate.getYear(), localDate.getMonthOfYear(), localDate.getDayOfMonth(), 23, 59, 59, 999);
        return ImmutablePair.of(beginTime, endTime);
    }

    /**
     * 获取当天的最小值
     *
     * @param localDate
     * @return
     */
    public static LocalDateTime minTime(LocalDate localDate) {
        Asserts.notNull(localDate);
        return new LocalDateTime(localDate.getYear(), localDate.getMonthOfYear(), localDate.getDayOfMonth(), 0, 0, 0, 0);
    }

    /**
     * 获取当天的最大值
     *
     * @param localDate
     * @return
     */
    public static LocalDateTime maxTime(LocalDate localDate) {
        Asserts.notNull(localDate);
        return new LocalDateTime(localDate.getYear(), localDate.getMonthOfYear(), localDate.getDayOfMonth(), 23, 59, 59, 999);
    }

    /*
     * 【 获取时间段内的所有日期 】
     *
     * @author YJX
     * @date 2019/11/11 22:46
     * @param startTime
     * @param endTime
     * @return java.util.ArrayList<org.joda.time.LocalDate>
     **/
    public static ArrayList<LocalDate> getLocalDates(LocalDate startTime, LocalDate endTime) {
        ArrayList<LocalDate> localDates = new ArrayList<>();
        do {
            localDates.add(startTime);
            startTime = startTime.plusDays(1);
        } while (endTime.toDate().getTime() > startTime.toDate().getTime());
        return localDates;
    }

    /*
     * 【 获取时间段内的所有日期 】
     *
     * @author YJX
     * @date 2019/11/11 22:46
     * @param startTime
     * @param endTime
     * @return java.util.ArrayList<org.joda.time.LocalDate>
     **/
    private ArrayList<LocalDate> getLocalDates(Date startTime, Date endTime) {
        LocalDate tradeDate = new LocalDate(startTime);
        ArrayList<LocalDate> localDates = new ArrayList<>();
        do {
            localDates.add(tradeDate);
            tradeDate = tradeDate.plusDays(1);
        } while (endTime.getTime() > tradeDate.toDate().getTime());
        return localDates;
    }

    /**
     * 【 String字符串或时间戳字符串转Date格式  】
     * @author yangjunxiong
     * @date 2020/2/19 11:26
     * @param dateString 需要转换的时间字段串(可以是时间戳或者patterns的类型)
     * @param dateFormat 传的是时间字符串  不传默认解析:"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm"
     * @return java.util.Date
     **/
    public static Date toDate(String dateString, String... dateFormat) {

        if (StringUtils.isBlank(dateString)) {
            return null;
        }
        //如果传的是时间戳
        try {
            if (StringUtils.isNumeric(dateString)) {
                return new Date(Long.parseLong(dateString));
            }
        } catch (NumberFormatException e) {
            logger.error("时间戳时间格式化出错:{}", dateString);
            return null;
        }
        try {
            //执行自定义的格式转换
            if (dateFormat.length > 0) {
                return DateUtils.parseDate(dateString, dateFormat);
            }
            return DateUtils.parseDate(dateString, patterns);
        } catch (ParseException e) {
            logger.error("字符串时间格式化出错:{}", dateString);
            return null;
        }
    }
}


Published 71 original articles · won praise 3 · Views 8742

Guess you like

Origin blog.csdn.net/qq_40250122/article/details/104389259
Recommended