[ 
https://issues.apache.org/jira/browse/AXIS2-5494?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13594314#comment-13594314
 ] 

David Rhee commented on AXIS2-5494:
-----------------------------------

Patched convertToDate() method for backward compatibility

{code}
    /**
     * Converts a given string into a date. Code from Axis1 DateDeserializer.
     *
     * @param source
     * @return Returns Date.
     */
    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 timeOffsetSeconds = 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("-") 
|| restpart.startsWith("T")) {
                    // this is a specific time format string
                    if (restpart.charAt(3) != ':'){
                        throw new RuntimeException("invalid time zone format (" 
+ source
                                + ") without : at correct place");
                    }
                    
                    int tzOffsetSign = 1;
                    int tzStartIdx = restpart.lastIndexOf("-");
                    if(tzStartIdx == -1) {
                        tzStartIdx = restpart.lastIndexOf("+");
                    } else {
                        tzOffsetSign = -1;
                    }
                    
                    int hours = 0;
                    int minits = 0;
                    int seconds = 0;
                    
                    if(tzStartIdx > 5) { // contains a 'T'
                        hours = Integer.parseInt(restpart.substring(1,3));
                        minits = Integer.parseInt(restpart.substring(4,6));
                        
                        if(tzStartIdx > 8) { // contains seconds
                                seconds = 
Integer.parseInt(restpart.substring(7, 9));
                        }
                        
                        timeOffsetSeconds  = hours * 3600 + minits * 60 + 
seconds;
                    }
                    
                    if(tzStartIdx > -1) {
                        int tzHours = 
Integer.parseInt(restpart.substring(tzStartIdx+1, tzStartIdx+3));
                        int tzMinits = 
Integer.parseInt(restpart.substring(tzStartIdx+4, tzStartIdx+6));
                            timeZoneOffSet = ((tzHours * 60) + tzMinits) * 
60000;
                            timeZoneOffSet *= tzOffsetSign;
                    }
                } 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);
        calendar.add(Calendar.SECOND, timeOffsetSeconds);
        calendar.set(Calendar.ZONE_OFFSET, timeZoneOffSet);

        // set the day light off set only if time zone
        if (source.length() >= 10) {
            calendar.set(Calendar.DST_OFFSET, 0);
        }
        calendar.getTimeInMillis();
        if (bc){
            calendar.set(Calendar.ERA, GregorianCalendar.BC);
        }

        return calendar.getTime();

    }
{code}
                
> Date conversion does not handle timzone offset correctly for date strings 
> containing seconds
> --------------------------------------------------------------------------------------------
>
>                 Key: AXIS2-5494
>                 URL: https://issues.apache.org/jira/browse/AXIS2-5494
>             Project: Axis2
>          Issue Type: Bug
>          Components: databinding
>    Affects Versions: 1.6.2
>            Reporter: David Rhee
>
> The date format containing seconds and timezone offset as follows generates 
> an exception:
> yyyy-MM-dd'T'hh:mm:ssZ
> e.g.:
> 2013-03-04T15:07:14+11:00
> The web service call fails with the exception:
> Caused by: java.lang.IllegalArgumentException: ZONE_OFFSET
>       at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2316)
>       at java.util.Calendar.updateTime(Calendar.java:2469)
>       at java.util.Calendar.getTimeInMillis(Calendar.java:1088)
>       at 
> org.apache.axis2.databinding.utils.ConverterUtil.convertToDate(ConverterUtil.java:623)
> ConvertUtil.java line 596:
>                     int hours = Integer.parseInt(restpart.substring(1,3));
>                     int minits = Integer.parseInt(restpart.substring(4,6));
>                     timeZoneOffSet = ((hours * 60) + minits) * 60000;

--
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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to