使用java将从adb shell getevent获得的时间戳转换为hh:mm:ss.SSS格式
我试图使用以下命令转换从adb shell getevent捕获的时间戳使用java将从adb shell getevent获得的时间戳转换为hh:mm:ss.SSS格式,java,android,datetime,adb,adb-shell,Java,Android,Datetime,Adb,Adb Shell,我试图使用以下命令转换从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 [ 1
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个小时来搜索和编写这个。无论如何,没有正确安排这项工作,现在我可以使用相同的格式将dmesg转换为adb shell日期格式。
注:-在替换日期和正常运行时间时使用日期;在adb外壳内同时获取cat/proc/uptime(不完全准确)
转换getevent文件的主方法
方法从每行获取事件时间戳
将历元时间转换为人类可读[四舍五入到毫秒]的方法
我最终发现getevent的输出是基于
uptimeMillis()
而不是elapsedRealtime()
。就像getEventTime
:
我使用此uptimeMillis获取正确的时间戳。我想将时间戳转换为HH:mm:ss格式,假设您从输出中提取的值为
长
(您可以使用例如扫描仪
)。然后您可以将其添加到上面提到的启动时间中,并将这样的时间戳总和传递到新的SimpleDataFormat(“hh:mm:ss”)。格式(新日期(时间戳))是SystemClock。elapsedRealtime()是android方法吗?是:否。我想要一个java@AlexP. 在Java中有没有一种将时间转换为时间的方法?C语言中有一种将时间戳转换为可读格式的解决方案。感谢您发布答案,我们将查看此方法是否有效,并会回复您任何问题
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;
}
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();
}