1

I have two strings, the first one contains an actual date, and the second one contains a date format.

I want to compare both the strings. Here is my code:

for current date

Date referenceDate = new Date();
    Calendar c = Calendar.getInstance(); 
    c.setTime(referenceDate); 
    c.add(Calendar.MINUTE, -30);
    //c.getTime();

    //System.out.println("Date class"+c.getTime());

    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    currentDateandTime = sdf.format(c.getTime());
    System.out.println("Simple Date Format "+currentDateandTime);

and 2nd date code is here

private void setDate() {


    try {
        sbMain = obj.get("date_of_report").toString().split(" ");
    } catch (JSONException e) {

    }

    String[] sb = sbMain[0].split("-");

    String[] sb2 = sbMain[1].split(":");

    System.out.println("Alert Time : " + sb[0] + " " + sb[1] + " " + sb[2] + " " + sb2[0] + ":" + sb2[1] + ":" + sb2[2]);

}
3
  • hello gaurav, have you looked at date4j library. It helps your in comparing dates efficiently. Commented Dec 6, 2013 at 10:24
  • no no i want to compare two strings only Commented Dec 6, 2013 at 11:33
  • 1
    then you may use regex, and match to see if your date matches some format/expression Commented Dec 6, 2013 at 11:55

1 Answer 1

2

When you have all the components of your second date, e.g Day,time,Month and Year then use the same Calendar class to obtain date object by setting correct time and date components you retrieved, now you have two valid instances of your dates, get the values of both in Millis, and perform whatever comparision you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.