为什么尽管持续时间增加了 javax.xml.datatype.Duration.getMinutes() 总是返回 0?如果您运行下面的代码超过一分钟,您将看到如下输出:
minutes: 0 , seconds: 61
我以前从未使用过这个类。我错过了一些关于它的基本知识吗?这是代码:
import java.util.Timer;
import java.util.TimerTask;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
public class TestDuration {
class MyTimerTask extends TimerTask {
DatatypeFactory df;
long delay;
Duration duration;
public MyTimerTask(DatatypeFactory df, long delay) {
this.df = df;
this.delay = delay;
duration = df.newDuration(0);
}
@Override
public void run() {
duration = duration.add(df.newDuration(delay));
System.out.println("minutes: " + duration.getMinutes() + " , seconds: " + duration.getSeconds());
}
}
public static void main(String[] args) {
Timer timer = new Timer();
final long delay = 1 * 1000;
DatatypeFactory df = null;
try {
df = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
MyTimerTask task = new TestDuration().new MyTimerTask(df, delay);
timer.schedule(task, 0, delay);
}
}