create UUID from current time - Android Hardware

Android examples for Hardware:Device ID

Description

create UUID from current time

Demo Code


//package com.java2s;

public class Main {
    public static final String createUUID() {
        long time1 = System.currentTimeMillis();
        long time2 = (long) ((double) System.currentTimeMillis() * Math
                .random());//from  w  w w. j  av  a2 s.  co m
        return toUUID((int) (time1 & 0xFFFF)) + "-"
                + toUUID((int) ((time1 >> 32) | 0xA000) & 0xFFFF) + "-"
                + toUUID((int) (time2 & 0xFFFF)) + "-"
                + toUUID((int) ((time2 >> 32) | 0xE000) & 0xFFFF);
    }

    private static final String toUUID(int seed) {
        String id = Integer.toString((int) (seed & 0xFFFF), 16);
        int idLen = id.length();
        String uuid = "";
        for (int n = 0; n < (4 - idLen); n++)
            uuid += "0";
        uuid += id;
        return uuid;
    }
}

Related Tutorials