从 java 控制台上的 LocalTime.of() 方法更新 Localtime 对象
Update a Localtime object from LocalTime.of() method on the java console
我遇到了一个小问题,如果有人能帮助我,我将不胜感激!
当我尝试更新从 LocalTime.now()
方法创建的 LocalTime
对象以便我可以看到时间流逝时, 它起作用了 下面是代码:
public static void main(String[] args) {
LocalTime d = LocalTime.now();
String h = String.valueOf(d.getHour());
String m = String.valueOf(d.getMinute());
String s = String.valueOf(d.getSecond());
System.out.print("\r"+h + ":" + m + ":" + s);
//Update :
Thread.sleep(1000);
}
输出:
1:53:2 (time is passing)
但是当我 运行 这个 :
public static void main(String[] args) {
LocalTime d = LocalTime.of(12, 15, 33);
String h = String.valueOf(d.getHour());
String m = String.valueOf(d.getMinute());
String s = String.valueOf(d.getSecond());
System.out.print("\r"+h + ":" + m + ":" + s);
//Update :
Thread.sleep(1000);
}
输出:
12:15:33 (time is not passing)
有人能告诉我为什么不更新吗?我如何从用户输入中获取时间 运行 一个 LocalTime
对象?
非常感谢您的宝贵时间!
静态的,不是动态的
Can someone tell me why it's not updating ?
A LocalTime
表示一天中的特定时间。对象是不可变的,不变的,不能更新。
调用 LocalTime.now()
捕获执行那一刻的时间。该值以后 不会 更改。要获取当天的当前时间 later,请再次调用 LocalTime.now()
以获得全新的对象。
要显示 LocalTime
对象的值,请调用 .toString
以标准 ISO 8701 值生成文本。对于其他格式,请使用 DateTimeFormatter
。搜索以了解更多信息,因为这已经处理过很多次了。
经过的时间
And how can I get time running with a LocalTime object from a user input ?
也许您的意思是您想要确定自较早时刻以来经过的时间量,例如用户上次执行特定动作或手势的时间。
so I can see time passing
对于经过的时间,您需要跟踪时刻而不是一天中的时间。 Instant
class 表示 UTC 中的一个时刻。
Instant instant = Instant.now() ; // Capture the current moment in UTC.
使用 Duration
.
以小时-分钟-秒为单位计算经过的时间
Duration d = Duration.between( instant , Instant.now() ) ;
如果您想要一个在特定时间开始更新的时钟,请尝试类似
LocalTime d = LocalTime.of(12, 15, 33);
LocalTime start = LocalTime.now();
for (int x = 0; x < 20; x++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalTime now = LocalTime.now();
Duration dur = Duration.between(start, now);
start = now;
d = d.plusSeconds(dur.getSeconds());
System.out.println(d);
}
我遇到了一个小问题,如果有人能帮助我,我将不胜感激!
当我尝试更新从 LocalTime.now()
方法创建的 LocalTime
对象以便我可以看到时间流逝时, 它起作用了 下面是代码:
public static void main(String[] args) {
LocalTime d = LocalTime.now();
String h = String.valueOf(d.getHour());
String m = String.valueOf(d.getMinute());
String s = String.valueOf(d.getSecond());
System.out.print("\r"+h + ":" + m + ":" + s);
//Update :
Thread.sleep(1000);
}
输出:
1:53:2 (time is passing)
但是当我 运行 这个 :
public static void main(String[] args) {
LocalTime d = LocalTime.of(12, 15, 33);
String h = String.valueOf(d.getHour());
String m = String.valueOf(d.getMinute());
String s = String.valueOf(d.getSecond());
System.out.print("\r"+h + ":" + m + ":" + s);
//Update :
Thread.sleep(1000);
}
输出:
12:15:33 (time is not passing)
有人能告诉我为什么不更新吗?我如何从用户输入中获取时间 运行 一个 LocalTime
对象?
非常感谢您的宝贵时间!
静态的,不是动态的
Can someone tell me why it's not updating ?
A LocalTime
表示一天中的特定时间。对象是不可变的,不变的,不能更新。
调用 LocalTime.now()
捕获执行那一刻的时间。该值以后 不会 更改。要获取当天的当前时间 later,请再次调用 LocalTime.now()
以获得全新的对象。
要显示 LocalTime
对象的值,请调用 .toString
以标准 ISO 8701 值生成文本。对于其他格式,请使用 DateTimeFormatter
。搜索以了解更多信息,因为这已经处理过很多次了。
经过的时间
And how can I get time running with a LocalTime object from a user input ?
也许您的意思是您想要确定自较早时刻以来经过的时间量,例如用户上次执行特定动作或手势的时间。
so I can see time passing
对于经过的时间,您需要跟踪时刻而不是一天中的时间。 Instant
class 表示 UTC 中的一个时刻。
Instant instant = Instant.now() ; // Capture the current moment in UTC.
使用 Duration
.
Duration d = Duration.between( instant , Instant.now() ) ;
如果您想要一个在特定时间开始更新的时钟,请尝试类似
LocalTime d = LocalTime.of(12, 15, 33);
LocalTime start = LocalTime.now();
for (int x = 0; x < 20; x++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalTime now = LocalTime.now();
Duration dur = Duration.between(start, now);
start = now;
d = d.plusSeconds(dur.getSeconds());
System.out.println(d);
}