Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check a particular date is within past 24 hours or not?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 311
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: