Use timers
Use a timer, the current time in 10 call the method, the output statement after seconds
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.TimerTask; public class Student extends TimerTask { String name; // constructor public Student(String name) { this.name = name; } // override the abstract method of TimerTask @Override public void run() { Calendar c=Calendar.getInstance(); SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(simpleDateFormat.format(c.getTime())); System.out.println (name + "learning" ); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Timer; // Timer public class StudentTest { public static void main(String[] args) { // create a Timer object Timer t = new new Timer (); // Create a Student object Student STU = new new Student ( "lili" ); // get a Calendar object Calendar C = Calendar.getInstance (); // formatting time the SimpleDateFormat SimpleDateFormat = new new the SimpleDateFormat ( "the MM-dd-YYYY HH: mm: SS" ); System.out.println(simpleDateFormat.format(c.getTime())); // Current delayed 10 seconds c.add (Calendar.SECOND, 10 ); // call the method Timer t.schedule(stu,c.getTime()); } }
operation result:
2019-07-18 16:23:43
2019-07-18 16:23:53
lili learning
The computer first output 2019-07-1816: 23: 43,10 the second output 2019-07-18 16: 23: 53, lili learn