Java 将时间转换为毫秒
我从发射器和系统中获得了时间…我想比较它们,确保差异不超过1亿毫秒Java 将时间转换为毫秒,java,android,time,Java,Android,Time,我从发射器和系统中获得了时间…我想比较它们,确保差异不超过1亿毫秒 public void test() throws InterruptedException { /* * Get the visible time from the launcher */ TextView onLauncherDisplay = (TextView) mNextUIMainActivity.findViewById(R.id.timeOfDay); System.out
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));
/*
* 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!!");
}
在这里,我比较了时间…小时和分钟,但它并不完美。我想转换成毫秒,差值不应该超过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));
/*
* 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)
//差值小于100毫秒
但它并不完美
它怎么不完美?到底出了什么问题?我想将启动时间和系统时间转换为毫秒onLauncherTime的可能副本。getTimeInMillis()无法正常工作。注意更具体一点吗?对于onLauncherTime,我认为默认时间为1970年1月1日……而对于当前时间,则假定为今天的日期