我已将用户输入的时间和日期视为:
DatePicker dp = (DatePicker) findViewById(R.id.datePicker);
TimePicker tp = (TimePicker) findViewById(R.id.timePicker);
int year = dp.getYear();
int month = dp.getMonth();
int date = dp.getDayOfMonth();
int hour = tp.getCurrentHour();
int min = tp.getCurrentMinute();
并且系统的当前日期和时间为:
Calendar cal = Calendar.getInstance();
int Syear = cal.get(Calendar.YEAR);
int Smonth = cal.get(Calendar.MONTH);
int Sdate = cal.get(Calendar.DAY_OF_MONTH);
int Shour = cal.get(Calendar.HOUR_OF_DAY);
int Smin = cal.get(Calendar.MINUTE);
假设用户不会从系统的当前日期和时间设置过去的日期和时间。现在我需要用户输入和系统当前日期之间的差异,时间以分钟或秒或毫秒为单位。我怎样才能做到这一点?任何例子,教程或任何东西都会有所帮助。谢谢。
答案 0 :(得分:2)
要获得以毫秒为单位的差异,您可以执行以下操作:
Calendar a = Calendar.getInstance();
Calendar now = Calendar.getInstance();
a.set(year, month, day, hour, min);
long difference = a.getTimeInMillis()-now.getTimeInMillis();