将 xml 持续时间 ex P0DT1H0M0S 转换为 java

发布于 2024-12-23 01:39:58 字数 217 浏览 1 评论 0原文

嗨,我正在尝试将 xml 持续时间读取为新的持续时间,但我没有说 “无法实例化类型 Duration”

我对 java 和 android 开发很陌生。所以请保留任何非建设性的评论。

代码

import javax.xml.datatype.Duration;

Duration duration = new Duration();

Hi i'm trying to read xml duration to a new duration but i't says
"Cannot instantiate the type Duration"

I'm quite new to java and android dev. so please keep any nonconstructive comments to yourself..

the code

import javax.xml.datatype.Duration;

Duration duration = new Duration();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2024-12-30 01:39:58

我自己找到的。

        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;
    }

}

Found it myself.

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

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

Edit..

The above is not possible due to a DatatypeFactory bug sooo i built this class to fix it myself..

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;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文