本文整理汇总了Scala中org.joda.time.format.DateTimeFormatter类的典型用法代码示例。如果您正苦于以下问题:Scala DateTimeFormatter类的具体用法?Scala DateTimeFormatter怎么用?Scala DateTimeFormatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DateTimeFormatter类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: DateJsonFormat
//设置package包名称以及导入依赖的类
package com.durooma.api.route
import java.sql.Date
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import com.durooma.api.model._
import com.durooma.db.Tables
import org.joda.time.DateTime
import org.joda.time.format.{DateTimeFormatter, ISODateTimeFormat}
import spray.json.{DefaultJsonProtocol, DeserializationException, JsString, JsValue, RootJsonFormat}
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit object DateJsonFormat extends RootJsonFormat[DateTime] {
private val parserISO : DateTimeFormatter = ISODateTimeFormat.dateTimeNoMillis()
override def write(obj: DateTime) = JsString(parserISO.print(obj))
override def read(json: JsValue) : DateTime = json match {
case JsString(s) => parserISO.parseDateTime(s)
case _ => throw DeserializationException("Invalid date format: " + json)
}
}
implicit object SqlDateJsonFormat extends RootJsonFormat[Date] {
override def write(obj: Date) = JsString(obj.toString)
override def read(json: JsValue) = json match {
case JsString(s) => Date.valueOf(s)
case _ => throw DeserializationException("Invalid date format: " + json)
}
}
implicit val userFormat = jsonFormat5(User.apply)
implicit val userRegistrationFormat = jsonFormat5(UserRegistration.apply)
implicit val accountFormat = jsonFormat4(Account.apply)
implicit val accounBodyFormat = jsonFormat2(AccountBody.apply)
implicit val labelFormat = jsonFormat3(Tables.LabelRow.apply)
implicit val transactionFormat = jsonFormat8(Transaction.apply)
implicit val transactionBodyFormat = jsonFormat7(TransactionBody.apply)
implicit val sessionFormat = jsonFormat3(Session.apply)
implicit val credentialsFormat = jsonFormat2(CustomCredentials.apply)
}
示例2: DateTimeFormat
//设置package包名称以及导入依赖的类
package me.lsbengine.json
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import me.lsbengine.api.admin.security.NewCredentials
import me.lsbengine.api.model.{FetchPostResponse, ListActionResponse, PostCreationResponse, TokenResponse}
import me.lsbengine.database.model.{AboutMe, HtmlMarkdownContent, NavBarConf, Post}
import org.joda.time.DateTime
import org.joda.time.format.{DateTimeFormatter, ISODateTimeFormat}
import spray.json._
import scala.util.Try
trait JSONSupport extends SprayJsonSupport with DefaultJsonProtocol with CollectionFormats {
implicit object DateTimeFormat extends RootJsonFormat[DateTime] {
val formatter: DateTimeFormatter = ISODateTimeFormat.dateTime
override def read(json: JsValue): DateTime = json match {
case JsString(s) => Try(formatter.parseDateTime(s)).fold(_ => error(s), identity)
case _ =>
error(json.toString())
}
override def write(obj: DateTime): JsValue = {
JsString(formatter.print(obj))
}
def error(v: Any): DateTime = {
val example = formatter.print(0)
deserializationError(s"$v is not a valid date. Dates must be in ISO formatForForm : $example")
}
}
//USED ONLY FOR BUILDINFO OTHERWISE STRANGE BEHAVIOUR
implicit object AnyFormat extends JsonFormat[Any] {
override def write(obj: Any): JsValue = JsString(s"$obj")
override def read(json: JsValue): Any = json match {
case JsString(s) => s
case _ =>
deserializationError(s"Expected a string, got $json")
}
}
implicit val htmlMarkdownContentFormat: RootJsonFormat[HtmlMarkdownContent] = jsonFormat2(HtmlMarkdownContent)
implicit val postFormat: RootJsonFormat[Post] = jsonFormat5(Post)
implicit val aboutMeFormat: RootJsonFormat[AboutMe] = jsonFormat2(AboutMe)
implicit val navbarFormat: RootJsonFormat[NavBarConf] = jsonFormat2(NavBarConf)
implicit val tokenResponseFormat: RootJsonFormat[TokenResponse] = jsonFormat1(TokenResponse)
implicit val listActionResponseFormat: RootJsonFormat[ListActionResponse] = jsonFormat1(ListActionResponse)
implicit val fetchPostResponseFormat: RootJsonFormat[FetchPostResponse] = jsonFormat1(FetchPostResponse)
implicit val postCreationResponseFormat: RootJsonFormat[PostCreationResponse] = jsonFormat1(PostCreationResponse)
implicit val newCredentialsFormat: RootJsonFormat[NewCredentials] = jsonFormat3(NewCredentials)
}
示例3: MandrillDateFormats
//设置package包名称以及导入依赖的类
package io.github.scamandrill.models
import org.joda.time.format.{DateTimeFormat, DateTimeFormatter, DateTimeFormatterBuilder}
import org.joda.time.{DateTime, DateTimeZone}
import play.api.libs.json._
object MandrillDateFormats {
sealed trait MandrillDateFormatter extends Writes[DateTime] with Reads[DateTime] {
def dateFormat: DateTimeFormatter
override def writes(o: DateTime): JsValue = JsString(o.toString(dateFormat))
override def reads(json: JsValue): JsResult[DateTime] =
json.validate[String]
.map(dateFormat.parseDateTime)
.map(_.toDateTime(DateTimeZone.getDefault))
}
object DATETIME_FORMAT extends MandrillDateFormatter {
override def dateFormat = {
val baseFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
val secondsFractionParser = new DateTimeFormatterBuilder()
.appendLiteral('.')
.appendFractionOfSecond(0, 9)
.toParser
new DateTimeFormatterBuilder()
.append(
baseFormatter.getPrinter,
new DateTimeFormatterBuilder()
.append(baseFormatter.getParser)
.appendOptional(secondsFractionParser)
.toParser
)
.toFormatter.withZoneUTC()
}
}
object DATE_FORMAT extends MandrillDateFormatter {
override def dateFormat = DateTimeFormat.forPattern("yyyy-MM-dd")
}
}
示例4: write
//设置package包名称以及导入依赖的类
package com.app.service.model
import org.joda.time.DateTime
import org.joda.time.format.{ DateTimeFormatter, ISODateTimeFormat }
import spray.json._
trait ServiceJsonFormatters extends DefaultJsonProtocol {
val formatter: DateTimeFormatter = ISODateTimeFormat.dateTime()
// Joda - DateTime
implicit val jodaDateTimeJFConverter = new JsonFormat[DateTime] {
override def write(obj: DateTime): JsValue = {
formatter.print(obj).toJson
}
override def read(js: JsValue): DateTime = {
formatter.parseDateTime(js.convertTo[String])
}
}
}