【发布时间】:2014-09-17 12:02:47
【问题描述】:
我有一个场景
start-time = 4:15 PM
end-time = 2:00 AM
如果我的current-time(假设是晚上 9:15,格式与开始或结束时间相同)介于 start-time 和 end-time 之间,则转到 screen 1 其他明智的转到 screen 2
我的问题是如何比较 current-time 大于或等于“开始时间”和小于或等于“结束时间”。我尝试将给定的时间值转换为milliseconds 并进行比较,但current-time = 9:15 PM 似乎大于“结束时间 = 凌晨 2:00”,因为凌晨 2:00 将在午夜之后到来意味着如果` 9:15 PM 天 = 星期四,然后 2:00 AM 将是星期五'。我搜索了很多,但无法弄清楚。我们将不胜感激任何类型的帮助。
已编辑:
当前时间、开始时间和结束时间所有值都取为String
编辑 2
代码spinet:
long currentTime = getMillis("9:15 PM");
long startTime = getMillis("4:15 PM");
long endTime = getMillis("2:00 AM");//this will be the next day's time confusing part for me
if(currentTime >= startTime && currentTime <= endTime)
{
//goto screen 1
}
else
{
// goto screen 2
}
private long getMillis(String givenTime)
{
SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
try {
Date mDate = sdf.parse(givenTime);
long timeInMilliseconds = mDate.getTime();
System.out.println("Date in milli :: " + timeInMilliseconds);
return timeInMilliseconds;
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
【问题讨论】:
-
你的时间怎么样?作为时间实例?或者作为 Date 实例的一部分,或者作为 Calendar 实例的一部分,或者只是作为一个字符串?
-
@TheLostMind 查看我的编辑
-
你如何比较这些对象。可以发一些代码吗?
-
你能用 24hr fromat 作为时间吗?
-
@blackbelt 我用代码编辑了我的问题
标签: java android time calendar