转换为毫秒的时间

时间:2013-08-10 12:22:24

标签: android time calendar

我想检查设置时间是否小于日历(移动设备)时间。
它显示警报设置,如果设置时间不超过1.5小时,则显示“ruko ruko”消息。如果两个条件都是假的,那么它将转到其他条件。
它没有给我预期的结果。这是我的代码:

 GregorianCalendar mcalender=new GregorianCalendar();
mcalender.set(2013, 7, 17, 13, 0);

if(mcalender.getTimeInMillis()<=cal.getTimeInMillis()){
    Toast.makeText(getBaseContext(), "sets ", Toast.LENGTH_SHORT).show();
}
else if(mcalender.getTimeInMillis()+5400000<=cal.getTimeInMillis()){
    Toast.makeText(mContext, "ruko ruko", Toast.LENGTH_SHORT).show();
}
else{
    Toast.makeText(getBaseContext(), "Time not gone ", Toast.LENGTH_SHORT).show();
}

1 个答案:

答案 0 :(得分:1)

问题是首先检查mcalender.getTimeInMillis()。例如,当您将闹钟设置为5:30时,在4:30,第一部分将为true,因此else if部分将被完全跳过。只是要扭转顺序,这样做:)

    if(mcalender.getTimeInMillis()+5400000<=cal.getTimeInMillis()){
        //If still have more than 1.5 hours to go (i.e. 2 hours left etc)
        Toast.makeText(mContext, "ruko ruko", Toast.LENGTH_SHORT).show();
    }
    else if(mcalender.getTimeInMillis()<=cal.getTimeInMillis()){
        //Not more than 1.5 hours, but still not time up yet.
        Toast.makeText(getBaseContext(), "sets ", Toast.LENGTH_SHORT).show();
    }
    else{
        //Time is up. Run action.
        Toast.makeText(getBaseContext(), "Time not gone ", Toast.LENGTH_SHORT).show();
    }