Time Difference in retrieving system time in java

Source: Internet
Author: User

Recently, in the project, we found that the Java acquisition system time new date (). tolocalestring () is 13 hours different. This was not the case before, but only after the upgrade.
The specific cause is still learning ing!
The solution is as follows:

Java code
  1. ImportJava. Text. simpledateformat;
  2. ImportJava. util. calendar;
  3. ImportJava. util. date;
  4. ImportJava. util. locale;
  5. ImportJava. util. timezone;
  6. Public ClassDateutil {
  7. Public StaticString getcurrentyyyymmdd (){
  8. Simpledateformat formatter =NewSimpledateformat ("yyyy-mm-dd", locale. US );
  9. Formatter. settimezone (timezone. gettimezone ("GMT + 8 "));
  10. Date currtime =NewDate ();
  11. String thistime =NewString (formatter. Format (currtime ));
  12. ReturnThistime;
  13. }
  14. Public StaticString getcurrentyyyymmddhhmmss (){
  15. Simpledateformat formatter =NewSimpledateformat ("yyyy-mm-dd hh: mm: SS", locale. US );
  16. Formatter. settimezone (timezone. gettimezone ("GMT + 8 "));
  17. Date currtime =NewDate ();
  18. String thistime =NewString (formatter. Format (currtime ));
  19. ReturnThistime;
  20. }
  21. Public Static VoidMain (string [] ARGs ){
  22. System. out. println (DateUtil. getCurrentYYYYMMDD ());
  23. System. out. println (DateUtil. getCurrentYYYYMMDDHHMMSS ());
  24. System. out. println (NewDate (). toLocaleString ());
  25. }
  26. }
import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;import java.util.TimeZone;public class DateUtil {    public static String getCurrentYYYYMMDD(){        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);        formatter.setTimeZone(TimeZone.getTimeZone("GMT+8"));        Date currTime = new Date();        String thisTime = new String(formatter.format(currTime));        return thisTime;}    public static String getCurrentYYYYMMDDHHMMSS(){        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);        formatter.setTimeZone(TimeZone.getTimeZone("GMT+8"));        Date currTime = new Date();        String thisTime = new String(formatter.format(currTime));        return thisTime;}        public static void main(String[] args){        System.out.println(DateUtil.getCurrentYYYYMMDD());System.out.println(DateUtil.getCurrentYYYYMMDDHHMMSS());System.out.println(new Date().toLocaleString());}}

Output result:

Java code
  1. 2008-10-10
  2. 01:52:22
  3. 12:52:22
2008-10-102008-10-10 01:52:222008-10-9 12:52:22

 

 

From: http://qqwyy.javaeye.com/blog/251045

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.