Org. Apache. Tools. Ant. util. dateutils # format () method bug

Source: Internet
Author: User

When it comes to the date formatting tool class, I have been building my own wheel. Recently, a brother of the project team used apache @ dateutils and found a bug in this class during the test.

Let's take a look at my wheels:

 public static String date2String(Date date, String style)    {        SimpleDateFormat sdf = new SimpleDateFormat(style);        //TimeZone gmt = TimeZone.getTimeZone("GMT+08:00");        //sdf.setTimeZone(gmt);        if (date != null)        {            return sdf.format(date);        }        return null;    }

Under normal circumstances, the two lines of code commented out in the above Code do not need to be added, because simpledateformat will find the time zone of the local server by default, and then set it, of course, the premise is that the time zone of the server is set correctly.

The Apache dateutils # format () method code is as follows:

public static String format(Date date, String pattern)    {        DateFormat df = createDateFormat(pattern);        return df.format(date);    }
 private static DateFormat createDateFormat(String pattern)    {        SimpleDateFormat sdf = new SimpleDateFormat(pattern);        TimeZone gmt = TimeZone.getTimeZone("GMT");        sdf.setTimeZone(gmt);        sdf.setLenient(true);        return sdf;    }

Note: The time zone is GMT, so the formatted time is biased.

To sum up, pay attention to this bug when using Apache @ dateutils.

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.