Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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 使用startTime和testDuration计算结束时间_Java_Beanshell - Fatal编程技术网

Java 使用startTime和testDuration计算结束时间

Java 使用startTime和testDuration计算结束时间,java,beanshell,Java,Beanshell,我有测试应该运行的测试开始时间(比如9:05:30)和总持续时间(比如5000秒)。如何从开始时间和结束时间计算结束时间?在javascipt中,您可以这样做 console.time('testSomeCall'); //code here console.timeEnd('testSomeCall'); 在其他语言中,通常在测试之前和测试之后获取时间或时间戳,并执行end-init,就像在java中这样 long timeInit = System.currentTimeMillis();

我有测试应该运行的测试开始时间(比如9:05:30)和总持续时间(比如5000秒)。如何从开始时间和结束时间计算结束时间?

在javascipt中,您可以这样做

console.time('testSomeCall');
//code here
console.timeEnd('testSomeCall');
在其他语言中,通常在测试之前和测试之后获取时间或时间戳,并执行end-init,就像在java中这样

long timeInit = System.currentTimeMillis();
//some code here
long timeEnd = System.currentTimeMillis();
System.out.println("Ttotal time: " + (timeEnd - timeInit));

如果您使用的是Java 8时间API中的
Instant
ZoneDateTime
LocalDateTime
,则可以使用
plusSeconds

Instant startTime = ...
Instant endTime = startTime.plusSeconds(5000);
如果您使用的是
Date
(哦,恐怖…),您只需使用
getTime

Date startTime = ...
Date endTime = new Date(startTime.getTime() + TimeUnit.SECONDS.toMillis(5000));
如何将09:05:5020解析为日期?

该值被成功解析为第5020秒,每60秒只需滚动到下一分钟以容纳越界值。如果要防止这种情况,请调用
setLenient(false)DateFormat
对象上执行code>。

使用java.time而不是旧的日期时间类是正确的。这里有更具体的细节

java.time 该框架内置于Java8及更高版本中。这些类取代了旧的麻烦的日期时间类,如
java.util.date
.Calendar
,&
java.text.SimpleDateFormat

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释

大部分java.time功能都在中向后移植到Java6和Java7,并在中进一步适应Android

Instant
如果从现在开始以编程方式获得5000秒,请使用Turner答案中所示的对象。是时间线上分辨率高达纳秒的时刻

Instant later = Instant.now().plusSeconds( 5_000 );
LocalTime
LocalTime
类表示一天中没有日期和时区的时间。因此,这只适用于一般的24小时工作日,而不适用于时区存在异常的实际工作日,如夏令时(DST)

如果达到午夜,结果将滚动到一天的凌晨,从小时开始,然后是小时,依此类推

ZoneDateTime
如果您想要一个符合实际的价值观,尊重等等,请使用


javascript和java完全没有任何关系。至于计算:将时间转换为秒,添加持续时间,将新值转换回timeOops。我错误地标记了它。谢谢!那么,你想计算测试是否持续了5000秒的正确时间吗?事实上,这是测试的一部分。实际上,我正在尝试为给定的线程和测试持续时间自动启动,但我有持续时间和开始时间。需要计算结束时间。查看一下,您需要执行inititalTime+持续时间来获得结束时间
Instant later = Instant.now().plusSeconds( 5_000 );
LocalTime lt = LocalTime.parse( "09:05:30" );
LocalTime later = lt.plusSeconds( 5_000 ); 
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate tomorrow = LocalDate.now( zoneId ).plusDays( 1 );
LocalTime lt = LocalTime.parse( "09:05:30" );
ZonedDateTime tomorrowAround9Am = ZonedDateTime.of( tomorrow , lt , zoneId 0;
ZonedDateTime later = tomorrowAround9Am.plusSeconds( 5_000 );