Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By wsaryada

Thread: Comparing Date Strings

  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Comparing Date Strings

    I have a linked list of assignments, each with a due date. Im looking to iterate through this list and for each assignment due date check if the date is after the first date, therefore finding which is due first.
    Below is what i have already. Im finding the first due date and formating the string into a date i can use. Then im going through the list of assignments formatting the due date and checking if it is after the dueDate string i have. I think i have the general idea but im not sure if iths the right way to do it.

    Java Code:
        public void dueDate()
        {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat ValidFormat = new SimpleDateFormat("dd/MM/yyyy");
            ListIterator<Assignment> iterator = assignmentList.listIterator(0);
            
            String dueDate = iterator.next().getDueDate();
            ValidFormat.format(dueDate);
            while(iterator.hasNext())
            {
               String checkDate = ValidFormat.format(iterator.next().getDueDate());
               if(checkDate.after(dueDate))
                   dueDate = checkDate;
               
            }
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Comparing Date Strings

    Turn it into a Date using that SimpleDateFormat object you have.
    Then you can use the after() method on Date to do the comparison (though you'll need to use the getTime() method to get a Date from the Calendar).
    Indeed, you can skip the Calendar and just use a new Date. That'll be the current Date.
    Please do not ask for code as refusal often offends.

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Date Strings

    Ive got this, which seems a bit of a long winded way of doing it because of the parseException. Does it look right to you? Im getting one error here if(checkDate.after(dueDate))
    cannot find symbol
    Symbol: method after(java.lang.String)
    location: variable checkDate of type java.lang.String

    Java Code:
     public void dueDate()
        {
            SimpleDateFormat ValidFormat = new SimpleDateFormat("dd/MM/yyyy");
            ListIterator<Assignment> iterator = assignmentList.listIterator(0);
            
            String dueDate = iterator.next().getDueDate();
            try
            {
                Date date = ValidFormat.parse(dueDate);
                dueDate = ValidFormat.format(date);
            }
            catch(ParseException pe)
            {
                System.out.println("ParseException" + pe);
            }
            
            while(iterator.hasNext())
            {
               String checkDate = ValidFormat.format(iterator.next().getDueDate());
               try
               {
                   Date dateCheck = ValidFormat.parse(checkDate);
                   checkDate = ValidFormat.format(dateCheck);
               }
               catch(ParseException pe)
               {
                   System.out.println("ParseException" + pe);
               }
               
               if(checkDate.after(dueDate))
                   dueDate = checkDate;
               
            }
        }

  4. #4
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Date Strings

    Any idea why netbeans is highlighting after and saying giving me the cannot find symbol message? I thought it might be because the strings havent been changed to dates but as far as i can tell they have been.

  5. #5
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Comparing Date Strings

    Quote Originally Posted by wdh321 View Post
    Ive got this, which seems a bit of a long winded way of doing it because of the parseException. Does it look right to you? Im getting one error here if(checkDate.after(dueDate))
    cannot find symbol
    Symbol: method after(java.lang.String)
    location: variable checkDate of type java.lang.String
    The message is saying that the type of checkDate is String and that the String class has no method after(String x), which of course, is totally true. Look at your code. Is checkDate really supposed to be a String?

  6. #6
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Comparing Date Strings

    Quote Originally Posted by wdh321 View Post
    Any idea why netbeans is highlighting after and saying giving me the cannot find symbol message? I thought it might be because the strings havent been changed to dates but as far as i can tell they have been.
    For the same reason as your error message from your previous post (and my subsequent reply). :-)

  7. #7
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Date Strings

    Yeh i know checkDate is supposed to be a date not a string. From what ive done thats what i hoped to achieve, ive done exactly the same to that string as i have to the first "dueDate" . . . unless they are both wrong.

  8. #8
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Comparing Date Strings

    Even if you called after using a Date object, you are passing it a String, which it won't like. The after method takes a Date.

  9. #9
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Date Strings

    I thought this line Date dateCheck = ValidFormat.parse(checkDate);
    was taking the string and creating a data object out of it which i could then use with the after method.
    Ive tried using dateCheck instead of checkDate but that also doesn't work
    Last edited by wdh321; 04-25-2012 at 11:16 PM.

  10. #10
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Comparing Date Strings

    That means now you have the following code:

    Java Code:
    ...
    if(dateCheck.after(dueDate)) {
        ....
    }
    ...
    And it doesn't work because the dueDate is not a Date type but a String instead. So the call to the after method is failed because it expects a Date as an input.
    Tolls likes this.

  11. #11
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Date Strings

    Thats what i attempted to do before but i hadn't declared the date variables first so it wasnt recognising them as dates. Changed now and problem fixed, thank you for your patience :)

  12. #12
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Date Strings

    Another little question, what would be the easiest way to return the assignment that it finds with the earliest due date?
    At the moment to test it works im converting the date into a string and then printing that out so i can make sure it finds the right one. I could iterate over the linkedlist again and find a matching dueDate to the one iver found . . . . although that seems a bit long and tedious.

  13. #13
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Comparing Date Strings

    It's hard to answer that question since none of that part of the class is visible to us here. Your dueDate method doesn't return anything and does not set any instance variables in its class... For all intents and purposes, it does nothing practical.

    To answer this question, we'd need to know what type of collection you are using for assignmentList (I'll guess it's a List though).

    If you are trying to find the earliest date, you are going to have to search through the whole list to determine that. BUT! Each element in your List has the assignment which contains the due date. What if you were to use a different data structure instead of a list of just assignments. What type of data structure uses a key to access a value. Your key could be your due date. Such a structure would allow you to obtain the value (assignment) given the key (due date). Take a look at Collections and see if anything jumps out at you.

  14. #14
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Comparing Date Strings

    I have to use either array, arraylist or linkedlist for this program.
    At the moment im using linkedList where assignmentList is the name of my linkedList.
    Below is what has been added to the end of the method. Ive just had another thought, as you can get the index from the linkedlist and also print out an item from the linked list by specifying its index then i could add this to the if statement below as the index value will change each time an earlier date is found and i can use the value as an index to find the assignment at that position
    int index = assignmentList.indexOf(iterator.next());

    Java Code:
               if(dateCheck.before(date))
               {
                   date = dateCheck;
               }
               
            }
            
            String dateDue = ValidFormat.format(date);
            System.out.println(dateDue);
    EDIT: Just a note to say ive got it to work using the way i mentioned above, this seems the easiest way to me.
    Last edited by wdh321; 04-26-2012 at 01:59 PM.

  15. #15
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Comparing Date Strings

    Given the constraints of the assignment, that is a good solution. Glad you got it working.

Similar Threads

  1. Comparing Strings
    By Adomini in forum New To Java
    Replies: 3
    Last Post: 02-17-2011, 12:20 AM
  2. need in help in comparing Strings
    By jaq in forum New To Java
    Replies: 1
    Last Post: 11-25-2009, 01:06 PM
  3. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM
  4. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  5. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •