"Go" method of obtaining local time in Java--good

Source: Internet
Author: User

Original URL: http://highforest.blog.51cto.com/125539/842496/

People familiar with Oracle database should know: Select To_char (sysdate, ' Yyyy-mm-dd ' Hh24:mi:ss) from dual will find out what kind of results, good, is similar to 2012-04-23 20:43:57 such a 19-bit string. So how do you get such a string in Java? A number of similar examples have been given online, but have been tried. Had to carefully study the Java API documentation, finally found a solution:

  1. Import java.util.*;
  2. Import java.text.*;
  3. public class Testcalendar {
  4. /**
  5. * @param args
  6. */
  7. public static void Main (string[] args) {
  8. Calendar cal1 = calendar.getinstance ();
  9. Timezone.setdefault (Timezone.gettimezone ("gmt+8:00")); Very critical!!!
  10. Java.text.SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd kk:mm:ss");
  11. System.out.println (Sdf.format (Cal1.gettime ()));
  12. }
  13. }

What needs to be explained is this one timezone.setdefault (Timezone.gettimezone ("gmt+8:00")); Very critical, without this sentence, the result must be GMT standard time, not Beijing times!

It is also necessary to note that each time the current moment is read by calling Calendar.getinstance () and not deceived by the Calendar.gettime () method:

  1. public class Testcalendar {
  2. /**
  3. * @param args
  4. */
  5. public static void Main (string[] args) {
  6. Calendar cal1 = calendar.getinstance ();
  7. Timezone.setdefault (Timezone.gettimezone ("gmt+8:00"));
  8. Java.text.SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd kk:mm:ss SSS");
  9. System.out.println (Sdf.format (Cal1.gettime ()));
  10. for (int i = 0; I<; i++) {
  11. float f = 999999.9f;
  12. do {
  13. ff = f-1.0000001f;
  14. } while (f>0.0f);
  15. }
  16. Cal1 = calendar.getinstance ();
  17. System.out.println (Sdf.format (Cal1.gettime ()));
  18. }
  19. }

Will output two identical time strings:

2012-04-23 21:11:56 699
2012-04-23 21:11:56 699

After removing the comment from line 17th, the output is:

2012-04-23 21:13:28 736
2012-04-23 21:13:29 235

"Go" method of obtaining local time in Java--good

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.