All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.opencodes.utils.IDUtils Maven / Gradle / Ivy

package cn.opencodes.utils;
import java.util.Base64;
import java.util.Random;
import java.util.UUID;

import org.apache.commons.lang.RandomStringUtils;  
  
/** 
 * 各种id生成策略 
 * @version 1.0 
 */  
public class IDUtils {  
	
	/**
	 * 随机生成指定长度字符串
	 * @param len
	 */
	public static String randomBuildString(int len){
		return RandomStringUtils.randomAlphanumeric(len);
	}
  
    /** 
     * 图片名生成 
     */  
    public static String genImageName() {  
        //取当前时间的长整形值包含毫秒  
        long millis = System.currentTimeMillis();  
        //long millis = System.nanoTime();  
        //加上三位随机数  
        Random random = new Random();  
        int end3 = random.nextInt(999);  
        //如果不足三位前面补0  
        String str = millis + String.format("%03d", end3);  
        return str;  
    }  
    /**
     * 获取日期格式流水号 (yyyyMMddHHmmss)
     * @param prefix
     */
    public static String buildDateLineNo(String prefix) {
		return String.format(prefix+"%1$tY%1$tm%1$td%1$tk%1$tM%1$tS", System.currentTimeMillis());
	}
      
    /** 
     * 商品id生成 
     */  
    public static long genItemId() {  
        //取当前时间的长整形值包含毫秒  
        long millis = System.currentTimeMillis();  
        //long millis = System.nanoTime();  
        //加上两位随机数  
        Random random = new Random();  
        int end2 = random.nextInt(99);  
        //如果不足两位前面补0  
        String str = millis + String.format("%02d", end2);  
        long id = new Long(str);  
        return id;  
    }
	
	
	public static String base64Encoder(String content){
		return Base64.getEncoder().encodeToString(content.getBytes());
	}
	
	public static String base64Decoder(String src){
		return new String(Base64.getDecoder().decode(src));
	}
	
	/**
	 * UUID 去掉-
	 */
	public static String guid() {  
		 String key = UUID.randomUUID().toString().replace("-", "");  
		 return key;  
	} 
	
}  




© 2015 - 2024 Weber Informatics LLC | Privacy Policy