Joda Time按时复杂运营

时间:2013-06-22 21:00:30

标签: parsing jodatime

我想在我的项目中使用Joda Time。我很想知道我是否知道如何使用它。 首先 - 我想创建一个我初始化的进度条,然后每秒计算它的值 - 它显示了完成过程所需的时间。

在初始化期间,我只有格式为“HH:mm:ss”的字符串(偶尔表示“D days HH:mm:ss”)表示剩余时间和百分比 - 进度条的初始状态。这就是我的全部。

现在我想创建一个表示任务完成时刻的DateTime对象。

PeriodFormatter timeFormatter = new PeriodFormatterBuilder()
        .appendHours().appendSeparator(":").appendMinutes()
        .appendSeparator(":").appendSeconds().toFormatter();
DateTime endDate_ = new Date();
Period periodLeft = null;
String[] parsedInput = timeLeft.split(" ");
if (parsedInput != null) {
    switch (parsedInput.length) {
    case 1: {
        periodLeft = timeFormatter.parsePeriod(parsedInput[0]);
        endDate_.plus(periodLeft);
        break;
    }
    case 3: {
        periodLeft = timeFormatter.parsePeriod(parsedInput[2]);
        periodLeft.plusDays(Integer.parseInt(parsedInput[0]));
        endDate_.plus(periodLeft);
        break;
    }
    default:
        break;
    }
}

据我所知,现在我有我想要的东西,对吧?现在我想计算整个过程的持续时间。这就是为什么我将这段时间转换为毫秒并根据进度计算总持续时间:

long duration_ = (periodLeft.toStandardDuration().getMillis() * 100) / (progress == 0 ? 1 : progress);

现在我必须实现一个基于当前时间返回实际进程状态的方法。我怎样才能做到这一点?我知道持续时间,所以我可以得到DateTime的开始。然后我可以简单地将当前日期与开始日期和计数百分比进行比较:(现在 - 开始)/ duration_ * 100.但是如何才能获得开始日期?

2 个答案:

答案 0 :(得分:0)

Jodatime非常易于使用,您无需使用格式化程序填充代码即可获得所需内容。您测量的过程必须始于代码中的某个点,并且您应该注意开始时间。尝试这样的事情。

 DateTime startTime = DateTime.now();
 DateTime endTime = DateTime.now();
 endTime.plusSeconds(50);


 //Write code for your application.
 //....
 Thread.sleep(3000);

 //Calculate your percentage.
 double remainingPercentage =  (  DateTime.now().getMillis() - startTime.getMillis()) / ( endTime.getMillis() - DateTime.now().getMillis() )*100 ;

//Then output the date using the string method.
 endTime.toString("MMM dd yy ");

答案 1 :(得分:0)

确定。我设法解决了!计算进度的函数现在稍微修改为:

if (progress == 0)
    duration_ = periodLeft.toStandardDuration().getMillis();
else
    duration_ = (long) ((periodLeft.toStandardDuration().getMillis() * 100) / (double) (100 - progress));

我接下来要做的是将我的progressBar最大值设置为duration_,并将进度的每秒设置值显示为:

new Period(startDate_, DateTime.now())
相关问题