简体   繁体   中英

how to calculate the difference between 2 date in android?

i am creating a method that calculate the difference between 2 date (END DATE _ CURRENT DATE). i want to display the date format in months , days , hours, minutes, seconds

like this format : 2 months.3 days. 8 hours. 12 min. 32 second

can anyone help me ??? i will appreciate that.

the method is

public void getDiffDate() {

        String dateStop = "12.6.2014";
        long now = System.currentTimeMillis();

        SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");

        Date d1 = null;
        Date d2 = null;
        try {
            d1 = new Date(now);
            d2 = format.parse(dateStop);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long difference = d2.getTime() - d1.getTime();
        long differenceBack = difference;
        differenceBack = difference / 1000;
        int secs = (int) (differenceBack % 60);
        differenceBack /= 60;
        int mins = (int) (differenceBack % 60);
        differenceBack /= 60;
        int hours = (int) (differenceBack % 24);

        TextView txtDate = (TextView) findViewById(R.id.txtDifDate);
        String resultDate = hours + "H" + " :" + mins + "M" + " :" + secs + "S";

        txtDate.setText(resultDate);
    }

actually i am getting the result but it is not correct so also i need to fix this error.

Try this:

String dateStart = "01/14/2012 09:29:58";
        String dateStop = "01/15/2012 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();
        }

//EDIT

Personally I would recommand you Joda Time. Example of Joda Time:

String dateStart = "01/14/2012 09:29:58";
String dateStop = "01/15/2012 10:31:48";

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

    DateTime dt1 = new DateTime(d1);
    DateTime dt2 = new DateTime(d2);

    System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
    System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
    System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");
    System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");

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

First, remove: long differenceBack = difference;

long differenceBack = difference;
differenceBack = difference / 1000;

Should be:

long differenceBack = difference / 1000;

First you need hours, to do this:

int hours = differenceBack/(60*60);

Than you have the remaining seconds:

int remaining = differenceBack%(60*60)

than again check how many minutes you have in the remaining seconds:

int minutes = remaining/60;

check how many seconds you have left:

int seconds = remaining%60;

You began right, by converting milliseconds to seconds. 1 minute = 60 seconds, 1 hour = 60 minutes, or 60*60 seconds. Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM