java 时间转换工具类

引言

记录使用

依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.67</version>
</dependency>

代码

import org.apache.commons.lang3.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间管理工具类
 *
 * @author zhujx
 */
public class DateUtils {

    public static final Long ONE_DAY = 24 * 60 * 60 * 1000L;

    public static final Long ONE_HOUR = 60 * 60 * 1000L;

    public static final Long ONE_MINUTE = 60 * 1000L;

    public static final ThreadLocal<DateFormat> DF_YMD_INT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd"));

    public static final ThreadLocal<DateFormat> DF_YMD = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

    public static final ThreadLocal<DateFormat> DF_YMD_HMS = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    public static final ThreadLocal<DateFormat> DF_YMD_HMS_INT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));

    public static final ThreadLocal<DateFormat> DF_YMD_HMS_TZ = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));

    public static final ThreadLocal<DateFormat> DF_YMD_HMS_S = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"));

    public static final DateTimeFormatter COMMON_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static Date resetZero(Date date) {
        try {
            return DF_YMD.get().parse(DF_YMD.get().format(date));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Date parse(String dateStr, String pattern) {
        if (StringUtils.isBlank(dateStr) || StringUtils.isBlank(pattern)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String format(Date date, String pattern) {
        if (date == null || StringUtils.isBlank(pattern)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static Date addDay(Date runDate, int i) {
        return new Date(runDate.getTime() + ONE_DAY * i);
    }

    public static Date addHour(Date runDate, int i) {
        return new Date(runDate.getTime() + ONE_HOUR * i);
    }

    public static boolean isBetween(Date time, Date startTime, Date endTime) {
        if (time == null || startTime == null || endTime == null) {
            return false;
        }
        return time.compareTo(startTime) > 0 && time.compareTo(endTime) < 0;
    }

    public static String dateYmdHmsString(Date date) {
        if (date == null) {
            return null;
        }
        return DF_YMD_HMS.get().format(date);
    }

    public static String dateYmdFormatString(Date date) {
        if (date == null) {
            return null;
        }
        return DF_YMD.get().format(date);
    }

    public static Long dateYmdLong(Date date) {
        if (date == null) {
            return null;
        }
        return Long.valueOf(DF_YMD_INT.get().format(date));
    }

    public static Long dateYmdHmsLong(Date date) {
        if (date == null) {
            return null;
        }
        return Long.valueOf(DF_YMD_HMS_INT.get().format(date));
    }

}

如果你喜欢这篇文章,请点个赞,加个关注吧!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值