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.
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
-
Answer # 2
If it is a part of the task to say "Do not stop the time with sleep etc.", if not, obediently
sleep
You 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; } } }
Related articles
- java main method not found using eclipse
- about calculation formula in java
- java: about addition algorithms without carry
- [java] i want to prohibit input of only blanks using regular expressions
- javascript - how to do biased randomness without using loops
- edit user information of each logged-in user without using db in php
- java - cannot compile using package (mac)
- java - updating variables using charat ();
- i want to make a calculation formula in java
- how to crop an image with javascript without using a library or plugin
- java - i want to perform search evaluation of board games without using recursive expressions
- about java calculation formula
- about java calculation formula
- about java calculation formula
- java - file upload using spring security
- css - i want to display the image size from 0% to 100% without using a mask
- java - cannot pass data (screen transition) using servlet
- java - how to increase the number of elements without using arraylist?
- ruby on rails - how to add form to index without using newhhtmlerb
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
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.
The execution result is as follows.
I'll also show you an example that doesn't use sleep, though such code is really bad as it consumes CPU.