繁体   English   中英

将时间转换为毫秒

[英]Convert time into milliseconds

我从启动器那里得到了时间,从系统那里得到了时间...我想比较它们,并确保相差不超过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!!");
}

Calendar类具有方法 getTimeInMillis ,该方法返回给定日历的unix epoc时间值。 就是说,我不完全了解您在做什么...

使用Calendar.getTimeInMillis()

if (currentSystemTime.getTimeInMillis() - onLauncherTime.getTimeInMillis() < 100)
   //difference is less than 100 milliseconds

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM