使用 java.time.Clock.offset 以任意开始时间增加时间
Using java.time.Clock.offset to increment in time with an arbitrary starting time-of-day
我想要一个设置为一天中任意时间的时钟,然后以一秒为增量滴答作响。
例如,以“14:55:20”开头。然后我的应用程序应该在一秒内输出“14:55:20”、“14:55:21”、“14:55:22”等等。
看来 Clock.offset
就是为了做到这一点。引用文档:
Obtains a clock that returns instants from the specified clock with the specified duration added
所以我尝试了以下方法:
LocalTime localTime = LocalTime.parse( "14:55:20" );
OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ).with( localTime );
Duration duration = Duration.between( odt.toInstant() , Instant.now() );
Clock clock = Clock.offset( Clock.systemUTC() , duration );
System.out.println( "odt = " + odt );
System.out.println( "duration = " + duration );
for ( int i = 1 ; i < 10 ; i++ )
{
LocalTime lt = OffsetDateTime.now( clock ).truncatedTo( ChronoUnit.SECONDS ).toLocalTime();
System.out.println( "localTime = " + lt );
try
{
Thread.sleep( TimeUnit.SECONDS.toMillis( 1 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
}
我最初 odt
的出发点似乎是正确的。但是后来我对 OffsetDateTime.now( clock )
的调用没有产生预期的结果。
odt = 2019-10-31T14:55:20Z
duration = PT-10H-29M-4.832902S
localTime = 17:57:10
localTime = 17:57:11
localTime = 17:57:12
➥ 我是不是没有正确使用 Clock.offset
?还是我误解了一些概念?
我想你可以简单地检查秒数是否发生了变化,例如你可以这样做:
public static void main(String[] args) {
// hour of launch
String startTime = getTime();
// actual time;
String time = getTime();
// elapsed time in seconds.
int elapsedTime = 0;
while (true) {
if(!time.equals(getTime())) {
time = getTime();
// Output actual time.
System.out.println(time);
elapsedTime += 1;
}
}
}
public static String getTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
时长写错了:
Duration duration = Duration.between( Instant.now(), odt.toInstant() ); // Flipped arguments
输出
odt = 2019-10-31T14:55:20Z
duration = PT8H1M41.0216146S
localTime = 14:55:20
localTime = 14:55:21
localTime = 14:55:22
localTime = 14:55:23
localTime = 14:55:24
localTime = 14:55:25
localTime = 14:55:26
localTime = 14:55:27
localTime = 14:55:28
我想要一个设置为一天中任意时间的时钟,然后以一秒为增量滴答作响。
例如,以“14:55:20”开头。然后我的应用程序应该在一秒内输出“14:55:20”、“14:55:21”、“14:55:22”等等。
看来 Clock.offset
就是为了做到这一点。引用文档:
Obtains a clock that returns instants from the specified clock with the specified duration added
所以我尝试了以下方法:
LocalTime localTime = LocalTime.parse( "14:55:20" );
OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ).with( localTime );
Duration duration = Duration.between( odt.toInstant() , Instant.now() );
Clock clock = Clock.offset( Clock.systemUTC() , duration );
System.out.println( "odt = " + odt );
System.out.println( "duration = " + duration );
for ( int i = 1 ; i < 10 ; i++ )
{
LocalTime lt = OffsetDateTime.now( clock ).truncatedTo( ChronoUnit.SECONDS ).toLocalTime();
System.out.println( "localTime = " + lt );
try
{
Thread.sleep( TimeUnit.SECONDS.toMillis( 1 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
}
我最初 odt
的出发点似乎是正确的。但是后来我对 OffsetDateTime.now( clock )
的调用没有产生预期的结果。
odt = 2019-10-31T14:55:20Z
duration = PT-10H-29M-4.832902S
localTime = 17:57:10
localTime = 17:57:11
localTime = 17:57:12
➥ 我是不是没有正确使用 Clock.offset
?还是我误解了一些概念?
我想你可以简单地检查秒数是否发生了变化,例如你可以这样做:
public static void main(String[] args) {
// hour of launch
String startTime = getTime();
// actual time;
String time = getTime();
// elapsed time in seconds.
int elapsedTime = 0;
while (true) {
if(!time.equals(getTime())) {
time = getTime();
// Output actual time.
System.out.println(time);
elapsedTime += 1;
}
}
}
public static String getTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
时长写错了:
Duration duration = Duration.between( Instant.now(), odt.toInstant() ); // Flipped arguments
输出
odt = 2019-10-31T14:55:20Z
duration = PT8H1M41.0216146S
localTime = 14:55:20
localTime = 14:55:21
localTime = 14:55:22
localTime = 14:55:23
localTime = 14:55:24
localTime = 14:55:25
localTime = 14:55:26
localTime = 14:55:27
localTime = 14:55:28