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

FastDateFormat.format() outputs incorrect week of year because locale isn't respected

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5
    • 3.0
    • lang.time.*
    • None
    • Ubuntu 10.04
      Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode)
      Commons Lang 2.5

    Description

      FastDateFormat apparently doesn't respect the locale it was sent on creation when outputting week in year (e.g. "ww") in format(). It seems to use the settings of the system locale for firstDayOfWeek and minimalDaysInFirstWeek, which (depending on the year) may result in the incorrect week number being output.

      Here is a simple test program to demonstrate the problem by comparing with SimpleDateFormat, which gets the week number right:

      import java.util.Calendar;
      import java.util.Date;
      import java.util.Locale;
      import java.text.SimpleDateFormat;
      
      import org.apache.commons.lang.time.FastDateFormat;
      
      public class FastDateFormatWeekBugDemo {
          public static void main(String[] args) {
              Locale.setDefault(new Locale("en", "US"));
              Locale locale = new Locale("sv", "SE");
      
              Calendar cal = Calendar.getInstance(); // setting locale here doesn't change outcome
              cal.set(2010, 0, 1, 12, 0, 0);
              Date d = cal.getTime();
              System.out.println("Target date: " + d);
      
              FastDateFormat fdf = FastDateFormat.getInstance("EEEE', week 'ww", locale);
              SimpleDateFormat sdf = new SimpleDateFormat("EEEE', week 'ww", locale);
              System.out.println("FastDateFormat:   " + fdf.format(d)); // will output "FastDateFormat:   fredag, week 01"
              System.out.println("SimpleDateFormat: " + sdf.format(d)); // will output "SimpleDateFormat: fredag, week 53"
          }
      }
      

      If sv/SE is passed to Locale.setDefault() instead of en/US, both FastDateFormat and SimpleDateFormat output the correct week number.

      Attachments

        1. LANG-645.patch
          2 kB
          Henri Yandell

        Activity

          People

            Unassigned Unassigned
            muv Mikael Uvebrandt
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: