Author: adrianc
Date: Thu Nov 12 16:52:23 2009
New Revision: 835440

URL: http://svn.apache.org/viewvc?rev=835440&view=rev
Log:
Changed the String to Timestamp converter code to fix a problem reported by 
Hans.

Modified:
    
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java

Modified: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java?rev=835440&r1=835439&r2=835440&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java
 (original)
+++ 
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java
 Thu Nov 12 16:52:23 2009
@@ -400,18 +400,35 @@
         }
 
         public java.sql.Timestamp convert(String obj, Locale locale, TimeZone 
timeZone, String formatString) throws ConversionException {
-            String trimStr = obj.trim();
-            if (trimStr.length() == 0) {
+            String str = obj.trim();
+            if (str.length() == 0) {
                 return null;
             }
             DateFormat df = null;
             if (formatString == null || formatString.length() == 0) {
+                // These hacks are a bad idea, but they are included
+                // for backward compatibility.
+                if (str.length() > 0 && !str.contains(":")) {
+                    str = str + " 00:00:00.00";
+                }
+                // hack to mimic Timestamp.valueOf() method
+                if (str.length() > 0 && !str.contains(".")) {
+                    str = str + ".0";
+                } else {
+                    // DateFormat has a funny way of parsing milliseconds:
+                    // 00:00:00.2 parses to 00:00:00.002
+                    // so we'll add zeros to the end to get 00:00:00.200
+                    String[] timeSplit = str.split("[.]");
+                    if (timeSplit.length > 1 && timeSplit[1].length() < 3) {
+                        str = str + "000".substring(timeSplit[1].length());
+                    }
+                }
                 df = 
UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
             } else {
                 df = UtilDateTime.toDateTimeFormat(formatString, timeZone, 
locale);
             }
             try {
-                return new java.sql.Timestamp(df.parse(trimStr).getTime());
+                return new java.sql.Timestamp(df.parse(str).getTime());
             } catch (ParseException e) {
                 throw new ConversionException(e);
             }


Reply via email to