Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6124723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:09:55+00:00 2026-05-23T16:09:55+00:00

I am having a problem when i am trying to compare two dates in

  • 0

I am having a problem when i am trying to compare two dates in Android. I am not sure if it is a problem with the emulator or if i have a problem in the code itself.
The thing is the code works in a normal Java program environment, which confuses me even more.

I have the following code to compare dates in Android 2.1 :

public boolean compareDates(String givenDateString) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    boolean True;
    try {
        True = false;
        Date givenDate = sdf.parse(givenDateString);
        Date currentDate = new Date();

        if(givenDate.after(currentDate)){
            True = true;
        } if(givenDate.before(currentDate)){
            True = false;
        } if(givenDate.equals(currentDate)){
            True = false;
        }   
    } catch (Exception e) {
        Log.e("ERROR! - comparing DATES", e.toString());
    }
    return True;
}

now the code works well in Java, but in Android it keeps returning me false.
The only change happens when i insert the currentDate variable with a string like this:

Date currentDate = sdf.parse("16:50");

With the currentDate variable set in a string it returns true when i compare it with a value that’s after the time given. I have also tried setting the currentDate variable with:

  Calendar calendar = Calendar.getInstance();
  Date currentDate = calendar.getTime();

I am completely at a loss here. Hoping someone has any ideas on what might be a problem here.

— EDIT —

Found the solution for my problem. I used the calendar and read the hour and minute from there, then i put them in a string to parse and it worked. The code now looks like this:

public boolean compareDates(String givenDateString) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    boolean True = false;
    try {
        Date givenDate = sdf.parse(givenDateString);
        Calendar now = Calendar.getInstance();
            int hour = now.get(Calendar.HOUR_OF_DAY);
            int minute = now.get(Calendar.MINUTE);
        Date currentDate = sdf.parse(hour + ":" + minute);

        if(givenDate.after(currentDate)){
            True = true;
        } if(givenDate.before(currentDate)){
            True = false;
        } if(givenDate.equals(currentDate)){
            True = false;
        }   
    } catch (Exception e) {
        Log.e("ERROR! - comparing DATES", e.toString());
    }
    return True;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:09:55+00:00Added an answer on May 23, 2026 at 4:09 pm

    When I’m doing comparisons on dates, I make sure to use Calendar objects. Then I use the compareTo() function:

    if ( myCalendar.compareTo(upperLimitCalendar) >= 0 )
    

    See the documentation here:

    http://developer.android.com/reference/java/util/Calendar.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i'm having a bit of a problem trying to compare two strings declared
I am trying to convert this test code to C# and having a problem
I have been trying to tackle this problem , but I am having difficulty
I am having a problem in this code. what i am trying to do
Having a problem trying to create a function, as part of a BizTalk helper
I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
I'm trying to find an answer for a problem my developers are having. I
I'm trying to do some basic paging in MSSQL. The problem I'm having is
Ok, so here's the full description of the problem I'm having: I am trying
I have been having some problems trying to get my PHP running. When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.