Update a Localtime object from LocalTime.of() method on the java console [closed]

醉酒当歌 提交于 2019-12-13 09:45:23

问题


I'm having a little issue here if anyone could help me with it I would really appreciate it !

When I try to update a LocalTime object created from the LocalTime.now() method so I can see time passing, it works here is the code :

     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);


     }

Output :

1:53:2 (time is passing)

But when I run this :

     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);


     }

Output :

12:15:33 (time is not passing)

Can someone tell me why it's not updating ? And how can I get time running with a LocalTime object from a user input ?

Thanks a lot for your time !


回答1:


Static, not dynamic

Can someone tell me why it's not updating ?

A LocalTime represents a particular time of day. The object is immutable, unchanging, and cannot be updated.

Calling LocalTime.now() captures the time of day at that moment of execution. The value will not change later. To get the current time of day later, call LocalTime.now() again for a fresh new object.

To display the value of a LocalTime object, call .toString to generate text in standard ISO 8701 value. For other formats, use DateTimeFormatter. Search to learn more as this has been handled many times already.

Elapsed time

And how can I get time running with a LocalTime object from a user input ?

Perhaps you mean you want to determine the amount of time that has elapsed since an earlier moment, such as when the user last performed a particular act or gesture.

so I can see time passing

For elapsed time, you need to track a moment rather than a time-of-day. The Instant class represents a moment in UTC.

Instant instant = Instant.now() ;  // Capture the current moment in UTC. 

Calculate elapsed time on a scale of hours-minutes-seconds with a Duration.

Duration d = Duration.between( instant , Instant.now() ) ;



回答2:


If you want a clock that updates starting at a certain time try something like

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);
}


来源:https://stackoverflow.com/questions/58635651/update-a-localtime-object-from-localtime-of-method-on-the-java-console

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!