UUID Magic number obtained by Date - Android java.util

Android examples for java.util:UUID

Description

UUID Magic number obtained by Date

Demo Code


//package com.java2s;

import java.util.Date;
import java.util.UUID;

public class Main {
// from #cassandra's thobbs, who claims to have stolen it from a Python library.
    public static UUID newTimeUUIDSimple(Date d) {

        final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;

        long origTime = d.getTime();
        long time = origTime * 10000 + NUM_100NS_INTERVALS_SINCE_UUID_EPOCH;
        long timeLow = time & 0xffffffffL;
        long timeMid = time & 0xffff00000000L;
        long timeHi = time & 0xfff000000000000L;
        long upperLong = (timeLow << 32) | (timeMid >> 16) | (1 << 12)
                | (timeHi >> 48);//ww  w.ja  v a2 s .co  m
        return new UUID(upperLong, 0xC000000000000000L);
    }

    public static java.util.UUID newTimeUUIDSimple() {
        return newTimeUUIDSimple(new Date());
    }
}

Related Tutorials