user2134821
user2134821

Reputation: 21

How can i convert dropbox log timestamp?

I am debugging a android framework. I pull out dropbox logs from device and it's created in /data/system/dropbox. Log file name is printed like this format.

event_data@1362451303699

1362451303699 is timestamp and i want to change it like 05/03/2013 16:00 for legibility.

How can i convert this timestamp? Is there any code needs to be changed?

Any help will be much appreciated.

Upvotes: 2

Views: 1070

Answers (3)

madlymad
madlymad

Reputation: 6530

use: Date date = new Date(timestamp);

Edit full code:

String wantedDate = "";
String log = "event_data@1362451303699";
int index = log.indexOf("@");
if(index != -1) {
  index = index + 1; // skip @ symbol
  if(index < log.length()) { // avoid out of bounds
    String logtime = log.substring(+1);
    try {
      long timestamp = Long.parseLong(logtime);
      SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
      Date date = new Date(timestamp);
      wantedDate = df.format(date);
    } catch (NumberFormatException nfe) {
      // not a number 
    }
  }
}
if( ! "".equals(wantedDate) ) {
       // everything OK
} else {
       // error cannot retrieve date!
}

Related doc:

Upvotes: 2

Adam Monos
Adam Monos

Reputation: 4307

It is a UNIX epoch timestamp, all you need to do is to convert the String representation of the number to long, then you can use it to create a Date object, which you can format with DateFormat. Something like this:

// Get this from the log
String timestamp = "1362451303699";
long epoch = Long.parseLong(timestamp);
Date date = new Date(epoch);

DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String formattedDate = format.format(date);

Upvotes: 0

Obl Tobl
Obl Tobl

Reputation: 5684

you can use a SimepleDateFormat to parse it. For example:

long ts = 1362451303699;
Date date = new Date(ts);    
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
System.out.println(sdf.format(date));

With the SimpleDateFormat you can bring your Date in a more readable format.

Upvotes: 0

Related Questions