Home>

As a school problem, I have a program that displays the date and time every second and displays it for 30 seconds and ends.
It is stuck because the time cannot be displayed every second.
As a precaution, I want to display the date and time in the calculation without stopping the time with sleep etc.

Applicable source code

Below sample

import java.util.Calendar;
import java.text.SimpleDateFormat;
public class Sample{
    public static void main(String[]args){
        SimpleDateFormat d = new SimpleDateFormat
        ("yyyy/MM/dd HH:mm:ss");
        Calendar c1 = Calendar.getInstance();
        int s1 = c1.get(Calendar.SECOND);
        Calendar c2 = Calendar.getInstance();
        int s2 = c2.get(Calendar.SECOND);
        int s3 = c2.compareTo(c1);
        while(s2-s1<30){
            s2++;
            if(s3 >= 0){
                System.out.println(d.format(s2.getTime()));
                c2.add(Calendar.SECOND,+1);
            }else if(s3 >= 30){
                break;
            }
        }
    }
}
what I want to know

If i write this way all of it will appear all at once in the processing of the while and below parts, so I would like to know how you can display it every second.

Supplemental information (FW/tool ​​version, etc.)

The tool uses java.

  • Answer # 1

    I'm sorry if I misunderstood, but I wonder if I want to perform other processing at the same time while displaying the date and time every second. Then you can do it by using the java.lang.Thread class. An example is shown below.

    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class Main {
        public static void main(String[] args) {
            System.out.println("Hello");
            new Thread(() ->{
                try {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                    for (int i = 0;i<30;i++) {
                        System.out.println(formatter.format(new Date()));
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
            System.out.println("World!");
        }
    }

    The execution result is as follows.

    Hello
    World!
    2020/08/19 22:36:33
    2020/08/19 22:36:34
    2020/08/19 22:36:35
    2020/08/19 22:36:36
    2020/08/19 22:36:37
    2020/08/19 22:36:38
    2020/08/19 22:36:39
    2020/08/19 22:36:40
    2020/08/19 22:36:41
    2020/08/19 22:36:42
    2020/08/19 22:36:43
    2020/08/19 22:36:442020/08/19 22:36:45
    2020/08/19 22:36:46
    2020/08/19 22:36:47
    2020/08/19 22:36:48
    2020/08/19 22:36:49
    2020/08/19 22:36:50
    2020/08/19 22:36:51
    2020/08/19 22:36:52
    2020/08/19 22:36:53
    2020/08/19 22:36:54
    2020/08/19 22:36:55
    2020/08/19 22:36:56
    2020/08/19 22:36:57
    2020/08/19 22:36:58
    2020/08/19 22:36:59
    2020/08/19 22:37:00
    2020/08/19 22:37:01
    2020/08/19 22:37:02


    I'll also show you an example that doesn't use sleep, though such code is really bad as it consumes CPU.

    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class Main {
        public static void main(String[] args) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            long endTime = System.currentTimeMillis() + 30000;
            long shownTime = 0;
            while (endTime>shownTime) {
                if (System.currentTimeMillis()<shownTime + 1000) continue;
                shownTime = System.currentTimeMillis();
                System.out.println(formatter.format(new Date(shownTime)));
            }
        }
    }

  • Answer # 2

    If it is a part of the task to say "Do not stop the time with sleep etc.", if not, obedientlysleepYou should let me.

    Even if you keep waiting in an endless loop etc., time will pass, but such a way isBusy loopIt is not desirable in terms of efficiency, such as "CPU usage jumps up while waiting".

  • Answer # 3

    thank you for your answer.
    Well, that's part of the challenge.

  • Answer # 4

    For the time being, I think this is the desired behavior.
    The end time is obtained by adding 30 seconds to the current millisecond currently acquired in the Date class.
    And now I am dividing the millisecond by 1000 and outputting to the console when there is no remainder,
    If it is left as it is, the output will run multiple times within 1 millisecond, so
    Hold the current second in holdTime and stop processing with continue until a difference appears after 1 millisecond.
    Well, it's not very beautiful.
    If there is someone who can write more neatly than this, please write.

    SimpleDateFormat d = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    long nowTime = (new Date()).getTime();
    long endTime = nowTime + (30 * 1000);
    long holdTime = -1;
    while (nowTime<endTime) {
        if (nowTime == holdTime) {
            nowTime = (new Date()).getTime();
            continue;
        }
        if (nowTime %1000 == 0) {
            holdTime = nowTime;
            System.out.println(d.format(new Date(nowTime)));
        }
        nowTime = (new Date()).getTime();
    }

  • Answer # 5

    If it's a school task, I wonder if there are so many things to use.

    public static void main(String[] args) {
        SimpleDateFormat d = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String prev = "";
        // while(System.currentTimeMillis() %1000 != 0);// wait for the second display 1 second later
        for(int i = 0;i<30;) {/ 30 times = 30 seconds display (30 times display is 29 seconds...)
            String now = d.format(new Date());
            if(!prev.equals(now)) {/
                i++;// increment i here instead of for
                System.out.println(now);
                prev = now;
            }
        }
    }