-1

Hey guys I have a problem. I have a start/end dates in my database (date,date2) stored as dd/mm/yyyy. I want to test if today's date is between the two dates that I've just mentionned. I tried this boolean function but it doesn't work I don't know why it returns false all the time:

private Boolean test() {
    Boolean bool = false;
    String ch,ch2;
    Date d=new Date();
    Date date2=new Date();
    Date date=new Date();
    try

    {   
        rs=st.executeQuery("select *from mytab");
        if(rs.next())
        {
            ch = rs.getString(2);
            ch2 = rs.getString(3);
            try
            {
                date = new SimpleDateFormat("dd-MM-YYYY", Locale.ENGLISH).parse(ch);
                date2 = new SimpleDateFormat("dd-MM-YYYY", Locale.ENGLISH).parse(ch2);
            }catch(ParseException s){
                System.out.println("Check parsing");
            }
            if(d.before(date2) && d.after(date))
            { 
                bool=true;
            }
        }
    }
    catch (SQLException e){System.out.println("Check the SQl");}    

return bool;
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Abdelaziz Dabebi
  • 1,624
  • 1
  • 16
  • 21
  • If you don't know why, then it's time to debug. – Hovercraft Full Of Eels Aug 14 '14 at 01:04
  • What happens when you run it? Have you tried printing variables in your code to see what they are? If so, what do you see when you print them? – nmore Aug 14 '14 at 01:04
  • 1
    Remember, `Date` carries time information, you're not just comparing the date portion but also the time portion. Do you have an example of the output values you are comparing? – MadProgrammer Aug 14 '14 at 01:06
  • take a look at this http://stackoverflow.com/questions/2592501/how-to-compare-dates-in-java – Kick Buttowski Aug 14 '14 at 01:07
  • yes, System.out.print(ch) prints the date stored in the db: "2010-08-14 00:00:00" System.out.print(date) doesnt, it prints "Sun Dec 31 00:00:00 WET 13 " – Abdelaziz Dabebi Aug 14 '14 at 01:08
  • @user3847203 You need to check your DateFormat, and adjust it to suit your actual input string – starf Aug 14 '14 at 01:10
  • Try something like `YYYY-MM-dd HH:mm:ss` – starf Aug 14 '14 at 01:13
  • Why are the two date values stored in different formats? – MadProgrammer Aug 14 '14 at 01:19
  • @starf yes you are close. my date is "20/08/2020" but it prints it "Mon Dec 30 00:00:00 WET 2019" – Abdelaziz Dabebi Aug 14 '14 at 01:20
  • If you're entering it in with the slashes, that might be your problem. Your formatter expects dashes instead. – Makoto Aug 14 '14 at 01:21
  • @MadProgrammer no, they are stored as dd-mm-yyyy – Abdelaziz Dabebi Aug 14 '14 at 01:23
  • @user3847203 You're own comments state *"start date is 14-08-2010"* and *"end date is 20/08/2020"* those are two different formats...one is `dd-MM-yyyy` and one is `dd/MM/yyyy`...and then you say *"System.out.print(ch) prints the date stored in the db: "2010-08-14 00:00:00""* which is yet another format...? – MadProgrammer Aug 14 '14 at 01:25
  • I'm not sure how this could be any more confusing. Are both dates stored in the same format? – starf Aug 14 '14 at 01:25
  • Two things. Add a `System.out.println` for both `ch` and `ch2` and use `e.printStackTrace();` in both your `catch` clauses, run the code and post the EXACT output... – MadProgrammer Aug 14 '14 at 01:28
  • yes, in the same format, as dd/mm/yyyy – Abdelaziz Dabebi Aug 14 '14 at 01:28
  • Also this is unrelated to answering the question, but consider storing your dates as an epoch time (depends on what else you are doing with these dates). This makes comparisons easy, and makes the code easy to read. – nmore Aug 14 '14 at 01:28
  • @user3847203 You keep contradicting yourself. But, we can assume that the formats are the same. Make sure that the parser is matching the stored values. – starf Aug 14 '14 at 01:30
  • @MadProgrammer okay 1 scd – Abdelaziz Dabebi Aug 14 '14 at 01:30
  • @MadProgrammer this is the output with the DateFormat("YYYY-MM-dd HH:mm:ss"): 2010-08-14 00:00:00 2020-08-20 00:00:00 mI think my problem is to convert these two to be comparable to this date "Date d=new Date();" – Abdelaziz Dabebi Aug 14 '14 at 01:36
  • @user3847203 IF the values are stored in the correct format as compared to the `SimpleDateFormat`, then it should just be a matter of parsing the values with the formatter, that simple. – MadProgrammer Aug 14 '14 at 01:38

1 Answers1

1

Because your date values are stored in two different formats...yea for storing date/time information as text, you need to try multiple formatters in order to parse the values back to date values.

Apache Commons has a API which can do this, but you could write your own as well, for example...

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

  public static void main(String[] args) {
    String startDate = "14-08-2010";
    String endDate = "20/08/2020";

    SimpleDateFormat sdf[] = new SimpleDateFormat[] {
      new SimpleDateFormat("dd/MM/yyyy"),  
      new SimpleDateFormat("dd-MM-yyyy")};

    Date dateStart = parse(startDate, sdf);
    Date dateEnd = parse(endDate, sdf);

    System.out.println("Is today bewteen " + dateStart + " and " + dateEnd);
    Date today = new Date();
    if (today.after(dateStart) && today.before(dateEnd)) {
      System.out.println("...Yes");
    } else {
      System.out.println("...No");
    }
  }

  public static Date parse(String value, DateFormat... formatters) {
    Date date = null;
    for (DateFormat formatter : formatters) {
      try {
        date = formatter.parse(value);
        break;
      } catch (ParseException e) {
      }
    }
    return date;
  }

}

Prints out...

Is today bewteen Sat Aug 14 00:00:00 EST 2010 and Thu Aug 20 00:00:00 EST 2020
...Yes
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366