Java obtains the current time in the specified date and time format and refreshes every second.

Source: Internet
Author: User

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 ~~







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.