Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:11:55+00:00 2026-06-16T01:11:55+00:00

I have a problem using java.util.Calendar and commons-lang DateUtil The problem is that my

  • 0

I have a problem using java.util.Calendar and commons-lang DateUtil
The problem is that my test works correctly on local machine and fails on CloudBees. Seems like there are problems with locales, but I’m not sure.

Here is the code:

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

//bla-bla-bla

public static final String TEST_DATE_AS_STRING = "13 10 2012 20:50:44";
public static final int MILLIS_IN_HOUR = 3600000;
private static final String LEAP_WEEK_DATE_AS_STRING = "31 10 2012 20:50:44";

private final SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy HH:mm:ss");

@Test
public void getWeekDatePair() throws ParseException{
    Date date = sdf.parse(TEST_DATE_AS_STRING);
    DatePair dp = Util.DateTime.getWeekDatePair(date);
    Assert.assertEquals(sdf.format(new Date(dp.getStart())), "08 10 2012 00:00:00");
    //java.lang.AssertionError: expected [14 10 2012 00:00:00] but found [07 10 2012 00:00:00]
    Assert.assertEquals(sdf.format(new Date(dp.getEnd())), "14 10 2012 00:00:00");
}

@Test
public void getLeapWeekDatePair() throws ParseException {
    Date leapDate = sdf.parse(LEAP_WEEK_DATE_AS_STRING);
    DatePair dp  = Util.DateTime.getWeekDatePair(leapDate);
    Assert.assertEquals(sdf.format(new Date(dp.getStart())), "29 10 2012 00:00:00");

    //java.lang.AssertionError: expected [04 11 2012 00:00:00] but found [28 10 2012 00:00:00]
    Assert.assertEquals(sdf.format(new Date(dp.getEnd())), "04 11 2012 00:00:00");
}

Here is failed test output:

 java.lang.AssertionError: expected [04 11 2012 00:00:00] but found [28 10 2012 00:00:00]
    at org.testng.Assert.fail(Assert.java:94)
    at org.testng.Assert.failNotEquals(Assert.java:494)
    at org.testng.Assert.assertEquals(Assert.java:123)
    at org.testng.Assert.assertEquals(Assert.java:176)
    at org.testng.Assert.assertEquals(Assert.java:186)
    at ru.rating.utils.UtilDateTimeTest.getLeapWeekDatePair(UtilDateTimeTest.java:77)

expected [14 10 2012 00:00:00] but found [07 10 2012 00:00:00]
Stacktrace

java.lang.AssertionError: expected [14 10 2012 00:00:00] but found [07 10 2012 00:00:00]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:494)
at org.testng.Assert.assertEquals(Assert.java:123)
at org.testng.Assert.assertEquals(Assert.java:176)
at org.testng.Assert.assertEquals(Assert.java:186)
at ru.rating.utils.UtilDateTimeTest.getWeekDatePair(UtilDateTimeTest.java:69)

Here is implementation:

    public static DatePair getWeekDatePair(){
        return getWeekDatePair(new Date());
    }


/**
 * This is test method
 * */
        static DatePair getWeekDatePair( Date date){
            Date truncDay = truncate(date.getTime(), Calendar.DAY_OF_MONTH);
            Calendar calStart = getCalendarInstance(date, Calendar.DAY_OF_MONTH);
            calStart.setTime(truncDay);
            calStart.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);        

            Calendar calEnd = Calendar.getInstance();
            calEnd.setTime(calStart.getTime());
            calEnd.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

            return new DatePair(calStart.getTime(), calEnd.getTime());
        }
    public static Date truncate(long date, int calField) {
        Calendar cal = getCalendarInstance(new Date(date), calField);
    cal = DateUtils.truncate(cal, calField);
        return cal.getTime();
    }

static Calendar getCalendarInstance(Date date, int calendarField){
    //Calendar cal = Calendar.getInstance();
    Calendar cal = new GregorianCalendar(Locale.ENGLISH);

    cal.setTime(date);
    if(calendarField!=Calendar.HOUR){
        cal.set(Calendar.HOUR_OF_DAY, 0);
    }
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:11:56+00:00Added an answer on June 16, 2026 at 1:11 am

    Although we are missing key piece of information here (how is Util.DateTime.getWeekDatePair(java.util.Date) implemented), I suspect that what you do there is instantiating java.util.Calendar using default Locale and then search for first day of the week.

    My suspicion came from the fact, that you don’t pass the Locale instance to the getWeekDatePair() method.

    Now, what is the problem here? Well, the first day of the week depends on the Locale. Therefore when you instantiate the Calendar like this: Calendar.getInstance(), what you in fact do is: Calendar.getInstance(Locale.getDefault(Locale.Category.FORMAT). And of course the first day of the week may differ on two different machines, because the Locales may differ.
    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 may expect different results.

    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.


    Edit

    It is quite obvious why it happens from the implementation, and I gave you the hints previously. Consider this 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()));
    

    This prints (correctly):

    13 10 2012 00:00:00
    08 10 2012 00:00:00
    07 10 2012 00:00:00

    Now, let’s change the Calendar instantiation to:

    Calendar cal = Calendar.getInstance(Locale.forLanguageTag("ru-RU"));
    

    Voila, now you’ll see:

    13 10 2012 00:00:00
    08 10 2012 00:00:00
    14 10 2012 00:00:00

    To see why this is the correct behavior, let’s test this code as well:

    System.out.println(cal.getFirstDayOfWeek());
    

    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.

    As you can see, it has nothing to do with DateUtils, it is merely related to Calendar’s behavior. And because of this code: Calendar cal = new GregorianCalendar(Locale.ENGLISH); 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.

    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) or merely modifying test dates so that they match the correct Calendar’s behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
I have a problem with Observer-Pattern and deadlock using threads. package observerDeadLock; import java.util.Observable;
I have created Excel Sheet using Java program. It works fine. My problem is,
I am using Struts and Java. The problem is that I have a page
I am using java on sql server and I have a DB problem of
I am using GWT/JAVA for development. I have following problem: I want to remove
I have a program that uses java.util.TimeZone to produce the GMT offsets for various
I am using java.util.logging.Logger Class for logging in my application. I have added FileHandler
A seemingly straightforward problem: I have a java.util.concurrent.Semaphore , and I want to acquire
I have a property text=000.01 but using java.util.Properties 's method getProperty("text"); returns "0.01" (must

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.