I'm trying to make a function which I want to only work between given time.
i.e: If startTime is "13:00" and endTime is "14:00" then every day when the user uses my app at/between this time then call the function, else do nothing.
Here's what I came across while trying to figure out but this doesn't work.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.UK);
try {
Date inTime = sdf.parse("13:00");
Date outTime = sdf.parse("14:00");
if (outTime != null) {
if (isTimeAfter(inTime, outTime)) {
Toast.makeText(SplashScreenActivity.this, "Time validation success", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(SplashScreenActivity.this, "Exit time must be greater then entry time", Toast.LENGTH_LONG).show();
}
}
} catch (ParseException e) {
e.printStackTrace();
Toast.makeText(SplashScreenActivity.this, "Parse error", Toast.LENGTH_LONG).show();
}
public static boolean isTimeAfter(Date startTime, Date endTime) {
return !endTime.before(startTime);
}
Any help is appreciated, thanks!
UPDATE 1:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.UK);
Date now = new Date(System.currentTimeMillis());
try {
Date inTime = sdf.parse("03:19");
Date outTime = sdf.parse("03:21");
if (now.after(inTime) && now.before(outTime)) {
Toast.makeText(SplashScreenActivity.this, "Time validation success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SplashScreenActivity.this, "Nope, it isn't wokring", Toast.LENGTH_LONG).show();
}
} catch (ParseException e) {
e.printStackTrace();
Toast.makeText(SplashScreenActivity.this, "Parse error", Toast.LENGTH_LONG).show();
}