0

I am trying to compare two dates placed in the main method. I needed to come up with a logic that will compare the two dates, to see if one or the other are the same date, or which one came before.

I am having a bit of trouble with getting the values into my boolean method. The methods have to be seperate - Main method has to hold the date values, and the boolean methods have to deal with the logic within the class.

     my main and boolean so far

public static void main(String[]args)
{
SimpleDate one = new SimpleDate("1999-01-23");
SimpleDate two = new SimpleDate("1998-03-23");
//one.displayDate();
//two.displayDate();
}

/*
public boolean isAfter(int SimpleDate){
boolean isAfter = false;
isAfter = one.equals(two);

if(isAfter == true);
System.out.println ("true");

else
return false;

}

Does anyone have any advice? or pointers? and also, my "else" statement at the end keeps giving me errors as well.

4 Answers4

1

try this, from a project I worked with earlier.

private boolean isValidDateFormat(String dateStr) {
    return dateStr.matches("\\d{2}-\\d{2}-\\d{4}");
}

private boolean isValidFutureDate(String dateStr) {
    if (!isValidDateFormat(dateStr)) {
        return false;
    }
    String[] parts = dateStr.split("-");
    Calendar c = Calendar.getInstance();
    c.set(Integer.parseInt(parts[2]), Integer.parseInt(parts[1]),
            Integer.parseInt(parts[0]));
    Calendar today = Calendar.getInstance();
    if (c.after(today)) {
        return true;
    } else {
        return false;
    }
}

private boolean isValidBeforeDate(String dateStr) {
    if (!isValidDateFormat(dateStr)) {
        return false;
    }
    String[] parts = dateStr.split("-");
    Calendar c = Calendar.getInstance();
    c.set(Integer.parseInt(parts[2]), Integer.parseInt(parts[1]),
            Integer.parseInt(parts[0]));
    Calendar today = Calendar.getInstance();
    if (c.before(today)) {
        return true;
    } else {
        return false;
    }
}

Basically what you can do is, accept 2 arguments and parse them to 2 arrays, and use the Calendar class to create an instance of two different dates, from there use the .before and .after method that the Calendar provides to check, you can also use compareTo(date)==0 to check for today's date.

Mc Kevin
  • 962
  • 10
  • 31
0

Use java.util.Date, compareTo method, which does what you need.

Ale Zalazar
  • 1,875
  • 1
  • 11
  • 14
0

The short answer is that dates cannot be compared in this manner. Check this link for different options: How to compare dates in Java?

Community
  • 1
  • 1
ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • Firstly, that is not the problem causing the error. Secondly, the SimpleDate class isn't part of the standard java API, so you may be able to compare ( call Object.equals() ) on them with meaningful results. – Pete Kirkham Feb 17 '14 at 00:53
0

I would split the Strings one and two at the - and put them into arrays, one for year, one for month, and one for the day. Then, use the .parseInt(); command to change each String[] to int[] and compare them as integers. First compare the years, then the months, then the days.

I can write up some sample code if you would like.

    String[][] sorted = new String[2][3]; //hold sorted data

    String[] unsorted = new String[2]; //hold unsorted data

    unsorted[0] = one;
    unsorted[1] = two;

    int[] year = new int[2];
    int[] month = new int[2];
    int[] day = new int[2];

    for (int i = 0; i < 2; i++){            
        sorted[i] = unsorted[i].split("-"); //sort data         
    }

    //place sorted data into seperate arrays to make calculations more logical to look at
    for (int i = 0; i < 2; i++){
        year[i] = Integer.parseInt(sorted[i][1]);
        month[i] = Integer.parseInt(sorted[i][2]);
        day[i] = Integer.parseInt(sorted[i][3]);        
    }

Basically, this splits up the dates into three separate arrays called year, month, and day. Now you can use comparator or if statements to check to see which date is larger.

ylun.ca
  • 2,504
  • 7
  • 26
  • 47
  • The sample code would be great, but my biggest issue right now is getting the values from main, into the boolean method =( – user2517185 Feb 17 '14 at 00:44
  • You have a problem in you isAfter method signature, it will probably need to be something like public boolean isAfter(SimpleDate one, SimpleDate two). Because one and two variables aren´t visible from inside the method, so you have to pass them as parameters. – Ale Zalazar Feb 17 '14 at 00:46
  • Or, you could just concatenate the date. From 1999-01-23, change it to 19990123 (do the same thing with the other one). Then, compare both. – wipindipy10 Feb 17 '14 at 00:48
  • SimpleDate.java:46: error: expected public boolean isAfter(int SimpleDate.one, int SimpleDate.two) Is what happens when i add those in – user2517185 Feb 17 '14 at 00:51
  • 1
    @user You're doing things without knowing why or how to do them. Please stop and review your notes or some tutorials. You're not going to get anywhere like this. – Sotirios Delimanolis Feb 17 '14 at 00:53
  • @user2517185, check my latest edit. It is based off of your current code. All you have to do now is add the checks for which date is larger. – ylun.ca Feb 17 '14 at 01:00