2013-03-05 177 views
2

我正在调试一个android框架。 我从设备中取出Dropbox日志,并在/ data/system/dropbox中创建该日志。 日志文件名称按此格式打印。如何转换Dropbox日志时间戳?

EVENT_DATA @ 1362451303699

1362451303699是时间戳,我想改变它像2013年5月3日16:00可读性。

如何转换此时间戳? 是否有任何代码需要更改?

任何帮助将不胜感激。

回答

2

使用:Date date = new Date(timestamp);

编辑全码:

String wantedDate = ""; 
String log = "[email protected]"; 
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! 
} 

相关文件:

+0

您是否知道存在源代码文件的特定目录?我无法找到文件,因为android文件夹中有太多文件。 – user2134821 2013-03-05 09:05:14

+0

源代码是什么? – madlymad 2013-03-05 09:08:56

+0

生成保管箱日志文件的文件。我只需拿出一个Dropbox日志即可。 – user2134821 2013-03-05 09:11:38

0

你可以使用SimepleDateFormat来解析它。例如:

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

随着SimpleDateFormat,你可以把你的时间更可读的格式。

0

这是一个UNIX纪元时间戳,所有你需要做的就是数字的String表示转换为long,那么你可以使用它来创建一个Date对象,你可以用DateFormat格式化。类似这样的:

// 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);