使用 java 将从 adb shell getevent 获取的时间戳转换为 hh:mm:ss.SSS 格式
Convert timestamp obtained from adb shell getevent to hh:mm:ss.SSS format using java
我正在尝试使用以下命令转换从 adb shell getevent 捕获的时间戳
adb shell getevent -lt /dev/input/event2 >filepath/filename.txt
这给出了如下所示的输出
[ 19393.303318] EV_ABS ABS_MT_TRACKING_ID 00000b87
[ 19393.303318] EV_ABS ABS_MT_POSITION_X 00000180
[ 19393.303318] EV_ABS ABS_MT_POSITION_Y 000004e2
[ 19393.303318] EV_ABS ABS_MT_PRESSURE 0000004c
[ 19393.303318] EV_ABS ABS_MT_TOUCH_MAJOR 00000001
[ 19393.303318] EV_SYN SYN_REPORT 00000000
这里看到的时间戳是19393.303318,不是正常的时间格式
如何使用 JAVA
将其转换为 hh:mm:ss:SSS 格式
如果可能,有没有其他方法可以为 adb shell getevent
获取正确的时间格式
提前致谢
它看起来像是开机后的秒数。要获得启动时刻的绝对时间戳,您可以使用如下内容:System.currentTimeMillis() - SystemClock.elapsedRealtime()
.
这是你的答案。我认为您必须开始使用谷歌搜索您的查询。我不擅长 java,但这花了我 2 个小时来搜索和编写 this.Not 正确安排无论如何这有效,现在我可以使用相同的方法将 dmesg 转换为 adb shell 日期格式.
注意:- 在替换 date and uptime
时使用 - date;cat /proc/uptime
inside adb shell 以同时获取两者(不完全准确)
main method to convert getevent file
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();
}
}
method to get event timestamp from each line
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;
}
method to convert epoch time to human readable [ 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();
}
我终于发现 getevent 的输出是基于 uptimeMillis()
而不是 elapsedRealtime()
。就像 getEventTime
: https://developer.android.com/reference/android/view/InputEvent.html#getEventTime().
我使用这个 uptimeMillis 得到正确的时间戳。
我正在尝试使用以下命令转换从 adb shell getevent 捕获的时间戳
adb shell getevent -lt /dev/input/event2 >filepath/filename.txt
这给出了如下所示的输出
[ 19393.303318] EV_ABS ABS_MT_TRACKING_ID 00000b87
[ 19393.303318] EV_ABS ABS_MT_POSITION_X 00000180
[ 19393.303318] EV_ABS ABS_MT_POSITION_Y 000004e2
[ 19393.303318] EV_ABS ABS_MT_PRESSURE 0000004c
[ 19393.303318] EV_ABS ABS_MT_TOUCH_MAJOR 00000001
[ 19393.303318] EV_SYN SYN_REPORT 00000000
这里看到的时间戳是19393.303318,不是正常的时间格式
如何使用 JAVA
将其转换为 hh:mm:ss:SSS 格式如果可能,有没有其他方法可以为 adb shell getevent
获取正确的时间格式提前致谢
它看起来像是开机后的秒数。要获得启动时刻的绝对时间戳,您可以使用如下内容:System.currentTimeMillis() - SystemClock.elapsedRealtime()
.
这是你的答案。我认为您必须开始使用谷歌搜索您的查询。我不擅长 java,但这花了我 2 个小时来搜索和编写 this.Not 正确安排无论如何这有效,现在我可以使用相同的方法将 dmesg 转换为 adb shell 日期格式.
注意:- 在替换 date and uptime
时使用 - date;cat /proc/uptime
inside adb shell 以同时获取两者(不完全准确)
main method to convert getevent file
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();
}
}
method to get event timestamp from each line
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;
}
method to convert epoch time to human readable [ 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();
}
我终于发现 getevent 的输出是基于 uptimeMillis()
而不是 elapsedRealtime()
。就像 getEventTime
: https://developer.android.com/reference/android/view/InputEvent.html#getEventTime().
我使用这个 uptimeMillis 得到正确的时间戳。