javaspringdatetimemocking

Java Date Time discrepancy


While writing integration test I was expecting a hardcoded date in the response.

Basically I hardcoded expected date value '2020-11-10T00:00:00.000-05:00' and ran new GregorianCalendar(2020, 10, 10).getTime()

When I put and assert and run it locally it passes, however when the same code was pushed to bamboo build server the actualValue it generated was'2020-11-10T00:00:00.000Z' and so my test failed.

  1. Why the same calendar.getTime generating two different times, is it because the server machine is configured to be on GMT?

  2. Can I do something to have them the same time or any other workaround?

NOTE: Making it string or comparing dates without time is not an option here, as I am using Spring MockWebServiceServer, where in I must hardcode the responseXML and specify date, something like this in Enum.

 REQUESTAUTOMATESETTLEMENTWORKCASE("<aut:AutomateSettlementWorkcaseRequest xmlns:aut=\"http://www.abcd.com/abcd/workflow/services/workcase/model/AutomateSettlementWorkcase_1_0_0\">" +
          "  <aut:customerAccountId>5049903033584324</aut:customerAccountId>\n" +
          "  <aut:settlementDate>2020-11-10T00:00:00.000-05:00</aut:settlementDate>\n" +
          "  </aut:AutomateSettlementWorkcaseRequest>"),

Solution

  • The "-5" part in your input data is throwing it off... ideally you should specify the time zone when constructing the calendar, and then set it to 5am UTC (for example). Basically, 2020-11-10T00:00:00.000-05:00 is the same instant as 2020-11-10T05:00:00.000Z.

    Now, what we don't know is whether it's important to you that you preserve the offset from UTC. If it is, you need to set an appropriate time zone in the calendar - one which has the same rules as whatever's generating your input data. If it isn't, I'd use UTC and set the time appropriately.

    I would personally recommend using Joda Time instead of Calendar and Date though - it's a much better date and time API. No 0-based months, for starters :)