0

The problem here is that i have the following code:

String newPattern = "yyyy-MM-dd HH:mm:ss.SSS";
SimpleDateFormat formatterDB = new SimpleDateFormat(newPattern);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

String nDate1 = rs.getString("FromDate");
String nDate2 = rs.getString("ToDate");
Date newDate1 = formatterDB.parse(nDate1);
Date newDate2 = formatterDB.parse(nDate2);

Date sd = formatter.parse("2013-08-22");
Date ed = formatter.parse("2013-08-24");
Date exd = formatter.parse("2013-08-23");

The first condition to be verified is that exd lies between [sd,ed] and if true then exd should also lie between [newDate1,newDate2] and if this is true then other elements from the dB will be fetched.

Please not that the source of both dates are different, one date is from the dB having a different format entirely whereas the other date is being given as a String input and they need to be compared, so they have got different format.

To make it more clear, here is an example:

I gave input for exd as 2013-08-23 the input for sd and ed is given the newDate1 is 2013-08-28(in dB format) and newDate2 is 2013-09-01(in dB Format) Still it accepts the input exd and fetches the other column.

** I have snipped the relevant code, for any issues, drop in a comment. Any Help is appreciated. Thanks in Advance!

D3X
  • 547
  • 3
  • 20

4 Answers4

2

You must use the java inbuilt date functionality.

Here is a small example :

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");

if(date1.compareTo(date2)>0){
    System.out.println("Date1 is after Date2");
}else if(date1.compareTo(date2)<0){
    System.out.println("Date1 is before Date2");
}else if(date1.compareTo(date2)==0){
    System.out.println("Date1 is equal to Date2");
}else{
    System.out.println("How to get here?");
}

Edit : Example with respect to user's question

exd is not between newDate1 and newDate2 !!

public class DateCompare {

public static void main(String args[]) throws ParseException
{
    String newPattern = "yyyy-MM-dd HH:mm:ss.SSS";
    SimpleDateFormat formatterDB = new SimpleDateFormat(newPattern);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    String nDate1 = "2013-08-29 11:11:11.000";
    String nDate2 = "2013-09-01 11:11:11.000";
    Date newDate1 = formatterDB.parse(nDate1);
    Date newDate2 = formatterDB.parse(nDate2);


    Date sd = formatter.parse("2013-08-22");
    Date ed = formatter.parse("2013-08-24");
    Date exd = formatter.parse("2013-08-23");


    //Comparing exd to sd and ed
    if(sd.compareTo(exd) * exd.compareTo(ed) > 0)
    {
        //comparing exd to newDate1 and newDate2
        if(newDate1.compareTo(exd) * exd.compareTo(newDate2) > 0)
        {
            System.out.println("Success : Now you can fetch data from DB !");
        }
        else
        {
            System.out.println("Oops ! Can't fetch data !");
        }
    }
  }
}

Please note that in this scenario, if the dates in different formats are compared, if the dates are same, they won't return 0, because of the time factor ! The time will also be compared and dates in yyyy-MM-dd format have HH:mm:ss.SSS all as 00:00:00.000

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • the source of both dates are different, one date is from the dB having a different format entirely whereas the other date is being given as a String input and they need to be compared, so they have got different format. although the comparison thing might work. Thanks! – D3X Feb 27 '14 at 05:27
  • The date from DB can be fetched, converted to String and then compared ! – ItachiUchiha Feb 27 '14 at 05:29
  • In the snippet given nDate1 and nDate2 are from the dB directly. and there is another string sd and ed there which are given as string inputs the db format is the newPattern and the other is the string format – D3X Feb 27 '14 at 05:31
  • The format doesn't matter as long. As long as they are `Date type they can be compared` – ItachiUchiha Feb 27 '14 at 05:53
  • I tried using your approach but the date which reflects after comparison is wrong it does not belong to the group specified. – D3X Feb 27 '14 at 05:57
  • @D3X please see the answer, I have edited it wrt to your question ! – ItachiUchiha Feb 27 '14 at 06:06
  • I have already used this approach, i saw it online before posting the question http://www.mkyong.com/java/how-to-compare-dates-in-java/ That is giving me a very odd result. I gave input for exd as 2013-08-23 the input for sd and ed is given the newDate1 is 2013-08-28(in dB format) and newDate2 is 2013-09-01(in dB Format) Still it accepts the input exd and fetches the other column. – D3X Feb 27 '14 at 06:14
  • See the new updated answer, all the values as specified in your comment. It doesn't fetch the data ! – ItachiUchiha Feb 27 '14 at 06:23
  • thanks! actually i was using exd.compareTo(newDate1). So it was giving an invalid response. I will just check if it fetches the dB data properly. – D3X Feb 27 '14 at 06:26
  • actually when now when i give a date within the range too it is not accepting, just tried it with real time data – D3X Feb 27 '14 at 06:34
  • Also, I dont want to consider the timings because, the time is basically the entity creation time which is of no use to me – D3X Feb 27 '14 at 06:35
  • @D3X if you are not concerned about time, you will have to reparse newDate1/newDate2 to yyyy-MM-dd using this http://stackoverflow.com/questions/3469507/how-can-i-change-the-date-format-in-java or make all the time as 0. You can use http://stackoverflow.com/questions/7784067/compare-date-without-time – ItachiUchiha Feb 27 '14 at 06:42
