我有2个时间戳,代表开始和结束。这意味着开始时间必须早于结束。
我的想法是使用Calendar
类并使用getTimeInMillis
进行验证,但它不起作用:
Calendar calFrom = Calendar.getInstance();
Calendar calTo = Calendar.getInstance();
calFrom.set(Calendar.HOUR_OF_DAY, timeSchedule.getCurrentHour());
calFrom.set(Calendar.MINUTE, timeSchedule.getCurrentMinute());
calTo.set(Calendar.HOUR_OF_DAY, toTime.getCurrentHour());
calTo.set(Calendar.MINUTE, toTime.getCurrentMinute());
if(calFrom.getTimeInMillis() > calTo.getTimeInMillis())
{
Crouton.makeText(MyActivity.this, "error", Style.ALERT).show();
}
当我调试上述代码时,虽然timeSchedule
早于toTime
,但calFrom.getTimeInMillis()
的值大于calTo.getTimeInMillis()
。
请帮助我,谢谢你的时间。
答案 0 :(得分:1)
int fromHour = calFrom.getCurrentHour();
int toHour = calTo.getCurrentHour();
int fromMin = calFrom.getCurrentMinute();
int toMin= calTo.getCurrentMinute();
if (fromHour >toHour )
{
System.out.println ("from time is greater");
}
else if (fromHour==toHour)
{
if (fromMin>toMin)
{
System.out.println ("from time is greater");
}
else
{
System.out.println ("To time is greater");
}
}
else
{
System.out.println ("To time is greater");
}