Gaurav kumar
Gaurav kumar

Reputation: 806

Compare Date String in Android

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]);

}

Upvotes: 1

Views: 134

Answers (1)

Techfist
Techfist

Reputation: 4344

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.

Upvotes: 2

Related Questions