2013-10-24 62 views
1

我有值爲「3D7H40M20S」 哪些應該被轉換爲3天7小時40分20秒」的字符串 這裏是我試過至今:如何格式化持續時間字符串?

 BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry(); 
    AttributeBinding attr = (AttributeBinding)bindings.getControlBinding("ScreeningSLAWaitTimeDuration"); 
    scrSlaWaitDur = (String)attr.getInputValue(); 
    System.out.println("-------------------------------------------SCREENING SLA WAIT DURATION---------------"+scrSlaWaitDur); 
    scrSlaWaitDur = scrSlaWaitDur.substring(2, scrSlaWaitDur.length()); 
    System.out.println("--------------------------------------------SUBSTRING--------------------"+scrSlaWaitDur); 
    int dIndex = scrSlaWaitDur.indexOf("D"); 
    System.out.println("*******************************************INDEX OF D**********************************"+dIndex); 
    if(dIndex != -1){ 
     String newDur = scrSlaWaitDur.substring(0, dIndex)+" days "+scrSlaWaitDur.substring(dIndex+1); 
     int mIndex = scrSlaWaitDur.lastIndexOf("M"); 
     String newDur2 = scrSlaWaitDur.substring(0, mIndex)+" minute "+scrSlaWaitDur.substring(mIndex+1); 
     int sIndex = newDur2.lastIndexOf("S"); 
     String newDur3 = newDur2.substring(0,sIndex)+" second"; 
     scrSlaWaitDur = newDur3; 
     return scrSlaWaitDur; 
    } 

我知道這是相當?煩人和相當長 我能做到的要求以更簡單的方式

回答

2

一個務實的方式進行更換單元令牌:

return scrSlaWaitDur.replace("D", " Days ").replace("H", " Hours ").replace("M", " Minutes ").replace("S", " Seconds"); 

輸出:

3 Days 7 Hours 40 Minutes 20 Seconds 
1

您可以使用子(),而不指數(),即:

String result = 
scrSlaWaitDur.replace("D"," Days ").replace("H"," Hours ").replace("M"," Minutes ").replace("S"," Seconds"); 

但我敢肯定有人會拿出一個fency正則表達式的解決方案。 ;-)

相關問題