Sometimes we need to check whether a date is within past 24 hours or not. Generally we implement this kind of functionality when we create an AuthToken or a code that we want to to be valid for some time duration.
Example: In the below example I'm checking the provided date is within 24 hours or not.
/**
* Check date is within 24 hours or not
* @param generatedDate
* @return
*/
public static boolean checkGeneratedCodeTime(Date generatedDate)
{
Calendar calendar = Calendar.getInstance();
long diff = calendar.getTimeInMillis() - generatedDate.getTime();
long diffHours = diff / (60 * 60 * 1000);
System.out.println("diffHours: "+ diffHours);
if (diffHours >= 24)
{
System.out.println("false: ");
return false;
}
else
{
System.out.println("true: ");
return true;
}
}
Hope this will help you :)
0 Comment(s)