Skip to content

Instantly share code, notes, and snippets.

@hongwei1
Created March 8, 2017 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongwei1/7c2ea8cf6aaa9f0389660744e48eb146 to your computer and use it in GitHub Desktop.
Save hongwei1/7c2ea8cf6aaa9f0389660744e48eb146 to your computer and use it in GitHub Desktop.
time format
successJsonResponse(Extraction.decompose(json)(formats = new Formats {
import java.text.{ParseException, SimpleDateFormat}
val dateFormat = new DateFormat {
def parse(s: String) = try {
Some(formatter.parse(s))
} catch {
case e: ParseException => None
}
def format(d: Date) = formatter.format(d)
private def formatter = {
val f = dateFormatter
f.setTimeZone(new GregorianCalendar().getTimeZone)
f
}
}
protected def dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
}))
@hongwei1
Copy link
Author

hongwei1 commented Mar 8, 2017

val importJsonDateFormat = {
val f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
//setting the time zone is important!
f.setTimeZone(TimeZone.getTimeZone("UTC"))
f
}

  t.transactionType should equal(dummyKind)

  //compare time as a long to avoid issues comparing Dates, e.g. java.util.Date vs java.sql.Date
  t.startDate.getTime should equal(importJsonDateFormat.parse(startDate).getTime) 
  t.finishDate.getTime should equal(importJsonDateFormat.parse(endDate).getTime)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment