在android中计算时间差得到不同的结果



我正在从两个edittext获取时间值,以及花费的总时间,但这并不像预期的那样工作。有谁能帮我一下吗?

      SimpleDateFormat format = new SimpleDateFormat("HH:mm");
    try {
        dateOne = format.parse(strFromVessel);
        dateTwo = format.parse(strToVessel);
        System.out.println("date1" + dateOne + "date2" + dateTwo);

        long diff = Math.abs(dateTwo.getTime() - dateOne.getTime());
        System.out.println("diff"+diff);
        System.out.println("dateTwo.getTime()"+dateTwo.getTime());
        System.out.println("dateo.getTime()"+dateOne.getTime());
        long Hours = diff/(1000 * 60 * 60);
        long Mins = diff % (1000*60*60);
        String difference = Hours + ":" + Mins; 
        System.out.println("long"+difference);
    } catch (Exception e) {
        e.printStackTrace();
    }

我给你们举个例子,在我的代码中,我取当前的时间差值从1970年1月1日开始,我这样计算时间差值,这可能会对你们有所帮助,只要把你们的时间而不是我的时间放在所有代码中,就像使用

 Calendar cal = Calendar.getInstance();
    Date currentLocalTime = cal.getTime();
    DateFormat date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");  
    date.setTimeZone(TimeZone.getTimeZone("GMT")); 
    String localTime = date.format(currentLocalTime); 
    long currenttime = Constant.retunlongdate(localTime);
    long fixtimejan = Constant.retunlongdate("01/01/1970 00:00:00 AM");
    long nTimeStamp = (currenttime - fixtimejan)/1000;
    System.out.println("and result is == " + nTimeStamp);
 public static long retunlongdate(String givenDateString)
        {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"); 
        long timeInMilliseconds=0;
        try {
            Date mDate =sdf.parse(givenDateString);
              timeInMilliseconds = mDate.getTime();
            System.out.println("Date in milli :: " + timeInMilliseconds);
            return timeInMilliseconds;
        } catch (ParseException e) {
                    e.printStackTrace();
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
        return timeInMilliseconds;
    }

最新更新