2013-03-02 10 views
13

Come convertire il UUID in formato data 2011-04-22?Come estrarre una data da un UUID utilizzando Java?

Ad esempio, ho UUID come questo

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

Come convertire questo formato della data?

ho cercato

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 che ho ottenuto: 4294744/11/02

Stesso caso funziona bene per il 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"; 
+0

controllare gentilmente con questo URL http://stackoverflow.com/questions/15127648/how-do-we-convert-uuid-to-date-in-perl – BALASCJP

+0

Sapete se quell'UUID di origine è un UUID v1? – Joe

+0

'timeStamp()' restituisce un timestamp misurato in unità di 100 nanosecondi da mezzanotte, 15 ottobre 1582 UTC; 'Data (data lunga)' si aspetta millisecondi dal 1 gennaio 1970, 00:00:00 GMT. Quindi devi convertire da un formato all'altro – tmuguet

risposta

13

Javadoc per UUID dice quanto segue circa il campo timestamp:

Il valore di timestamp a 60 bit viene costruito dai campi time_low, time_mid e time_hi di questo UUID. Il timestamp risultante viene misurato in unità di 100 nanosecondi da mezzanotte, 15 ottobre 1582 UTC.

(sottolineatura mia)

Java timestamp è espresso in millisecondi dal 1970-01-01. Per ottenere una data significativa da un UUID, devi fare due cose: convertire da 100ns a 1ms di precisione (dividi per 10000) e rebase dal 1582-10-15 al 1970-01-01, cosa che puoi fare aggiungendo un valore costante.

WolframAlpha tells us che 1582/10/15 corrisponde a un timestamp UNIX di -12219292800, in modo da ottenere la data corretta, è necessario aggiungere 12219292800 al numero di millisecondi che hai dopo aver diviso da 10000.

Come nota a margine :

Il valore timestamp è significativa solo in un UUID basato sul tempo, che ha tipo di versione 1. Se questo UUID non è un UUID basate sul tempo, questo metodo genera UnsupportedOperationException.

... quindi assicuratevi che il vostro codice incontri sempre solo UUID di tipo 1, o che possa gestire che non hanno un timestamp.

+0

Grazie Barend puoi dare un semplice esempio. – BALASCJP

+1

Intendi 'nuova data (uid.timeStamp()/10000L + 12219292800L)'? Potrei darti un esempio, ma sono sicuro che puoi capirlo :). – Barend

+0

ya certo grazie ... – BALASCJP

Problemi correlati