Reputation: 1142
I have a scenario
start-time = 4:15 PM
end-time = 2:00 AM
if my current-time
(let say 9:15 PM in the same format as start or end time) fall between start-time
and end-time
then goto screen 1
other wise goto screen 2
My question is how do I compare current-time
greater or equals to 'start-time' and less or equals to 'end-time'. I have tried by converting given time values into milliseconds
and compare it but the current-time = 9:15 PM
is appeared to be greater than 'end-time = 2:00 AM', as 2:00 AM will come after mid night means if `9:15 PM day = Thursday then 2:00 AM will be the Friday'. I have searching a lot but can't figure it out. Any type of help will be appreciated.
EDITED:
current-time, start-time and end-time all values taken to be as String
EDIT 2
code 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;
}
Upvotes: 2
Views: 6304
Reputation: 339053
Use a date-time library that supports a time-only value without a date or time zone.
In the Java world, we have two good date-time libraries: Joda-Time and java.time (bundled with Java 8, inspired by Joda-Time, defined by JSR 310). Both libraries offer a LocalTime
class.
Here is some untested code in Joda-Time 2.4 to get you in the right direction.
String inputStart = "4:15 PM";
String inputStop = "2:00 AM";
DateTimeFormatter formatter = DateTimeFormat.forPattern( "h:mm a" );
LocalTime start = formatter.parseLocalTime( inputStart );
LocalTime stop = formatter.parseLocalTime( inputStop );
String now = LocalTime.now( DateTimeZone.forID( "America/Montreal" ) );
boolean isNowInRange = false;
if( start.isEqual( stop ) ) {
isNowInRange = ( now.isEqual( start ) );
} else if ( start.isBefore( stop ) ) {
isNowInRange = ( ( now.isEqual( start ) || now.isAfter( start ) ) && now.isBefore( stop ) );
} else if ( start.isAfter( stop ) ) {
isNowInRange = ( ( now.isEqual( start ) || now.isAfter( start ) ) || now.isBefore( stop ) );
} else {
// FIXME: Handle this supposedly impossible case.
}
Upvotes: 1
Reputation: 15358
Step 1: You simply need to add a day to your end time when it is lower than your start time.
Step 2: Apply your conditions to check whether current time falls in between start time and end time or not
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
}
Upvotes: 8
Reputation: 5973
Here is a sample code to get the current date int timestamp(long millisec)
Long dateTs;
Date dateToday = new Date();
String s = dateToday.toString();
s = s.substring(4, 10) + ", " + s.substring(30, 34);
try {
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH)
.parse(s);
dateTs = date.getTime(); // today 00:00 hours
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Similarly, you can get timestamp for any datetime you want
and then simply compare timestamp with if else from current date.. i can explan more if you feel need for.
Upvotes: 0
Reputation: 36304
From the top of my head, you could do this. You are lazy (some small code would do this), so am I
if start time contains "AM" and current time contains "AM", then just check for values of both to know which one is bigger.
If 1 is true, then if end-time is pm, then return true. Else, check if current time < end time and return true/false.
PS : Follow the same approach if you have PM in case-1. Also, this is certainly, not a good way of achieving what you want.
Upvotes: 0
Reputation: 3993
try this:
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String curTime = sdf.format(dt);
Date start = parser.parse("4:15 PM");
Date end = parser.parse("2:00 AM");
try {
Date userDate = parser.parse(curTime);
if (userDate.after(start)){
....
//you can have your if...else if... conditions
}
if(userDate.before(end)) {
....
}
} catch (ParseException e) {
// Invalid date was entered
}
Upvotes: 4