-->

Update a Localtime object from LocalTime.of() meth

2020-01-20 13:41发布

问题:


Want to improve this question? Add details and clarify the problem by editing this post.

Closed 3 months ago.

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