使用ConcurrentHashMap保证线程安全性,唯一性,AtomicInteger实现自增长

本文介绍了如何在Java中利用ConcurrentHashMap保证线程安全性和唯一性,以及使用AtomicInteger实现17位代码的自增长。这17位代码由3位固定字符、9位当前时间戳和5位自增数构成,作为Uuid的替代方案。
摘要由CSDN通过智能技术生成

情景:需要生成17位代码来代替UUid,前3位固定,后五位使用 AtomicInteger实现自增长,中间9为是截取当前时间的年月日时分秒

自定义的DateUtil工具类:


```java


import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by babata on 2014/12/25.
 */
@Slf4j
public class DateUtil {
    public static final String timeFormate = "yyyy-MM-dd HH:mm:ss";
    public static final String dateFormate = "yyyy-MM-dd";
    public static final String pannelTime = "yyyyMMddHHmmss";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String HHMMSS = "HH:mm:ss";
    /**
     * 获取系统时间 年月日时分秒
     * @return
     */
    public static String nowDateTime(){
        SimpleDateFormat sdf = new SimpleDateFormat(timeFormate);
        Date date = new Date();
        return sdf.format(date);
    }

    public static String nowDay(){
        SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd);
        Date date = new Date();
        return sdf.format(date);
    }



    public static long getDistaceLong(String dateStr) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(timeFormate);
            Date date = sdf.parse(dateStr);
            return new Date().getTime() - date.getTime();
        }catch (Exception e){
            log.error("getDistaceLong error", e);
        }

        return 0;
    }

    public static String nowPannelDateTime(){
        SimpleDateFormat sdf = new SimpleDateFormat(pannelTime);
        Date date = new Date();
        return sdf.format(date);
    }


    public static Long nowDateTimeLong() {
        return new Date().getTime() ;
    }
    public static long time2Long(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat(timeFormate);
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            log.error("exe error", e);
        }
        return date.getTime();
    }

    /**
     * 年月日
     * @return
     */
    public static String yyyyMMdd(){
        SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd);
        Date date = new Date();
        return sdf.format(date);
    }

    public static String nowDateTimeSimple(){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormate);
        Date date = new Date();
        return sdf.format(date);
    }

    public static String getNowHour(){
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        Date date = new Date();
        return sdf.format(date);
    }

    public static String getNowYear(){
        SimpleDateFormat 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值