You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by "Gokul Balakrishnan (JIRA)" <ji...@apache.org> on 2013/04/18 11:31:15 UTC

[jira] [Created] (AXIS2-5512) Incorrect ConverterUtil date, datetime conversion from strings

Gokul Balakrishnan created AXIS2-5512:
-----------------------------------------

             Summary: Incorrect ConverterUtil date, datetime conversion from strings
                 Key: AXIS2-5512
                 URL: https://issues.apache.org/jira/browse/AXIS2-5512
             Project: Axis2
          Issue Type: Bug
          Components: adb
         Environment: Found to be present in Linux Mint 14 and Solaris 10.
            Reporter: Gokul Balakrishnan


Possibly related issue: AXIS2-5458

The methods convertToDate and convertToDateTime output incorrect values for dates before 1928-01-01. For example, 
GIVEN ConverterUtil.convertToDate("1900-01-01")
RESULT is "Sun Dec 31 23:23:24 CST 1899"
GIVEN ConverterUtil.convertToDate("1929-01-01")
RESULT is "Tue Jan 01 00:00:00 CST 1929"

The same issue was observed in convertToDateTime as well.

Note that the local timezone had to be changed to CST to observe the issue. In the local timezone (IST), the bug could not be observed.

-----------------------------------------------------------------------------------------------------------------------------------------------------------

The fix described in AXIS2-5458 did not work, so I had to modify ConverterUtil.convertToDate further and was able to fix this by the following modification. I am not sure whether this will work for all timezones.

public static Date convertToDate(String source) {

        // the lexical form of the date is '-'? yyyy '-' mm '-' dd zzzzzz?
        if ((source == null) || source.trim().equals("")) {
            return null;
        }
        source = source.trim();
        boolean bc = false;
        if (source.startsWith("-")) {
            source = source.substring(1);
            bc = true;
        }

        int year = 0;
        int month = 0;
        int day = 0;
        int timeZoneOffSet = TimeZone.getDefault().getRawOffset();

        if (source.length() >= 10) {
            //first 10 numbers must give the year
            if ((source.charAt(4) != '-') || (source.charAt(7) != '-')){
                throw new RuntimeException("invalid date format (" + source + ") with out - s at correct place ");
            }
            year = Integer.parseInt(source.substring(0,4));
            month = Integer.parseInt(source.substring(5,7));
            day = Integer.parseInt(source.substring(8,10));

            if (source.length() > 10) {
                String restpart = source.substring(10);
                if (restpart.startsWith("Z")) {
                    // this is a gmt time zone value
                    timeZoneOffSet = 0;
                } else if (restpart.startsWith("+") || restpart.startsWith("-")) {
                    // this is a specific time format string
                    if (restpart.charAt(3) != ':'){
                        throw new RuntimeException("invalid time zone format (" + source
                                + ") without : at correct place");
                    }
                    int hours = Integer.parseInt(restpart.substring(1,3));
                    int minits = Integer.parseInt(restpart.substring(4,6));
                    timeZoneOffSet = ((hours * 60) + minits) * 60000;
                    if (restpart.startsWith("-")){
                        timeZoneOffSet = timeZoneOffSet * -1;
                    }
                } else {
                    throw new RuntimeException("In valid string sufix");
                }
            }
        } else {
            throw new RuntimeException("In valid string to parse");
        }

        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setLenient(false);
        calendar.set(Calendar.YEAR, year);
        //xml month stars from the 1 and calendar month is starts with 0
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
       
       //BEGIN MODIFICATION
        if(source.length() >10 ){
            calendar.set(Calendar.ZONE_OFFSET, timeZoneOffSet);

            calendar.get(Calendar.ZONE_OFFSET);

            calendar.set(Calendar.DST_OFFSET, 0);
        }
        //END MODIFICATION

        calendar.getTimeInMillis();
        if (bc){
            calendar.set(Calendar.ERA, GregorianCalendar.BC);
        }

        return calendar.getTime();

    }


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@axis.apache.org
For additional commands, e-mail: java-dev-help@axis.apache.org