当前位置: 首页>>代码示例>>Scala>>正文


Scala DateTimeParseException类代码示例

本文整理汇总了Scala中java.time.format.DateTimeParseException的典型用法代码示例。如果您正苦于以下问题:Scala DateTimeParseException类的具体用法?Scala DateTimeParseException怎么用?Scala DateTimeParseException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了DateTimeParseException类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: Rfc3339Util

//设置package包名称以及导入依赖的类
package de.zalando.play.controllers

import java.time.format.{ DateTimeFormatter, DateTimeParseException }
import java.time.{ LocalDate, ZoneId, ZonedDateTime }


object Rfc3339Util {

  private val fullDate = DateTimeFormatter.ofPattern("yyyy-MM-dd")
  private val shortDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")
  private val shortDTWithTicks = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
  private val fullDTWithTicks = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'")
  private val dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSZ")

  def parseDateTime(datestring: String): ZonedDateTime =
    if (datestring.endsWith("Z") || datestring.endsWith("z")) parseFull(datestring)
    else parseParts(datestring)

  def parseDate(datestring: String): LocalDate =
    LocalDate.parse(datestring, fullDate)

  def writeDate(date: LocalDate): String = fullDate.format(date)

  def writeDateTime(date: ZonedDateTime): String = dateTime.format(date)

  private def parseParts(datestring: String): ZonedDateTime = {
    //step one, split off the timezone.
    val sepChar = if (datestring.indexOf('+') > 0) '+' else '-'
    val firstpart = datestring.substring(0, datestring.lastIndexOf(sepChar.toInt))
    val secondpart = datestring.substring(datestring.lastIndexOf(sepChar.toInt))
    //step two, remove the colon from the timezone offset
    val thirdpart = secondpart.substring(0, secondpart.indexOf(':')) + secondpart.substring(secondpart.indexOf(':') + 1)
    val dstring = firstpart + thirdpart
    try {
      ZonedDateTime.parse(dstring, shortDateTime)
    } catch {
      case pe: DateTimeParseException =>
        ZonedDateTime.parse(dstring, dateTime)
    }
  }

  private def parseFull(datestring: String): ZonedDateTime = {
    val z = ZoneId.systemDefault()
    try {
      ZonedDateTime.parse(datestring, shortDTWithTicks.withZone(z))
    } catch {
      case p: DateTimeParseException => ZonedDateTime.parse(datestring, fullDTWithTicks.withZone(z))
    }
  }

} 
开发者ID:LappleApple,项目名称:api-first-hand,代码行数:52,代码来源:Rfc3339Util.scala

示例2: CustomExceptionHandling

//设置package包名称以及导入依赖的类
package com.queirozf.utils

import java.time.format.DateTimeParseException

import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.ExceptionHandler
import com.queirozf.utils.ResponseUtils._
import org.slf4j.LoggerFactory

import scala.util.control.NonFatal


object CustomExceptionHandling {
  private val logger = LoggerFactory.getLogger("balance-tracker.errors")

  def handler = ExceptionHandler {

    case dpe: DateTimeParseException => complete(JsonError(dpe))
    case NonFatal(nf) => {
      logger.error("Non-fatal error thrown: ", nf)
      // avoid leaking information
      complete(JsonError(new Exception("An unexpected error occurred.")))
    }
    case t: Throwable => {
      logger.error("Fatal error thrown: ", t)

      complete(JsonError(new Exception("An unexpected error occurred.")))
    }
  }
} 
开发者ID:queirozfcom,项目名称:akka-http-docker-aws-code-pipeline-beanstalk,代码行数:31,代码来源:CustomExceptionHandling.scala

示例3: Decoders

//设置package包名称以及导入依赖的类
package eveapi.utils

import argonaut._
import java.time.format.DateTimeParseException
import org.http4s.Uri
import java.time._
import scalaz._, Scalaz._

object Decoders {
  implicit val urlCodec: CodecJson[Uri] = CodecJson(
      (uri: Uri) => Json.jString(uri.toString),
      c =>
        c.as[String]
          .flatMap(str =>
                Uri
                  .fromString(str)
                  .fold(err => DecodeResult.fail(err.toString, c.history), DecodeResult.ok))
  )
  implicit val instantCodec: CodecJson[Instant] = CodecJson(
      (instant: Instant) => Json.jString(instant.toString),
      c =>
        c.as[String]
          .flatMap(str =>
                \/.fromTryCatchNonFatal(Instant.parse(str))
                  .orElse(
                      \/.fromTryCatchNonFatal(LocalDateTime.parse(str).toInstant(ZoneOffset.UTC)))
                  .fold({
                case e: DateTimeParseException => DecodeResult.fail(e.toString, c.history)
                case e => throw e
              }, DecodeResult.ok))
  )
} 
开发者ID:scala-eveapi,项目名称:eveapi,代码行数:33,代码来源:codecs.scala

示例4: Binders

//设置package包名称以及导入依赖的类
package controllers

import java.time.LocalDate
import java.time.format.DateTimeParseException

import models.Stats.TimePeriod
import play.api.mvc.QueryStringBindable

object Binders {

  implicit def queryStringBindable(implicit stringBinder: QueryStringBindable[String]) =
    new QueryStringBindable[LocalDate] {
      override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, LocalDate]] = {
        stringBinder.bind(key, params).map {
          _.right.flatMap { dateStr =>
            try {
              Right(LocalDate.parse(dateStr))
            } catch {
              case e: DateTimeParseException => Left("Failed to parse date")
            }
          }
        }
      }

      override def unbind(key: String, localDate: LocalDate): String =
        stringBinder.unbind(key, localDate.toString)
    }
} 
开发者ID:ovotech,项目名称:comms-audit-log,代码行数:29,代码来源:Binders.scala

示例5: write

//设置package包名称以及导入依赖的类
package aia.stream

import java.time.ZonedDateTime
import java.time.format.{ DateTimeFormatter, DateTimeParseException }

import scala.util.Try
import spray.json._

trait EventMarshalling extends DefaultJsonProtocol {
  implicit val dateTimeFormat = new JsonFormat[ZonedDateTime] {
    def write(dateTime: ZonedDateTime) = JsString(dateTime.format(DateTimeFormatter.ISO_INSTANT))
    def read(value: JsValue) = value match {
      case JsString(str) => 
        try {
          ZonedDateTime.parse(str)
        } catch {
          case e: DateTimeParseException => 
            val msg = s"Could not deserialize $str to ZonedDateTime"
            deserializationError(msg)
        }
      case js => 
        val msg = s"Could not deserialize $js to ZonedDateTime."
        deserializationError(msg)
    }
  }

  implicit val stateFormat = new JsonFormat[State] {
    def write(state: State) = JsString(State.norm(state))
    def read(value: JsValue) = value match {
      case JsString("ok") => Ok
      case JsString("warning") => Warning
      case JsString("error") => Error
      case JsString("critical") => Critical
      case js => 
        val msg = s"Could not deserialize $js to State."
        deserializationError(msg)
    }
  }

  implicit val eventFormat = jsonFormat7(Event)
  implicit val logIdFormat = jsonFormat2(LogReceipt)
  implicit val errorFormat = jsonFormat2(ParseError)
} 
开发者ID:gilbutITbook,项目名称:006877,代码行数:44,代码来源:EventMarshalling.scala


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