While reinstalling myeclipse, I will share with you a small method. Hope can help you guys
1. Return the current time string. The classes used include calendar, date, and simpledateformat.
1. Use calendar = calendar. getinstance (); To obtain an instance of the current system calendar.
2. Use date = (date) calendar. gettime (); to get the current time.
3. use simpledateformat format = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); to set your own time format. By the way, the HH here is in the 24-hour format, if you want to change to the 12-hour system, you can change it to HH,
For more details, refer to the API documentation, or Google.
4. format. Format (date) can be used to format the date in the specified format and return a stringbuffer.
2. If you need to refresh every second, you also need to use the timer and timertask classes.
1. Timer timer = new timer (); get a timer.
2. Timer. Schedule (New remindtask (), 0, 1000); Use the schedule method of timer to specify a task and execute it once every second. You can set the time to refresh when executing a task so that it can be refreshed once per second. This needs to be set in remindtask.
3. Set the Update Time in the remintask class. (For details, refer to the example ).
3. The following is a specific instance.
Public class time extends jframe {
Jlabel label;
Jtextfield text;
String time = NULL;
Public time (){
Label = new jlabel ("current time :");
TEXT = new jtextfield ();
Setbounds (300,300,300, 70 );
Setlayout (null );
Label. setbounds (10, 10,100, 20 );
Text. setbounds (110, 10,150, 20 );
Add (Label );
Add (text );
Text. settext (gettime ());
Setvisible (true );
Timer timer = new timer ();
Timer. Schedule (New remindtask (), 0, 1000 );
}
Public String gettime (){
Calendar calendar = calendar. getinstance ();
Date = (date) calendar. gettime ();
Simpledateformat format = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
Time = format. Format (date );
Return time;
}
Public static void main (string [] ARGs ){
New Time ();
}
Private class remindtask extends timertask {
Public void run (){
Text. settext (gettime ());
}
}
}
Effect
=============="
Of course, this is only my own method. There must be other good methods. Anyone can share them ~~