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 9041057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:08:45+00:00 2026-06-16T10:08:45+00:00

I am stuck on the following problem : I want to write a specs2

  • 0

I am stuck on the following problem : I want to write a specs2 specification to assert that my to and from json transformations are symetrical. However, I get an error on joda datetime dates.

'2012-04-17T00:04:00.000+02:00' is not equal to '2012-04-17T00:04:00.000+02:00'. Values have the same string representation but possibly different types like List[Int] and List[String] (TimeSpecs.scala:18) 

Here is a minimalist specs demonstrating the problem

import org.joda.time.DateTime
import org.specs2.mutable.Specification

class TimeSpecs extends Specification {
  "joda and specs2" should {
    "play nice" in {
      val date = DateTime.parse("2012-04-17T00:04:00+0200")
      val date2 = DateTime.parse("2012-04-17T00:04:00+0200")
      date === date2
    }
    "play nice through play json transform" in {
      import play.api.libs.json._
      import play.api.libs.json.Json._
      val date = DateTime.parse("2012-04-17T00:04:00+0200")
      val jsDate= toJson(date)
      val date2= jsDate.as[DateTime]

      date === date2
    }
  }
}

how should I compare date and date2 in the second test ? they are the same but specs2 doesn’t seem to see that 🙁

— edit

“manually” inspecting the type at runtime with date.getClass.getCanonicalName returns org.joda.time.Datetime as expected

import org.joda.time.DateTime
import org.specs2.mutable.Specification

class TimeSpecs extends Specification {
  "joda and specs2" should {
    "play nice" in {
      val date = DateTime.parse("2012-04-17T00:04:00+0200")
      val date2 = DateTime.parse("2012-04-17T00:04:00+0200")
      date === date2
    }
    "play nice through play json transform" in {
      import play.api.libs.json._
      import play.api.libs.json.Json._
      val date:DateTime = DateTime.parse("2012-04-17T00:04:00+0200")
      val jsDate= toJson(date)
      val date2:DateTim= jsDate.as[DateTime]
      println(date.getClass.getCanonicalName) //prints org.joda.time.DateTime
      println(date2.getClass.getCanonicalName)//prints org.joda.time.DateTime
      date === date2
    }
  }
}

Using DateTime#isEqual does kind of work but I loose the benefit of fluent matchers and the useful error messages they bring. Aditionally, what I am actually trying to compare are case class instances which happen to contain dates, not the dates themselves.

Using

      date should beEqualTo(date2)

yields the same error as ===

  • 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-16T10:08:46+00:00Added an answer on June 16, 2026 at 10:08 am

    The problem is that joda time defines a very strict equals which considers the date’s Chronology for the equality ( DateTime#getChronology ). The isEqual method proposed by Kim Stebel does ignore the Chronology.

    From there on, there are 2 possibilities: Defining custom read and writes for play, then using the same pattern to create the dates as in the following example

    import org.joda.time.DateTime
    import org.joda.time.format.DateTimeFormat
    import org.specs2.mutable.Specification
    
    class TimeSpecs extends Specification {
        val pattern = "yyyy-MM-dd'T'HH:mm:ssZZ"
      "joda and specs2" should {
        "play nice" in {
          val date = DateTime.parse("2012-04-17T00:04:00+0200",DateTimeFormat.forPattern(pattern))
          val date2 = DateTime.parse("2012-04-17T00:04:00+0200",DateTimeFormat.forPattern(pattern))
          date === date2
        }
        "play nice through play json transform" in {
          import play.api.libs.json.Json._
    
          //play2 custom write
          implicit def customJodaWrite = play.api.libs.json.Writes.jodaDateWrites(pattern)
          //play2 custom read
          implicit def customJodaRead = play.api.libs.json.Reads.jodaDateReads(pattern) 
    
    
          val date:DateTime = DateTime.parse("2012-04-17T00:04:00+0200",DateTimeFormat.forPattern(pattern)) //make sure you parse the initial date with the same pattern
          val jsDate= toJson(date)
          val date2:DateTime= jsDate.as[DateTime]
    
          println(date.getClass.getCanonicalName)
          println(date2.getClass.getCanonicalName)
          println(jsDate)
          date should beEqualTo(date2)
        }
      }
    }
    

    Play 2.1 defaults to parsing (and writing to json) based on the unix timestamp in milliseconds without timezone information. When parsing back from the unix timestamp, it will consider it in the local computer timezone (in my case Europe/Paris). Hence the need for a custom parser/writer

    Joda uses a specific formatter when calling parse without a parser argument, it doesn’t seem possible to create the same formatter with only a pattern string ( I haven’t found a way to activate the DateTimeFormatter#withOffsetParsed method through a pattern string).

    Another possibility may be to define a custom specs2 matcher for jodatime which would use isEqual instead of equals.
    Since I don’t want the unix epoch in my json anyway, I’ll stick with the custom play transformers

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

Sidebar

Related Questions

I am stuck in the following problem from a long time - I want
I'm stuck with the following problem: I've got a spreadsheet where I want to
I'm stuck with the following problem. My backend (Java sevlet) returns from a database
I am stuck and need outside help from JSF experts with the following problem:
So the problem is the following: I have an action method that returns json.
I am stuck with following problem.I want my application to exit if it detects
I'm trying to write a trigger that deals with the following problem . I'm
I'm new to wicket and stuck with the following problem: I have a table
I'm a beginner programmer and I'm stuck with the following problem: If I have
I'm stuck with the following. I'm programming a game and I need to get

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.