You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Charles Honton (JIRA)" <ji...@apache.org> on 2013/05/17 19:03:15 UTC

[jira] [Commented] (LANG-891) working with multiple date Formats return a wrong date for dd/mm/yy

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

Charles Honton commented on LANG-891:
-------------------------------------

The following unit test demonstrates that parsing is working.
{code}

    @Test
    public void testLang891() throws ParseException {
    	for(Locale locale : Locale.getAvailableLocales()) {
    		for(String tzId : TimeZone.getAvailableIDs()) {
    			TimeZone timezone = TimeZone.getTimeZone(tzId);
    			testLang891(locale, timezone);
    		}
    	}
    }

    public void testLang891(Locale locale, TimeZone timezone) throws ParseException {
      final String pattern = "dd/MM/yy";
      final FastDateFormat parser= FastDateFormat.getInstance(pattern, timezone, locale);
      final Calendar cal= Calendar.getInstance(timezone, locale);
      int definingYear = cal.get(Calendar.YEAR);

      Date date= parser.parse("01/11/09");
      cal.setTime(date);
      cal.setTimeZone(timezone);

      assertEquals(locale.getDisplayLanguage()+" "+timezone.getDisplayName(), expandYear(definingYear, 9), cal.get(Calendar.YEAR));
      assertEquals(locale.getDisplayLanguage()+" "+timezone.getDisplayName(), 10, cal.get(Calendar.MONTH));
      assertEquals(locale.getDisplayLanguage()+" "+timezone.getDisplayName(), 01, cal.get(Calendar.DAY_OF_MONTH));

      date= parser.parse("25/11/09");
      cal.setTime(date);
      cal.setTimeZone(timezone);

      assertEquals(locale.getDisplayLanguage()+" "+timezone.getDisplayName(), expandYear(definingYear, 9), cal.get(Calendar.YEAR));
      assertEquals(locale.getDisplayLanguage()+" "+timezone.getDisplayName(), 10, cal.get(Calendar.MONTH));
      assertEquals(locale.getDisplayLanguage()+" "+timezone.getDisplayName(), 25, cal.get(Calendar.DAY_OF_MONTH));
    }

    private int expandYear(int definingYear, int stubYear) {
    	if(stubYear>100) {
    		return stubYear;
    	}
	    // add century to year - choose the century that starts at most 80 years earlier or 20 years later
    	int thisCentury= stubYear + definingYear - definingYear%100;
        if(thisCentury < definingYear+20) {
            return thisCentury;
        }
        return thisCentury-100;
    }
{code}
                
> working with multiple date Formats return a wrong date for dd/mm/yy
> -------------------------------------------------------------------
>
>                 Key: LANG-891
>                 URL: https://issues.apache.org/jira/browse/LANG-891
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.time.*
>    Affects Versions: 3.2
>         Environment: Windows
>            Reporter: Amit Lodha
>              Labels: date, dateutil, dd/mm/yy
>   Original Estimate: 96h
>  Remaining Estimate: 96h
>
> hi,
> while working with multiple date format, when i have date in format 25/11/09 i.e in dd/MM/yy it return me a wrong result.
> Previous Date: 01/11/09, Returned Date: 2009-01-11
> Previous Date: 25/11/09, Returned Date: 2011-01-11
> i was expecting my return date should be in yyyy-MM-dd format
> Other Formar Result:
> Previous Date: 25.11.2009, Returned Date: 2009-11-25
> Previous Date: 25.Nov.2009, Returned Date: 2009-11-25
> Previous Date: Nov.09, Returned Date: 2009-11-01
> Previous Date: Nov.2009, Returned Date: 2009-11-01
> Previous Date: 09.Nov, Returned Date: 2009-11-01
> Previous Date: Nov2509, Returned Date: 2009-11-25
> Previous Date: 2009-11-25, Returned Date: 2009-11-25

--
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