2011-12-28 9 views
1

こんにちは私は新しい持続するXML期間を読みしようとしているにP0DT1H0M0Sしかしi't は、私は、JavaとAndroidのdevのに非常に新しいです変換のxml期間EXは、java

「タイプの持続時間をインスタンス化できません」と言います。それを発見

...自分自身に自分自身をコード

import javax.xml.datatype.Duration; 

Duration duration = new Duration(); 

答えて

3

を任意のnonconstructiveコメントを保管してください。

 try { 
      DatatypeFactory dtFactory = DatatypeFactory.newInstance(); 
     } catch (DatatypeConfigurationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

Duration duration = dtFactory.newDuration("p0dt1h0m0s"); 

編集..

上記すっごく私はそれを自分自身を修正するために、このクラスを建てによるDatatypeFactoryバグのために可能ではない..です

public class XmlDuration { 
    private String _xmlDuration; 
    private int _years; 
    private int _months; 
    private int _days; 
    private int _hours; 
    private int _minutes; 
    private int _seconds; 

    private boolean _isNegative; 

    public XmlDuration(String xmlDuration) { 
     try { 

      _xmlDuration = xmlDuration; 

      _isNegative = ((String)_xmlDuration.subSequence(0,1)).matches("[-]"); 

      String period; 
      String time; 

      int tIndex =_xmlDuration.indexOf("T"); 

      period = xmlDuration.substring(0, tIndex); 
      time = _xmlDuration.substring(tIndex); 

      String numericSection = ""; 

      for (int i = 0; i < period.length(); i++) { 
       char[] c = new char[] {period.charAt(i)}; 
       String s = new String(c);     

       if(s.matches("\\d")) 
       { 
        numericSection += s; 
       } 
       else if (s.matches("[Yy]")) 
       { 
        _years = Integer.parseInt(numericSection); 
        numericSection = ""; 
       } 
       else if (s.matches("[Mm]")) 
       { 
        _months = Integer.parseInt(numericSection); 
        numericSection = ""; 
       } 
       else if (s.matches("[Dd]")) 
       { 
        _days = Integer.parseInt(numericSection); 
        numericSection = ""; 
       } 

      } 

      for (int i = 0; i < time.length(); i++) { 
       char[] c = new char[] {time.charAt(i)}; 
       String s = new String(c); 

       if(s.matches("\\d")) 
       { 
        numericSection += s; 
       } 
       else if (s.matches("[Hh]")) 
       { 
        _hours = Integer.parseInt(numericSection); 
        numericSection = ""; 
       } 
       else if (s.matches("[Mm]")) 
       { 
        _minutes = Integer.parseInt(numericSection); 
        numericSection = ""; 
       } 
       else if (s.matches("[Ss]")) 
       { 
        _seconds = Integer.parseInt(numericSection); 
        numericSection = ""; 
       }  

      } 

     } catch (Exception e) { 
      // TODO: handle exception 
     } 



    } 

    public String getXmlString() 
    { 
     return _xmlDuration; 
    } 

    public int getYears() 
    { 
     return _years; 
    } 

    public int getMonth() 
    { 
     return _months; 
    } 

    public int getDays() 
    { 
     return _days; 
    } 

    public int getHours() 
    { 
     return _hours; 
    } 

    public int getMinutes() 
    { 
     return _minutes; 
    } 

    public int getSeconds() 
    { 
     return _seconds; 
    } 

    public boolean getIsNegative() 
    { 
     return _isNegative; 
    } 

} 
+0

私は、これは古いです知っている...しかし、これは素晴らしい。しかし、多くの場合、比較を行う必要があり、少なくとも一貫したユニットが必要です。だから私は、単純な強化は、合計秒数に結果を合計するメソッドの作成だろうと思った。しかし、それをするためには、単位月が表すものを知る必要があります。それは30日ですか?私は年が365日と仮定します。 –

+0

少し遅れました。ありがとうございますが、これは標準的なXML期間の単なる変換です。 TimeSpanを使う方が良いでしょう。 – jrb

関連する問題