一个安全且完美的DateUitls(Date LocalDateTime long String相互转化)

Date LocalDateTime long String相互转化

先发代码

代码比较简单,没有记下来的意义,主要是备忘

不要使用SimpleDateFormat ,因为线程不安全,会在文章的最后,简单说明多线程情况下不安全的原因

package com.zhiweicoding.haiyoutool.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author by diaozhiwei on 2023/01/31.
 * @email diaozhiwei2k@163.com
 */
public class DateTimeFormatUtil {

    public static final DateTimeFormatter DATE_TIME_FULL = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    public static final DateTimeFormatter DATE_TIME_ALL = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter TIME_ALL = DateTimeFormatter.ofPattern("HH:mm:ss");
    public static final DateTimeFormatter DATE_ALL = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter DATE_TIME_ALL_CLOSELY = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    public static LocalDateTime dateToDT(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Date dTToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static long stringToLong(String dateStr, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return stringToLong(dateStr, formatter);
    }

    public static long stringToLong(String dateStr, DateTimeFormatter pattern) {
        LocalDateTime parse = LocalDateTime.parse(dateStr, pattern);
        return dTToLong(parse);
    }

    public static LocalDateTime stringToDT(String dateStr, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return stringToDT(dateStr, formatter);
    }

    public static LocalDateTime stringToDT(String dateStr, DateTimeFormatter pattern) {
        LocalDateTime parse = LocalDateTime.parse(dateStr, pattern);
        return parse.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static LocalDateTime longToDT(long timestamp) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), TimeZone.getDefault().toZoneId());
    }

    public static long dTToLong(LocalDateTime dt) {
        return dt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static String dTToString(LocalDateTime dt, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return dTToString(dt, formatter);
    }

    public static String dTToString(LocalDateTime dt, DateTimeFormatter pattern) {
        return dt.format(pattern);
    }

}

github源码地址

简单说明下使用LocalDateTime的原因

先看java DateFormat源码代码

    protected Calendar calendar;
 
    private StringBuffer format(Date date, StringBuffer toAppendTo,
                                FieldDelegate delegate) {
        // Convert input date to time field list
       calendar.setTime(date);

       boolean useDateFormatSymbols = useDateFormatSymbols();

       for (int i = 0; i < compiledPattern.length; ) {
           int tag = compiledPattern[i] >>> 8;
           int count = compiledPattern[i++] & 0xff;
           if (count == 255) {
               count = compiledPattern[i++] << 16;
               count |= compiledPattern[i++];
           }
           switch (tag) {
           case TAG_QUOTE_ASCII_CHAR:
               toAppendTo.append((char)count);
               break;
           case TAG_QUOTE_CHARS:
               toAppendTo.append(compiledPattern, i, count);
               i += count;
               break;
           default:
               subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
               break;
           }
       }
       return toAppendTo;
   }

calendar 属性是共享变量,在多线程的情况下,没有做线程安全控制。很可能发生一个线程刚刚set了值,另外一个线程又set了另外的值,导致结果异常的情况。所以在原来的代码中经常会使用ThreadLocal的方式创建SimpleDateFormat对象。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值