2017-08-29 168 views
-11

請提供此問題的代碼,我有代碼來計算日期之間的差異,我可以檢查日期結束或沒有,但如何計算時間(小時),如果日期差異超過1天如何計算當前日期和結束日期之間的時間(安卓)

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
      SimpleDateFormat timesdf = new SimpleDateFormat("HH:mm"); 
      currentitme = timesdf.format(new Date()); 

      currentDateandTime = sdf.format(new Date()); 
      try { 
       date1 = sdf.parse(currentDateandTime); 
       date2 =sdf.parse(auctionBean.getEnddate()); 
       timeend = timesdf.parse(auctionBean.getEndtime()); 
       if (date1.equals(date2)){ 

        compareTimes("11:50:00"); 

       } 
       else if (date1.after(date2)){ 
        Log.e("222","111"); 

       } 
       else if (date1.before(date2)){ 

        Log.e("333","111"); 
       } 

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





private void compareTimes(String endtime) throws ParseException { 

     SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); 
      timeend = dateFormat.parse(endtime); 

     timecurrent = dateFormat.parse(dateFormat.format(new Date())); 

     if (timecurrent.after(timeend)) 
     { 
      Log.e("time is finish",""); 
     } 
    } 
+4

**請爲這個問題提供了代碼.. ?? **這不是教程網站我的朋友 –

+1

類似的問題,12分鐘前?? HTTPS://stackoverflow.com/questions/45932344/diff-bw-current -date-timejavaand-end-datetime – Raghavendra

+1

投票結束,作爲題外話題。 @Raghavendra如果他們是相似的(我無法驗證)在這個問題上使用國旗。 – Zoe

回答

-1

可以實現與JodaTime

日期之間的時間差
DateTime startTime, endTime; 
Period p = new Period(startTime, endTime); 
int hours = p.getHours(); 
int minutes = p.getMinutes(); 
+0

先生我有結束日期31-08-2017和結束時間14:10以及如何計算這個時間從目前 –

+0

嘗試轉換所有時間毫秒,並得到differernce –

+0

Thanxs @Gupta先生您節省我的時間 –

-1

請使用以下提到的代碼

public static boolean isBefore(Calendar cal, String startTime) { 
    if (cal == null || startTime == null) { 
     return true; 
    } 

    if (!startTime.contains(":")) { 
     return false; 
    } 


    int startHour = getHourOfDayOrMinute(startTime, true); 
    int startMin = getHourOfDayOrMinute(startTime, false); 

    Calendar now = DateUtil.getCurrentLocalCalendar(cal.getTimeZone().getID()); 
    now.set(Calendar.HOUR_OF_DAY, startHour); 
    now.set(Calendar.MINUTE, startMin); 

    if (now.after(cal)) { 
     return true; 
    } 

    int calHour = cal.get(Calendar.HOUR_OF_DAY); 
    int calMin = cal.get(Calendar.MINUTE); 


    if (calHour < startHour) { 
     return true; 
    } else if (calHour == startHour) { 
     if (calMin <= startMin) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
    return false; 
} 

public static int getHourOfDayOrMinute(String hhmmAMPM, boolean getHour) { 
    String[] time = hhmmAMPM.split(":"); // 6:00 PM 
    int hour = NumberUtil.toInt(time[0], 0); // 6 
    String s = null; 
    if (!getHour) { 
     if (time[1].contains(" ")) { 
      s = time[1].substring(0, time[1].indexOf(" ")); // 00 PM 
     } else { 
      s = time[1].substring(0, time[1].length() - 2); // 00 PM 
     } 

     return NumberUtil.toInt(s, 0); 
    } 
    if (time[1].trim().endsWith("PM")) { 
     if (hour < 12) { 
      hour += 12; 
     } 
    } 
    return hour; 
} 
0
public static long getValidDateTimeLong(String dateTime) { 
    long time = 0; 
    try { 

     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
     time = simpleDateFormat.parse(dateTime).getTime(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return time; 
} 

添加以下代碼放到要

long endTime = getValidDateTimeLong(productListItemModel.getEndDate()); 
      timeDiff = endTime - System.currentTimeMillis() 
      if (timeDiff > 0) { 
       int day = (int) TimeUnit.SECONDS.toDays(timeDiff/1000); 
       int hours = (int) TimeUnit.SECONDS.toHours(timeDiff/1000) - (day * 24); 
       int minutes = (int) (TimeUnit.SECONDS.toMinutes(timeDiff/1000) - (TimeUnit.SECONDS.toHours(timeDiff/1000) * 60)); 
       int seconds = (int) (TimeUnit.SECONDS.toSeconds(timeDiff/1000) - (TimeUnit.SECONDS.toMinutes(timeDiff/1000) * 60)); 
       days = String.valueOf(day); 
       hrs = String.valueOf(hours); 
       mins = String.valueOf(minutes); 
       sec = String.valueOf(seconds); 
      } 

它將返回天,小時,分鐘,秒

,如果你只想要個小時,只粘貼下面的代碼

if ((86400 * 1000) < timeDiff) { //86400 is seconds per day. then converted it into milli seconds. then checked the condition 
    int hours = (int) TimeUnit.SECONDS.toHours(timeDiff/1000) ; 
    hrs = String.valueOf(hours); 
} 

希望這有助於。

相關問題