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

Fragments are wrong by 1 day when using fragment YEAR or MONTH

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.1
    • 3.3
    • lang.time.*
    • None

    Description

      When one trys to get the fragments of a calendar object and uses the fragment YEAR or MONTH, the returned value is wrong by 1 day in the targeted timeunit. The bug resides in the class DateUtils in function

      private static long getFragment(Calendar calendar, int fragment, int unit)
      

      There is an initial recalculation if the fragment is YEAR or MONTH. So if one would like to have the minutes for the fragment YEAR for the date 2000-01-01 00:00:00 this would return 1440 which is actually wrong. The error can be found on lines 1635 - 1643.

      Suggested fix:

      // Fragments bigger than a day require a breakdown to days
              switch (fragment) {
                  case Calendar.YEAR:
                      result += ((calendar.get(Calendar.DAY_OF_YEAR)-1) * MILLIS_PER_DAY) / millisPerUnit;
                      break;
                  case Calendar.MONTH:
                      result += ((calendar.get(Calendar.DAY_OF_MONTH)-1) * MILLIS_PER_DAY) / millisPerUnit;
                      break;
              }
      

      Attachments

        1. LANG-951.patch
          2 kB
          Sebastian Götz

        Issue Links

          Activity

            People

              britter Benedikt Ritter
              s.goetz Sebastian Götz
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - 1h
                  1h
                  Remaining:
                  Remaining Estimate - 1h
                  1h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified