Xtr*_*per 2 java time android calendar
我有一个场景
start-time = 4:15 PM
end-time = 2:00 AM
Run Code Online (Sandbox Code Playgroud)
如果我current-time
(让我们说下午9:15采用与开始或结束时间相同的格式)介于两者之间start-time
,end-time
然后转到screen 1
其他明智的转到screen 2
我的问题是如何将" current-time
更大"或"等于"与"开始时间" 进行比较,将"等于"与"结束时间"进行比较.我试过将给定的时间值转换成milliseconds
比较但是current-time = 9:15 PM
看起来大于'结束时间=凌晨2:00',因为凌晨2点将在午夜之后出现意味着如果在晚上9:15 =星期四然后凌晨2点将是星期五'.我经常搜索,但无法弄清楚.任何类型的帮助将不胜感激.
编辑:
当前时间,开始时间和结束时间所有值均为 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;
}
Run Code Online (Sandbox Code Playgroud)
第1步:当您的结束时间低于开始时间时, 您只需要添加一天.
第2步: 应用您的条件来检查当前时间是否落在开始时间和结束时间之间
try {
Date mToday = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String curTime = sdf.format(mToday);
Date start = sdf.parse("4:15 PM");
Date end = sdf.parse("2:00 AM");
Date userDate = sdf.parse(curTime);
if(end.before(start))
{
Calendar mCal = Calendar.getInstance();
mCal.setTime(end);
mCal.add(Calendar.DAY_OF_YEAR, 1);
end.setTime(mCal.getTimeInMillis());
}
Log.d("curTime", userDate.toString());
Log.d("start", start.toString());
Log.d("end", end.toString());
if (userDate.after(start) && userDate.before(end)) {
Log.d("result", "falls between start and end , go to screen 1 ");
}
else{
Log.d("result", "does not fall between start and end , go to screen 2 ");
}
} catch (ParseException e) {
// Invalid date was entered
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3838 次 |
最近记录: |