这是你的答案。我认为您必须开始搜索您的查询。我不擅长java,但这花了我2个小时来搜索和写这个。无论如何都没有正确安排这工作,现在我可以使用它来将dmesg转换为adb shell日期格式。注意:- 虽然替换date and uptime
使用 - date;cat /proc/uptime
在 adb shell 中同时获取两者(不完全准确)
转换getevent文件的主要方法
public static void convertGetEventFiletoHumanReadableTime(
String geteventFileLocation) throws ParseException, IOException {
SimpleDateFormat adbDatefmt = new SimpleDateFormat(
"EEE MMM dd hh:mm:ss z yyyy");
String adbDate = "Mon Jan 4 21:23:19 KST 2016"; //replace with your adb shell date
String uptime = "33224.56 242604.45" //replace with your device cat adb shell /proc/uptime
uptime = uptime.split(" ")[0];
String event = "/dev/input/event1" //replace with event type you need to
BufferedReader br = new BufferedReader(
new FileReader(dmesgFileLocation));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
int lineNum = 0;
while (line != null) {
// System.out.println(lineNum + ": " + line); //for debug
if (line.trim().length() > 0) {
String eventTimeStamp = getEventTimeStamp(line, event);
System.out.println("TIME " + eventTimeStamp);
float eventTimeF = Float.parseFloat(eventTimeStamp);
float eventTimeMillisecF = (float) (Math
.round(eventTimeF * 100.0) / 100.0);
Date dmesghTime = convertGeteventTimestampToHumanReadable(
uptime, adbDate, eventTimeMillisecF);
line = line.replace(eventTimeStamp,
adbDatefmt.format(dmesghTime));
sb.append(line);
sb.append(System.lineSeparator());
}
line = br.readLine();
lineNum++;
}
String everything = sb.toString();
// System.out.println(everything);
try {
FileWriter fw = new FileWriter("/getevent_humanTime.txt", true);
fw.write(everything);
fw.close();
} catch (IOException e) {
System.out.println("Something happened - here's what I know: ");
e.printStackTrace();
}
} finally {
br.close();
}
}
从每一行获取事件时间戳的方法
public static String getEventTimeStamp(String event_string, String pattern) {
Pattern pTime = Pattern
.compile("\\[.*(\\d\\d\\d\\d\\d\\.\\d\\d\\d\\d\\d\\d)\\].*"
+ pattern);
Matcher mTime = pTime.matcher(event_string);
String time = null;
if (mTime.find()) {
System.out.println("Time " + mTime.group(1));
time = mTime.group(1);
}
return time;
}
将纪元时间转换为人类可读的方法 [round to millisec]
public static Date convertGeteventTimestampToHumanReadable(String uptime,String adbDate , float secTimestamp)
throws ParseException {
System.out.println("DEVICE UPTIME IN SECS " + uptime);
float uptimeMilliSecF = Float.parseFloat(uptime) * 1000;
int uptimeMilliSec = Float.valueOf(uptimeMilliSecF).intValue();
int dmesgtimeinMillisec = (int) (secTimestamp * 1000);
SimpleDateFormat adbDatefmt = new SimpleDateFormat(
"EEE MMM dd hh:mm:ss z yyyy");
Date date = adbDatefmt.parse(adbDate);
System.out.println("ADB DATE: " + date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MILLISECOND, -uptimeMilliSec);
System.out.println("ADB DATE AT DEVICE UP: " + calendar.getTime());
calendar.add(Calendar.MILLISECOND, dmesgtimeinMillisec);
System.out.println("ADB DATE FOR DMESG EVENT: " + calendar.getTime());
return calendar.getTime();
}