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 8354181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:28:36+00:00 2026-06-09T09:28:36+00:00

I am currently working on some code that requires a comparison of dates as

  • 0

I am currently working on some code that requires a comparison of dates as follows:

public int compare(ItemLocation o1, ItemLocation o2) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
                Date date1 = sdf.parse(o1.getDatePublished());
                Date date2 = sdf.parse(o2.getDatePublished());
                Calendar cal1 = Calendar.getInstance();
                Calendar cal2 = Calendar.getInstance();
                cal1.setTime(date1);
                cal2.setTime(date2);
                if(cal1.equals(cal2)) { 
                    return 0; 
                } else if(cal1.before(cal2)) {
                    return -1;
                } else if(cal1.after(cal2)) {
                    return 1;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

So my question is muti-part.

  1. is this the best method to go about comparing 2 dates?
  2. with the “the method must return a result of type int” error msg, what is the best way to go about fixing this? (I don’t think that adding a return 0; at the end is very practical…which is what I was thinking)
  3. [optional] is there a more efficient way to write the 3 if/else if statements?

Thanks guys!

  • 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-06-09T09:28:37+00:00Added an answer on June 9, 2026 at 9:28 am

    Looks like the good method to compare dates.
    For the compilation error, rethrow an exception in your catch block or return an int value.

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

Sidebar

Related Questions

I'm currently working on a plug-in for Eclipse that translates some custom Java code
I am currently working on some Ajax heavy code and I am wondering how
I am currently working with some users that do not have the .NET Framework
I'm currently working on a project that requires me to match our database of
I'm working on a project that requires Kendo UI. We are currently working with
Currently I am working on a bit of code which (I believe) requires quite
I'm currently working on a python project, that requires file transmission from a client
I have some terribly written ASP.NET code that is just not working right (go
The system I am currently working on requires some role-based security, which is well
I'm currently working on an app for the Android OS that displays some data.

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.