Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although we are missing key piece of information here (how is <code>Util.DateTime.getWeekDatePair(java.util.Date)</code> implemented), I suspect that what you do there is instantiating <code>java.util.Calendar</code> using default <code>Locale</code> and then search for first day of the week.</p> <p>My suspicion came from the fact, that you don't pass the Locale instance to the <code>getWeekDatePair()</code> method.</p> <p>Now, what is the problem here? Well, the first day of the week depends on the Locale. Therefore when you instantiate the <code>Calendar</code> like this: <code>Calendar.getInstance()</code>, what you in fact do is: <code>Calendar.getInstance(Locale.getDefault(Locale.Category.FORMAT)</code>. And of course the first day of the week may differ on two different machines, because the Locales <em>may</em> differ.<br> For example, first day of the week is Sunday in US, but Monday in Poland (I believe it is like that in Russia, isn't it?) Therefore if you do this test on two different machines, fist of which has Locale set to en-US and second to ru-RU, you <em>may</em> expect different results.</p> <p>If it is only the problem of tests, you may just as well set default Locale and everything should be working just fine. However, please keep in mind that if you are testing web application, using default Locale is a bad thing, as it will return server Locale rather than the one that comes from web browser (or some user profile if you have one). Should this Locales differ, you might use something that is confusing for end user.</p> <hr> <h2>Edit</h2> <p>It is quite obvious why it happens from the implementation, and I gave you the hints previously. Consider this code:</p> <pre><code>Calendar cal = Calendar.getInstance(Locale.ENGLISH); System.out.println(sdf.format(cal.getTime())); // Cloning so that Calendar could be re-used Calendar calEnd = (Calendar) cal.clone(); // Setting start of the week date cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); System.out.println(sdf.format(cal.getTime())); calEnd.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); System.out.println(sdf.format(calEnd.getTime())); </code></pre> <p>This prints (correctly):</p> <blockquote> <p>13 10 2012 00:00:00<br> 08 10 2012 00:00:00<br> 07 10 2012 00:00:00</p> </blockquote> <p>Now, let's change the Calendar instantiation to:</p> <pre><code>Calendar cal = Calendar.getInstance(Locale.forLanguageTag("ru-RU")); </code></pre> <p>Voila, now you'll see:</p> <blockquote> <p>13 10 2012 00:00:00<br> 08 10 2012 00:00:00<br> 14 10 2012 00:00:00</p> </blockquote> <p>To see why <em>this is the correct behavior</em>, let's test this code as well:</p> <pre><code>System.out.println(cal.getFirstDayOfWeek()); </code></pre> <p>For English Locale, it will return 1 (Sunday) as oppose to 2 (Monday) which is the result for Russian Locales. This code behaves correctly, as it returns Monday and Sunday from given week. The only "problem" is the fact, that week means something else all over the world.</p> <p>As you can see, it has nothing to do with DateUtils, it is merely related to Calendar's behavior. And because of this code: <code>Calendar cal = new GregorianCalendar(Locale.ENGLISH);</code> the behavior should in fact be consistent, so you should always get an error no matter what machine you are testing the code on. It it is not, I really can't understand why.</p> <p>Depending on what you are trying to achieve, it may make sense to add Locale as a parameter to your method (and instantiate Calendar accordingly), then write some tests covering few Locales (some Arabic Locale may also be interesting as nobody said first day of the week has to be either Sunday or Monday) <strong>or</strong> merely modifying test dates so that they match the correct Calendar's behavior.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload