score:0
import java.time.localdatetime
case class user(createdat: localdatetime)
implicit val userreads = json.reads[user]
score:2
there is already predefined format for dates defaultlocaldatetimereads
:
import java.time.localdatetime
val json = json.parse("""{"date": "2016-05-30t00:23:27.070z"}""")
(json \ "date").as[localdatetime]
in case you need some other datetime library/format, you could write custom reader like this one:
import org.joda.time.datetime
import play.api.libs.json.{jserror, _}
implicit object datetimereads extends reads[datetime] {
val format = org.joda.time.format.datetimeformat
.forpattern("yyyy-mm-dd't'hh:mm:ss.sss'z'")
def reads(json: jsvalue) = json match {
case jsstring(x) => jssuccess(format.parsedatetime(x))
case _ => jserror(s"can't read $json as datetime")
}
}
(json \ "date").as[datetime]
res0: org.joda.time.datetime = 2016-05-30t00:23:27.070+03:00
Source: stackoverflow.com
Related Query
- What date/time class should I be mapping too?
- What type should I declare a DateTime object in a scala class constructor?
- I don't get what should be the C[_] type in Slick 2.1 Query class
- What datetime class should I use for my case classes?
- what should be the type of attributes in case class with Akka
- What is the difference between Scala's case class and class?
- What is the formal difference in Scala between braces and parentheses, and when should they be used?
- Class broken error with Joda Time using Scala
- What is the point of the class Option[T]?
- What is *so* wrong with case class inheritance?
- What are the differences between final class and sealed class in Scala?
- What is the difference between a class and a type in Scala (and Java)?
- How to convert String to date time in Scala?
- What replaces class variables in scala?
- How to get the current date without time in scala
- What is/are the Scala way(s) to implement this Java "byte[] to Hex" class
- What does the @elidable annotation do in Scala, and when should I use it?
- What does => mean at the beginning of a Scala class definition?
- How do I declare a constructor for an 'object' class type in Scala? I.e., a one time operation for the singleton
- "functions are first class values" what does this exactly mean?
- What parts of the Java ecosystem and language should a developer learn to get the most out of Scala?
- What should a Scala developer know about Java and/or the JVM?
- Scala: what is the real difference between fields in a class and parameters in the constructor
- What does "reflective access of structural type member method should be enabled..." warning mean in Scala?
- Scala 2.11 LinkedList is deprecated, what should I use?
- What library should I use for accessing Riak from Scala?
- What are Scala's future platform concerns people should be prepared for?
- Custom mapping to nested case class structure in Slick (more than 22 columns)
- What is the performance impact of using the type class pattern in Scala
- dynamically create class in scala, should I use interpreter?
More Query from same tag
- How to start external application from Scala
- Better way of compressing a String
- successive keys in an rdd
- How to set class name in TextArea in Play2 Framework Scala template?
- How do I write a trait that can mix in overrides of CRUDify members (that need to access the User model) in Mapper models?
- Regex replace doesnt match when there is a dot in the text in Scala
- How to mirror schema in Spark data frame
- Spark with Scala and Pandas
- Subtyping leads to Any: Bug in compiler or issue with my code?
- ClassCastException when trying to insert with Squeryl
- Search for strings in the prefix tree. Abstract suggest
- Join two dataframes and replace the original column values using Spark Scala
- hasDefiniteSize and knownSize
- Issue with using Macros in SBT
- is a method parameter implicit? - using scala 2.10 reflection
- Generate all binary words of length n
- How to do POST request with Newman?
- Compilation error with Ordered in Scala
- Spark dataframe transform in time window
- Spark job doesn't print when running on multiple clusters on databrick notebook
- How pass implicit parameters in akka with inheritance?
- Why can't the compiler infer type parameter for package object (while it works fine for object)?
- Scala free monad combining with Doobie
- Lift algebird aggregator to consume (and return) Map
- what is the an implicit conversion must be more specific
- Can you capture and inspect arguments supplied to a method under test with ScalaMock?
- How to exclude routes and ReverseRoutes files auto generated using scoverage for play scala framework?
- Constructor cannot be instantiated to expected type; p @ Person
- xmllint: XML won't validate because of whitespace inserted by prettyprinter
- In memory cache persisted between batches spark structured streaming