Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1152

FastDateFormat seems to behave differently with very large dates than simple date format

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.x
    • 3.5
    • lang.time.*
    • None
    • ANY

    Description

      Inconsistent behaviour compared to SimpleDateFormat.
      StringIndexOutOfBoundsException using one format and no exception when using another format. (both legal).
      Different results when using another format.

      Testcase:

      import org.apache.commons.lang3.time.FastDateFormat;
      import org.hamcrest.Matchers;
      import org.junit.Assert;
      import org.junit.Test;

      import java.text.SimpleDateFormat;
      import java.util.Date;

      public class FastDateFormatBug {

      @Test
      public void testFastFormatWitLongMaxValue()

      { Date date = new Date(Long.MAX_VALUE); String dateAsString = FastDateFormat.getInstance("yyyy-MM-dd").format(date); Assert.assertThat(dateAsString, Matchers.equalTo("292278994-08-17")); }

      @Test
      public void testWeirdResultWithMaxValue()

      { Date date = new Date(Long.MAX_VALUE); String dateAsString = FastDateFormat.getInstance("dd/MM/yyyy").format(date); Assert.assertThat(dateAsString, Matchers.equalTo("292278994-08-17")); }

      @Test
      public void testSimpleFormatWithLongMaxValueWithLenientOff()

      { Date date = new Date(Long.MAX_VALUE); SimpleDateFormat fft = new SimpleDateFormat("yyyy-MM-dd"); fft.setLenient(false); String dateAsString = fft.format(date); Assert.assertThat(dateAsString, Matchers.equalTo("292278994-08-17")); }

      @Test
      public void testSimpleFormatWithLongMaxValueWithLenientOn()

      { Date date = new Date(Long.MAX_VALUE); SimpleDateFormat fft = new SimpleDateFormat("yyyy-MM-dd"); fft.setLenient(true); String dateAsString = fft.format(date); Assert.assertThat(dateAsString, Matchers.equalTo("292278994-08-17")); }

      }

      Attachments

        Activity

          People

            chonton Charles Honton
            pasfilip Pas Filip
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: