5

I want to compare two dates in my application

The first date will be todays date The second will come from a database

To save the second date my code is as follows; (Used tomorrows date to keep it simple)

 Calendar calendar = Calendar.getInstance();
 calendar.add(Calendar.DAY_OF_YEAR, 1);
 Date tomorrow = calendar.getTime();
 DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");

 String tomorrowAsString = dateFormat.format(tomorrow);

So tomorrowAsString (10-Oct-2015) is stored in a table in my database

In another form I'm retrieving that date and putting it into a String

String steepingDate = (c.getString(3));

I imagine that I'll need to convert that string into a date format and then use the following to get todays date;

Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");

String todayAsString = dateFormat.format(today);

Here is where I'm getting stuck as I'm not sure how to turn the steepingDate into a Date and then what is the actual way of comparing the strings, I've tried looking online but there's so many different ways that I've tried and none so far have worked. I want to know which one of the two is grater

EDIT:

I've combined some of the answers and come up with the following;

do {
      String steepingDate = (c.getString(3));
      SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
      Date steepingdate = formatter.parse(steepingDate);

      Calendar calendar = Calendar.getInstance();
      Date today = calendar.getTime();

      if(today.compareTo(steepingdate)>0)
      {
         CompleteSteeping.add(c.getString(1));
      }
      i += 1;
   }while (c.moveToNext());

However on;

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date steepingdate = formatter.parse(steepingDate);

I'm getting an error stating

'Unparseable date: "java.util.GregorianCalender[time=1444461352665,areFieldsSet=true,Ienient=true\zone=GMT,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2015,MONTH=9,WEEK_OF_YEAR=41,WEEK_OF_MONTH=2,DAY_OF_MONTH=10,DAY_OF_YEAR=283,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=7,HOUR_OF_DAY=7,MINUTE=15,SECOND=52,MILLISECOND=665,ZONE_OFFSET=0DST_OFFSET=0]" (at offset 0)

Sjharrison
  • 729
  • 3
  • 16
  • 39

3 Answers3

4

If you just want to compare the Strings it would look like this:

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_YEAR, 1);
    Date tomorrow = calendar.getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

    String tomorrowAsString = dateFormat.format(tomorrow);

    calendar = Calendar.getInstance();
    Date today = calendar.getTime();

    String todayAsString = dateFormat.format(today);

    if(tomorrowAsString.equals(todayAsString))
        System.out.println(true);
    else
        System.out.println(false);
Surro
  • 298
  • 1
  • 7
  • Thanks for your answer, I should have been more clear in my question it's not just knowing if they are equal or not it's knowing which one is grater – Sjharrison Oct 09 '15 at 08:18
  • So then should the answer from Amit Desale work for you. ;) – Surro Oct 09 '15 at 08:21
  • I've updated my question as getting an error on what I've tried – Sjharrison Oct 09 '15 at 08:23
  • 1
    Use SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); It just can´t handle dd-MM-yyyy. – Surro Oct 09 '15 at 08:36
  • Your right, I changed the original format when the data is being stored to database from dd-MMM-yyyy to dd/MMM/yyyy and it's now working Thanks everyone for your help – Sjharrison Oct 09 '15 at 08:41
1

Here is where I'm getting stuck as I'm not sure how to turn the steepingDate into a Date

You need to instantiate a SimpleDateFormat with a patter that matches the String you saved, and call parse on it.

and then what is the actual way of comparing the strings, I've tried looking online but there's so many different ways that I've tried and none so far have worked

if you have to compare date to understand if one is less/grater/equal than, then Date has the compareTo method, which returns an int < 0 if this Date is less than the specified Date, 0 if they are equal, and an int > 0 if this Date is greater.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I've updated my question as getting an error on what I've tried – Sjharrison Oct 09 '15 at 08:24
  • It's in the question `tomorrowAsString (10-Oct-2015)` but it's the string is called steepingDate However I've just noticed that I had dd/MM/yyyy so I've now changed it to dd-MM-yyyy but still getting an error – Sjharrison Oct 09 '15 at 08:32
  • are you getting the same error ? Could you update the question posting the changes you made and post the stacktrace of the exception ? – Blackbelt Oct 09 '15 at 08:34
1

You can convert from String date to Date and compare it

Use the SimpleDateFormat class:

private Date parseDate(String date, String format) throws ParseException
{
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    return formatter.parse(date);
}
Usage:

Date date = parseDate("19/05/2009", "dd/MM/yyyy");
winston
  • 875
  • 1
  • 8
  • 15