InnocentKiller
InnocentKiller

Reputation: 5234

Android - How to minus current date with any other date

I have 2 Date-picker in my activity, 1 for Assigning date and other is due date, now what i want to do is when user set's both date i want difference of both the date in text-view.

Ex. if assign date is 12-05-2013 and due date is 14-05-2013 then text-view should show "2 Days Left" I have done with this.

Now the problem come's here, when date changes like if today is 13-05-2013 then i want to show "1 Day's Left" in text-view, it mean text-view should automatically update with current date.

public class Code {
public static Calendar AssignDate;
public static Calendar DueDate;
}

imageViewSubmit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            Code.title = editTextTitle.getText().toString().trim();
            Code.description = editTextDesc.getText().toString().trim();
            Code.diff = Code.DueDate.getTimeInMillis()
                    - Code.AssignDate.getTimeInMillis();
            Code.days = Code.diff / (24 * 60 * 60 * 1000);

            String strDays = String.valueOf(Code.days);
            textViewDiff.setText(strDays);
            String dateDifference = textViewDiff.getText().toString().trim();
        }
    });            

Upvotes: 1

Views: 2178

Answers (2)

ravi
ravi

Reputation: 2792

Hope this example may help you.

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

public class DateDifferenceExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String dateStart = "12/09/2013 09:29:58";
        String dateStop = "12/15/2013 10:31:48";

        // HH converts hour in 24 hours format (0-23), day calculation
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

        Date d1 = null;
        Date d2 = null;

        try {
            d1 = format.parse(dateStart);
            d2 = format.parse(dateStop);

            // in milliseconds
            long diff = d2.getTime() - d1.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);

            System.out.print(diffDays + " days, ");
            System.out.print(diffHours + " hours, ");
            System.out.print(diffMinutes + " minutes, ");
            System.out.print(diffSeconds + " seconds.");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Upvotes: 1

Manitoba
Manitoba

Reputation: 8702

String date1 = "12-05-2013";
String date2 = "14-05-2013";
Calendar date1Calendar = Calendar.getInstance();
date1Calendar.setTime(new SimpleDateFormat("dd-MM-yyyy").parse(inputDateString));
Calendar date2Calendar = Calendar.getInstance();
date2Calendar.setTime(new SimpleDateFormat("dd-MM-yyyy").parse(inputDateString));
if(date2Calendar.after(date1Calendar))
    String daysLeft = "Days Left: " + (date2Calendar.get(Calendar.DAY_OF_MONTH) -(date1Calendar.get(Calendar.DAY_OF_MONTH)));

Upvotes: 0

Related Questions