Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.util.Calendar confusion
    primarykey
    data
    text
    <p>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.</p> <p>Here is the code:</p> <pre><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"); } </code></pre> <p>Here is failed test output:</p> <pre><code> 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) </code></pre> <p>Here is implementation:</p> <pre><code> 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; } </code></pre>
    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. 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