2012-01-18 30 views
2

JTable包括時間字段,例如, 「01:50」。我需要將這個值讀入整數變量。爲此,我想將時間轉換爲分鐘。例如,「01:50」應該轉換爲110.在JAVA中將時間字段H:M轉換爲整數字段(分鐘)

要解決此任務,首先將時間值保存爲字符串。

String minutS = tableModel.getValueAt(1,1).toString(); 

其次,I'm去處理這個字符串和前後符號:後提取整數。最後我會計算總分鐘數。 這個解決方案是否高效?也許,可以用Calendar來代替它,或者像這樣?

+2

有什麼實際類型的表模型中的數據的? – 2012-01-18 11:26:56

回答

15

我不使用日曆/日期會比這種情況下,簡單的解析更好地認爲看樣品。如果你的時間格式的確H:M,那麼我不認爲你需要什麼比這更復雜:

/** 
* @param s H:m timestamp, i.e. [Hour in day (0-23)]:[Minute in hour (0-59)] 
* @return total minutes after 00:00 
*/ 
private static int toMins(String s) { 
    String[] hourMin = s.split(":"); 
    int hour = Integer.parseInt(hourMin[0]); 
    int mins = Integer.parseInt(hourMin[1]); 
    int hoursInMins = hour * 60; 
    return hoursInMins + mins; 
} 
+0

@ sudocode如果給定的字符串是12小時格式,比如3:30 pm,那麼在上面的代碼中存在問題,那麼上面的代碼將在行上引發numberFormat異常:int mins = Integer.parseInt(hourMin [1]); 那麼如何用上面的代碼來避免這個問題呢?所以基本上可以在12小時格式或24小時格式字符串應該工作!!!!!! – Swift 2017-01-10 10:20:10

1

低於我們使用我們的應用程序

public static int toMinutes(String sDur, boolean bAssumeMinutes) throws NumberFormatException { 
     /* Use of regular expressions might be better */ 
     int iMin = 0; 
     int iHr = 0; 
     sDur = sDur.trim(); 
     //find punctuation 
     int i = sDur.indexOf(":");//HH:MM 
     if (i < 0) { 
      //no punctuation, so assume whole thing is an number 
      //double dVal = Double.parseDouble(sDur); 
      double dVal = ParseAndBuild.parseDouble(sDur, Double.NaN); 
      if (Double.isNaN(dVal)) throw new NumberFormatException(sDur); 
      if (!bAssumeMinutes) { 
       //decimal hours so add half a minute then truncate to minutes 
       iMin = (int)((dVal * 60.0) + (dVal < 0 ? -0.5 : 0.5)); 
      } else { 
       iMin = (int)dVal; 
      } 
     } 
     else { 
      StringBuffer sBuf = new StringBuffer(sDur); 
      int j = sBuf.indexOf(MINUS_SIGN); 
      //check for negative sign 
      if (j >= 0) { 
       //sign must be leading/trailing but either allowed regardless of MINUS_SIGN_TRAILS 
       if (j > 0 && j < sBuf.length() -1) 
        throw new NumberFormatException(sDur);     
       sBuf.deleteCharAt(j); 
       i = sBuf.indexOf(String.valueOf(":")); 
      } 
      if (i > 0) 
       iHr = Integer.parseInt(sBuf.substring(0, i)); //no thousands separators allowed 
      if (i < sBuf.length() - 1) 
       iMin = Integer.parseInt(sBuf.substring(i+1)); 
      if (iMin < 0 || (iHr != 0 && iMin >= 60)) 
       throw new NumberFormatException(sDur); 
      iMin += iHr * 60; 
      if (j >= 0) iMin = -iMin; 
     } 
     return iMin; 
    } 
2

如何:

String time = "1:50"; 
String[] split = time.split(":"); 
if(split.length == 2) { 
     long minutes = TimeUnit.HOURS.toMinutes(Integer.parseInt(split[0])) + 
         Integer.parseInt(split[1]); 
     System.out.println(minutes); 
    } 

/* Output: 110 */ 
0

處理之前剝去你的輸入。 (commons-lang utils

空格可以使NumberFormatExceptions發生。

0

由於Java 1.8中最優雅的解決方案可能是:

long minutes = ChronoUnit.MINUTES.between(LocalTime.MIDNIGHT, LocalTime.parse("01:50"));

相關問題