Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a date from a UUID using Java? [closed]

Tags:

java

date

uuid

How to convert the UUID to date format 2011-04-22 ?

For example, I have UUID like this

118ffe80-466b-11e1-b5a5-5732cf729524.

How to convert this to date format?

I tried

 String uuid="118ffe80-466b-11e1-b5a5-5732cf729524"; 
    UUID uid = UUID.fromString(uuid);
    long ls=convertTime(uid.timeStamp()); // it returns long value

    public String convertTime(long time){
            System.out.println("====="+time);
            Date date = new Date(time);
            Format format = new SimpleDateFormat("yyyy/MM/dd");
            return format.format(date).toString();
        }

output I got:4294744/11/02

Same case working fine for perl

$uuid='ef802820-46b3-11e2-bf3a-47ef6b3e28e2';
$uuid =~ s/-//g;

my $timelow = hex substr( $uuid, 2 * 0,     2 * 4 );
my $timemid = hex substr( $uuid, 2 * 4,     2 * 2 );
my $version = hex substr( $uuid, 2 * 6,     1 );
my $timehi  = hex substr( $uuid, 2 * 6 + 1, 2 * 2 - 1 );

my $time = ( $timehi * ( 2**16 ) + $timemid ) * ( 2**32 ) + $timelow;
my $epoc = int( $time / 10000000 ) - 12219292800;
my $nano = $time - int( $time / 10000000 ) * 10000000;

#$time_date = scalar localtime $epoc;
#print strftime( '%d-%m-%Y %H:%M:%S', localtime($epoc) );
#print "\n Time: ", scalar localtime $epoc, " +", $nano / 10000, "ms\n";
like image 573
BALASCJP Avatar asked Mar 02 '13 21:03

BALASCJP


People also ask

How do I find the UUID date?

The Java timestamp is in milliseconds since 1970-01-01. In order to get a meaningful date from a UUID, you'll need to do two things: convert from 100ns to 1ms precision (divide by 10000) and rebase from 1582-10-15 to 1970-01-01, which you can do by adding a constant value.

Does UUID contain timestamp?

The timestamp value associated with this UUID. The 60 bit timestamp value is constructed from the time_low, time_mid, and time_hi fields of this UUID . The resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC.

Can we convert UUID to string in Java?

UUID toString() Method in Java with ExamplesThe toString() method of UUID class in Java is generally used to get the string representation of this UUID. Parameters: This method does not take any parameter. Return Value: This method returns a String value which is the string representation of this UUID.

Can we decode UUID?

The UUID version is represented by the 13th digit of a hexadecimal UUID string ("M" in the diagram below). The variant is represented in the 17th digit ("N" in the diagram below). The version and variant are encoding within UUIDs. The version is straight forward to decode.


1 Answers

The javadoc for UUID says the following about the timestamp field:

The 60 bit timestamp value is constructed from the time_low, time_mid, and time_hi fields of this UUID. The resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC.

(emphasis mine)

The Java timestamp is in milliseconds since 1970-01-01. In order to get a meaningful date from a UUID, you'll need to do two things: convert from 100ns to 1ms precision (divide by 10000) and rebase from 1582-10-15 to 1970-01-01, which you can do by adding a constant value.

WolframAlpha tells us that 1582-10-15 corresponds to a UNIX timestamp of -12219292800, so to get the correct date, you must add 12219292800 to the number of milliseconds you got after dividing by 10000.

As a side note:

The timestamp value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.

...so make sure your code either only ever encounters Type 1 UUID's, or can handle that they don't have a timestamp.

like image 176
Barend Avatar answered Sep 30 '22 16:09

Barend