2

我正在调试一个android框架。我从设备中提取 Dropbox 日志,它是在 /data/system/dropbox 中创建的。日志文件名以这种格式打印。

event_data@1362451303699

1362451303699 是时间戳,我想将其更改为 05/03/2013 16:00 以提高可读性。

我怎样才能转换这个时间戳?是否有任何代码需要更改?

任何帮助都感激不尽。

4

3 回答 3

2

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

编辑完整代码:

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!
}

相关文档:

于 2013-03-05T08:50:19.123 回答
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您可以以更易读的格式提供您的日期。

于 2013-03-05T08:52:05.237 回答
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);
于 2013-03-05T08:56:22.460 回答