1

Here is one way of dealing with the problem ....

int flag=0;    
  if(exd.compareTo(sd)>0 )
       if(exd.compareTo(ed)<0){
             System.out.println(exd+" Lies between"+ sd +"and"+ ed);  
             flag=1;
       } 
  /*convert from newPattern to "yyyy-MM-dd"*/
  SimpleDateFormat formatterDB = new SimpleDateFormat(newPattern);
  Date temp = formatterDB.parse(nDate1);
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  String temp1 = formatter.format(temp);
  Date newDate1 = formatter.parse(temp1); 
  if(flag==1)
     if(exd.compareTo(newDate1)>0)
         if(exd.compareTo(newDate2)<0){
               /* your code to fetch other item from DB*/
         }
Arindam Das
  • 206
  • 1
  • 7
  • 29
  • exd and newDate1 and newDate2 being in different formats are not getting compared and giving erroneous results. Anyway, good approach. Thanks! – D3X Feb 27 '14 at 05:43
  • I have added an approach for Date conversion method. – Arindam Das Feb 27 '14 at 06:03
  • can you just check the example i posted. – D3X Feb 27 '14 at 06:17
  • Just To make it clear, nDate1 & nDate2 are in DB format (yyyy-MM-dd HH:mm:ss.SSS) and exd is in "yyyy-MM-dd" format ? And You need to convert from default DB format to "yyyy-MM-dd" format ? If so then use the above date conversion method .... it should work. – Arindam Das Feb 27 '14 at 06:21
1

Short Answer

These two answers of mine have both the concepts and example code you need to neatly solve your problem:

Date versus Date-Time

You have some date-only values and some date-time values. You need to make a decision as to what a date-only value means in relationship to the date-time.

One possibility is that a date-only is considered to be the first moment of the day, such as 2014-01-02 00:00:00. Note that the time of first moment is usually 00:00:00, but not necessarily because Daylight Saving Time (DST) or other anomalies could move the time. For example, 01:00:00. Joda-Time will automatically adjust the time to first moment of day when parsing a date-only string into a date-time. But be aware that you can also call withTimeAtStartOfDay (ignore the "midnight"-related classes/methods as they are virtually deprecated by the creators).

Another possibility is that a date-only should be represented as a span of time, from first moment of the day to the first moment of the next day.

Half-Open

Note that I am referring to the "Half-Open" approach where the beginning is inclusive and the ending is exclusive. Half-open generally works best for spans of time.

For more discussion and examples of Half-Open time spans, see this answer and this answer of mine.

Parse First, Process Second

Get your date strings and date-time strings all parsed into date-time objects first. Once you have some reasonable objects in hand, then move on to your comparisons and business logic.

Joda-Time

The java.util.Date and .Calendar classes are notoriously troublesome. Avoid them.

Instead use either the open-source library Joda-Time or the new java.time package in Java 8.

Joda-Time offers three classes to handle a span of time: Period, Duration, and Interval. They include very handy methods of comparison, including contains, abuts, overlap, and gap.

Search StackOverflow

Search for "joda interval" to find other examples.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

If you're just comparing two dates, where one has precision down to time, you should just truncate the time so both just represent Date.

Using Apache commons:

DateUtils.truncate(dateYouWantToTruncate, Calendar.Date);

Because your formatter is using ISO-8601 (year first, then month, then day), you can use the formatter you created to convert both dates to Strings and compare the strings.

formatter.format(sd).compareTo(formatter.format(newDate1));

You can also use the Calendar class. It's fairly verbose, but would work.

phatfingers
  • 9,770
  • 3
  • 30
  • 44