检查第一次获取时间是否早于第二次获取时间

时间:2015-01-17 06:44:30

标签: java android

我有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()

请帮助我,谢谢你的时间。

1 个答案:

答案 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");
}