如何计算android中2日期之间的差异?

时间:2014-04-01 12:17:24

标签: android time error-handling

我正在创建一个计算2日期(END DATE _ CURRENT DATE)之间差异的方法。 我想以月,日,小时,分钟,秒显示日期格式

喜欢这种格式:2个月3天。 8小时。 12分钟32秒

谁能帮助我? 我会很感激的。

方法是

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

实际上我得到了结果但是它不正确所以我也需要修复这个错误。

2 个答案:

答案 0 :(得分:1)

试试这个:

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

<强> //修改

我个人会推荐你Joda Time。 Joda时间示例:

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

答案 1 :(得分:0)

首先,删除:long differenceBack = difference;

long differenceBack = difference;
differenceBack = difference / 1000;

应该是:

long differenceBack = difference / 1000;

首先你需要几个小时才能做到这一点:

int hours = differenceBack/(60*60);

比你有剩余的秒数:

int remaining = differenceBack%(60*60)

再次检查剩余秒数内你有多少分钟:

int minutes = remaining/60;

检查你还剩多少秒:

int seconds = remaining%60;

你开始正确,将毫秒转换为秒。 1分钟= 60秒,1小时= 60分钟,或60 * 60秒。祝你好运。