Uploaded image for project: 'Axis2'
  1. Axis2
  2. AXIS2-5512

Incorrect ConverterUtil date, datetime conversion from strings

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.6.0
    • None
    • adb
    • Found to be present in Linux Mint 14 and Solaris 10.

    Description

      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();

      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            gokul@wso2.com Gokul Balakrishnan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: