我从启动器和系统的时间得到了时间...我想比较它们并确保差异不超过100毫秒
public void test() throws InterruptedException {
/*
* Get the visible time from the launcher
*/
TextView onLauncherDisplay = (TextView) mNextUIMainActivity.findViewById(R.id.timeOfDay);
System.out.println("Launcher time: " + onLauncherDisplay.getText().toString());
/*
* Get the system time
*/
Calendar currentSystemTime = Calendar.getInstance();
currentSystemTime.setTime(new Date());
Calendar currentSystemTImeInMIlliseconds = Calendar.getInstance();
currentSystemTImeInMIlliseconds.seu
/*
* Convert time into 12-hour format
*/
SimpleDateFormat date = new SimpleDateFormat("h:mm aa");
Calendar onLauncherTime = Calendar.getInstance();
try {
onLauncherTime.setTime(date.parse(onLauncherDisplay.getText().toString()));
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("System time: " + currentSystemTime.get(Calendar.HOUR) + ":" + currentSystemTime.get(Calendar.MINUTE));
我在这里比较时间......小时和分钟,但这并不完美。我想转换成毫秒,差异不应超过100毫秒
/*
* Compare the Launcher time with the System time
*/
if ((onLauncherTime.get(Calendar.HOUR) == currentSystemTime.get(Calendar.HOUR)) || (currentSystemTime.get(Calendar.HOUR) - onLauncherTime.get(Calendar.HOUR) == 1)) {
if (onLauncherTime.get(Calendar.MINUTE) == currentSystemTime.get(Calendar.MINUTE) || (currentSystemTime.get(Calendar.MINUTE) - onLauncherTime.get(Calendar.MINUTE) == 1)) {
System.out.println("Launcher time matches the On-System time!");
}
} else {
System.out.println("Launcher time does not matches the On-System time!!");
}
答案 0 :(得分:1)
Calendar
类有一个method getTimeInMillis
,它返回给定日历的unix纪元时间值。那就是说,我不完全明白你在做什么......
答案 1 :(得分:1)
使用Calendar.getTimeInMillis()
:
if (currentSystemTime.getTimeInMillis() - onLauncherTime.getTimeInMillis() < 100)
//difference is less than 100 milliseconds