Restores the string of the Date object returned by BeanUtils's getProperty method to an object.

Source: Internet
Author: User

Restores the string of the Date object returned by BeanUtils's getProperty method to an object.

Symptom description:

It is intended to convert the string of the Date type returned by the BeanUtils getProperty Method to the Date type through SimpleDateFormat again. The ParseException is thrown.

Analysis:

Call the getProperty method in the BeanUtils object in the commons project to return the Date type member variable of an object. BeanUtil describes the getProperty method as follows:

Return the value of the specified property of the specified bean, no matter which property reference format is used, as a String.

That is to say, no matter what type the member variable is, the value returned by the getProperty method will be a string.

If the type of this member variable is java. util. date, then BeanUtils will call a converter specially prepared for the Date type to convert this Date. In fact, the toString method of Date is actually called.

In some special cases, we need to restore the Date represented by this string to a real Date class for future use.

If an exception is thrown, the conversion format does not match.

Solution Process and achievements:

Check the JDK manual and find the toString method of the Date class. The description indicates that all the Date classes will be converted into the form of "dow mon dd hh: mm: ss zzz yyyy, what is "dow, mon? On the surface, it seems to be "E" and "MMM". From the tracing results of debug, it looks like "e mmm dd hh: mm: ss zzz yyyy ".

However, an error still occurs when converting strings in the above format. No way, directly rush into the JDK source code to view the toString method of the Date class:

public String toString() { // "EEE MMM dd HH:mm:ss zzz yyyy"; BaseCalendar.Date date = normalize(); StringBuilder sb = new StringBuilder(28); int index = date.getDayOfWeek(); if (index == gcal.SUNDAY) {     index = 8; } convertToAbbr(sb, wtb[index]).append(' ');     // EEE convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' ');  // MMM CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':');   // HH CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss TimeZone zi = date.getZone(); if (zi != null) {     sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz } else {     sb.append("GMT"); } sb.append(' ').append(date.getYear());  // yyyy return sb.toString();    }

At this glance, we can see that the date format is indeed "eee mmm dd HH: mm: ss zzz yyyy" (you don't need to read the code. I will tell you it directly in comments ), in this case, I initially determined that its format string is correct. It is indeed "E MMM dd hh: mm: ss zzz yyyy" (the number of E is no different between 1-3 ), so what is the problem?

No way, simply convert a Date object in the format of "e mmm dd hh: mm: ss zzz yyyy" to see what it looks like:

public static void main(String args[]) {  Calendar c = Calendar.getInstance();  SimpleDateFormat format = new SimpleDateFormat("E MMM dd hh:mm:ss z yyyy");  System.out.println(format.format(c.getTime())); }

Output: "Sunday November 04 05:03:52 CST 2007"

Now, you can suddenly understand it. it turns out that we are Chinese and in China, Java library developers are very considerate of us, and the Locale used by SimpleDateFormat class is replaced by the Locale in our position by Human Care, output A Date representation that matches the habits of Chinese people. however, the toString method does not have the same user-friendly settings in the Date toString method. The toString method outputs the US time string format "Sun Nov 04 17:10:26 CST 2007 ". however, the Chinese and American languages are not available, so you can switch to another round without making any mistakes.

The solution is simple, that is, when constructing the SimpleDateFormat object, use the constructor with two parameters to specify Locale as US. You can:

SimpleDateFormat format = new SimpleDateFormat ("e mmm dd hh: mm: ss z yyyy", Locale. US );

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.