「javauuid时间」javaUUID

博主:adminadmin 2022-11-28 11:58:08 85

本篇文章给大家谈谈javauuid时间,以及javaUUID对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

什么是UUID,Java中怎么产生UUID

UUID: 通用唯一识别码 (Universally Unique Identifier),是根据时间,机器码,网络地址生成的全球唯一数。

引入 java.util.UUID,直接调用以下即可得到一个32为的随机数,即UUID.

UUID uuid = UUID.randomUUID();

JAVA JDK提供的一个自动生成主键 UUID.randomUUID()的方法 它永远都不会重复嘛??

从一定意义上讲,这个UUID号称是世界级的不重复,也就是说有生之年这个ID肯定是不重复的,但也只是个相对的概念。小概率事件不可能发生。

什么是UUID,Java中怎么产生UUID?

UUID 是 通用唯一识别码(Universally Unique Identifier)的缩写,是一种软件建构的标准,亦为开放软件基金会组织在分布式计算环境领域的一部分。其目的,是让分布式系统中的所有元素,都能有唯一的辨识信息,而不需要通过中央控制端来做辨识信息的指定。

生成UUID的方法:

public static String getUUID32(){ String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase(); return uuid;//  return UUID.randomUUID().toString().replace("-", "").toLowerCase();}

注:因为一般数据库主键为String类型,所以接收类型为String,生成的uuid数据包含-,所以要去掉-,故UUID.randomUUID().toString().replace("-", "").toLowerCase()。

扩展资料:

UUID由以下几部分的组合:

(1)当前日期和时间,UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相同。

(2)时钟序列。

(3)全局唯一的IEEE机器识别号,如果有网卡,从网卡MAC地址获得,没有网卡以其他方式获得。

UUID的唯一缺陷在于生成的结果串会比较长。关于UUID这个标准使用最普遍的是微软的GUID(Globals Unique Identifiers)。在ColdFusion中可以用CreateUUID()函数很简单地生成UUID,其格式为:xxxxxxxx-xxxx- xxxx-xxxxxxxxxxxxxxxx(8-4-4-16)。

其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。而标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12),可以从cflib 下载CreateGUID() UDF进行转换。

参考资料:百度百科-UUID

java怎么把系统当前时间和UUID组成上传后文件的文件名求全代码

UUID uuid = UUID.randomUUID();

long time = System.currentTimeMillis();

System.out.println(uuid+"-"+time);

java一个util类方法,为什么生成的uuid会占用到大部分程序处理时间

  //得到指定数量的UUID,以数组的形式返回

    public static String[] getUUID(int num){

        if( num = 0)

            return null;

        String[] uuidArr = new String[num];

        for (int i = 0; i  uuidArr.length; i++) {

            uuidArr[i] = getUUID32();

        }

        return uuidArr;

    }

    //得到32位的uuid

        public static String getUUID32(){

            return UUID.randomUUID().toString().replace("-", "").toLowerCase();

        }

主要是生成的过程中有个循环,这个时间长点

怎样用java生成GUID与UUID

两种方式生成guid 与uuid

需要comm log 库

/**

 * @author Administrator

 *

 * TODO To change the template for this generated type comment go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.util.Random;

public class RandomGUID extends Object {

   protected final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory

      .getLog(getClass());

   public String valueBeforeMD5 = "";

   public String valueAfterMD5 = "";

   private static Random myRand;

   private static SecureRandom mySecureRand;

   private static String s_id;

   private static final int PAD_BELOW = 0x10;

   private static final int TWO_BYTES = 0xFF;

   /*

    * Static block to take care of one time secureRandom seed.

    * It takes a few seconds to initialize SecureRandom.  You might

    * want to consider removing this static block or replacing

    * it with a "time since first loaded" seed to reduce this time.

    * This block will run only once per JVM instance.

      */

   static {

      mySecureRand = new SecureRandom();

      long secureInitializer = mySecureRand.nextLong();

      myRand = new Random(secureInitializer);

      try {

         s_id = InetAddress.getLocalHost().toString();

      } catch (UnknownHostException e) {

         e.printStackTrace();

      }

   }

   /*

    * Default constructor.  With no specification of security option,

    * this constructor defaults to lower security, high performance.

    */

   public RandomGUID() {

      getRandomGUID(false);

   }

   /*

    * Constructor with security option.  Setting secure true

    * enables each random number generated to be cryptographically

    * strong.  Secure false defaults to the standard Random function seeded

    * with a single cryptographically strong random number.

    */

   public RandomGUID(boolean secure) {

      getRandomGUID(secure);

   }

   /*

    * Method to generate the random GUID

    */

   private void getRandomGUID(boolean secure) {

      MessageDigest md5 = null;

      StringBuffer sbValueBeforeMD5 = new StringBuffer(128);

      try {

         md5 = MessageDigest.getInstance("MD5");

      } catch (NoSuchAlgorithmException e) {

         logger.error("Error: " + e);

      }

      try {

         long time = System.currentTimeMillis();

         long rand = 0;

         if (secure) {

            rand = mySecureRand.nextLong();

         } else {

            rand = myRand.nextLong();

         }

         sbValueBeforeMD5.append(s_id);

         sbValueBeforeMD5.append(":");

         sbValueBeforeMD5.append(Long.toString(time));

         sbValueBeforeMD5.append(":");

         sbValueBeforeMD5.append(Long.toString(rand));

         valueBeforeMD5 = sbValueBeforeMD5.toString();

         md5.update(valueBeforeMD5.getBytes());

         byte[] array = md5.digest();

         StringBuffer sb = new StringBuffer(32);

         for (int j = 0; j  array.length; ++j) {

            int b = array[j]  TWO_BYTES;

            if (b  PAD_BELOW)

               sb.append('0');

            sb.append(Integer.toHexString(b));

         }

         valueAfterMD5 = sb.toString();

      } catch (Exception e) {

         logger.error("Error:" + e);

      }

   }

   /*

    * Convert to the standard format for GUID

    * (Useful for SQL Server UniqueIdentifiers, etc.)

    * Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6

    */

   public String toString() {

      String raw = valueAfterMD5.toUpperCase();

      StringBuffer sb = new StringBuffer(64);

      sb.append(raw.substring(0, 8));

      sb.append("-");

      sb.append(raw.substring(8, 12));

      sb.append("-");

      sb.append(raw.substring(12, 16));

      sb.append("-");

      sb.append(raw.substring(16, 20));

      sb.append("-");

      sb.append(raw.substring(20));

      return sb.toString();

   }

     // Demonstraton and self test of class

     public static void main(String args[]) {

       for (int i=0; i 100; i++) {

         RandomGUID myGUID = new RandomGUID();

         System.out.println("Seeding String=" + myGUID.valueBeforeMD5);

         System.out.println("rawGUID=" + myGUID.valueAfterMD5);

         System.out.println("RandomGUID=" + myGUID.toString());

       }

     }

}

同样

UUID uuid = UUID.randomUUID();

System.out.println("{"+uuid.toString()+"}");

UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。通常平台会提供生成UUID的API。UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。由以下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长。关于UUID这个标准使用最普遍的是微软的GUID(Globals Unique Identifiers)。

关于javauuid时间和javaUUID的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。