Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何格式化持续时间字符串?_Java - Fatal编程技术网

Java 如何格式化持续时间字符串?

Java 如何格式化持续时间字符串?,java,Java,我有一个值为“3D7H40M20S”的字符串 应转换为3天7小时40分20秒”。 以下是我迄今为止所做的尝试: BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry(); AttributeBinding attr = (AttributeBinding)bindings.getControlBinding("ScreeningSLAWaitTimeDuration"

我有一个值为“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;
    }
我知道这很烦人,也很冗长。
我可以用一种更简单的方法来实现需求吗?

一种实用的方法是替换单元令牌:

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

3 Days 7 Hours 40 Minutes 20 Seconds
可以使用不带索引()的子字符串(),即:

但我相信有人会想出一个芬西正则表达式的解决方案

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