• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala DateTimeFormatter类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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)

} 
开发者ID:durooma,项目名称:api,代码行数:48,代码来源:JsonSupport.scala


示例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)
} 
开发者ID:jrabasco,项目名称:LSBEngine,代码行数:58,代码来源:JSONSupport.scala


示例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")
  }
} 
开发者ID:scamandrill,项目名称:scamandrill,代码行数:41,代码来源:MandrillDateFormats.scala


示例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])
    }
  }

} 
开发者ID:witZack,项目名称:scala-akka-skeleton,代码行数:22,代码来源:ServiceJsonFormatters.scala



注:本文中的org.joda.time.format.DateTimeFormatter类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala PackratParsers类代码示例发布时间:2022-05-23
下一篇:
Scala OutHandler类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap