开发者

check current time between time interval android

I want to create an app that will allow the user to check whether or not the current time falls between a specified time interval. More specifically, I created a sql table using sqllite program, with the table specifying an end time and a start time for each record. The problem is that the type of data each field can be is limited to text, number, and other type other than a datetime type. So, how would I be able to check if the current time is between the start and end time since the format of time is in h:mm and not just a开发者_开发问答n integer value that I could just do less than or greater than? Do I have to convert the current time to minutes?


You should be able to do the comparison even if time is not stored in the datetime type, here is a link that explains the conversion from string to time.

If that doesn't work, convert the time to seconds (int) and calculate the difference.


Try this. You can save and retrieve your time as String:

String to long: String.valueOf()
long to String: Long.valueOf()

Then, you use this procedure to check time:

//Time checker
private boolean timeCheck(long startTimeInput, long endTimeInput) {
    long currentTime = System.currentTimeMillis();
    if ((currentTime > startTimeInput) && (currentTime < endTimeInput)) {
        return true;
    } else {
        return false;         
    }
}

And in your main program check it like this:

//You kept the times as String
if (timeCheck(Long.valueOf(start), Long.valueOf(end))) {
    //it is in the interval
} else {
    //not in the interval
}

I hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