时间字符串转时间戳

在计算处理的时候,日期是时间格式,要把日期转换为时间戳,才能计算。要怎么处理呢?

直接上代码:

代码:

public static void main(String[] args) {
    String timeStr = "2021-04-22 09:00:28";
    String dateStr = "2021-04-22";
    System.out.println(DateTimeUtils.toLongDateStr(timeStr));
    System.out.println(DateTimeUtils.toLongDateStr(dateStr));
}

结果:

1619053228000
1619020800000

日期工具类:

public class DateTimeUtils {
    private static final String DATE_TIME_STR = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_STR = "yyyy-MM-dd";
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_STR);
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_STR);

    /**
     * 字符串转时间戳到毫秒级
     * @param time 时间串
     * @return long 毫秒级时间戳
     */
    public static Long toLongDateStr(String time) {
        if (time.length() == DATE_TIME_STR.length()) {
            LocalDateTime dateTime = stringToDateTime(time);
            return dateTimeToEpochMilli(dateTime);
        }
        LocalDate date = stringToDate(time);
        return dateToEpochMilli(date);

    }

    /**
     * 获取到毫秒级时间戳
     * @param localDate 具体时间
     * @return long 毫秒级时间戳
     */
    public static long dateToEpochMilli(LocalDate localDate) {
        return localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
    }

    /**
     * 获取到毫秒级时间戳
     * @param localDateTime 具体时间
     * @return long 毫秒级时间戳
     */
    public static long dateTimeToEpochMilli(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }


    /**
     *  时间字符串 转LocalDateTime
     * @param localDateTime  时间字符串
     * @return LocalDateTime
     */
    public static LocalDateTime stringToDateTime(String localDateTime) {
        return LocalDateTime.parse(localDateTime, DATE_TIME_FORMATTER);
    }

    /**
     *  时间字符串 转LocalDateTime
     * @param localDateTime  时间字符串
     * @return LocalDateTime
     */
    public static LocalDate stringToDate(String localDateTime) {
        return LocalDate.parse(localDateTime, DATE_FORMATTER);
    }


    /**
     *  转换带T的日期的格式
     *  @param timet 时间串
     */
    public static String formatIncludeTTime(String timet) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            SimpleDateFormat sdf2 = new SimpleDateFormat(DATE_TIME_STR);
            Date date = sdf1.parse(timet);
            return sdf2.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timet;
    }

    /**
     * 判断是否是时间格式
     * @param dateString 时间串
     * @return boolean 是否是时间格式
     */
    public static boolean isValidDateStr(String dateString) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_STR);
        try {
            df.parse(dateString);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     *  字符串时间戳转日期格式
     * @param timestamp  字符串时间戳转
     * @return String 日期格式
     */
    public static String formatTimeStamp(String timestamp) {
        // Instant.ofEpochMilli 转 毫秒,  Instant.ofEpochSecond 转 秒
        LocalDateTime localDateTime = Instant.ofEpochMilli(Long.parseLong(timestamp)).atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        return localDateTime.format(DATE_TIME_FORMATTER);
    }


    /**
     *  判断字符串内容是否为时间串
     * @param timestamp  字符串时间戳转
     * @return boolean 是否为日期格式
     */
    public static boolean isValidTimeStamp(String timestamp) {
        SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_STR);
        try {
            df.format(new Date(Long.parseLong(String.valueOf(timestamp))));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}

总结:

   时间的处理经常会碰到,写一个常用的日期工具类,后面有添加功能就里面加,用的时候直接调用,这样会方便很多,做好备注也不怕忘记。

   关联文章: 计算处理时长-日期计算

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值