8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Scala: joda-timeで日付をISO-8601形式にフォーマットしたい

Last updated at Posted at 2014-06-29

Scalaでjoda-timeライブラリを用いて、現在日時を[ISO-8601]形式で整形する方法。

import org.joda.time.DateTime
import org.joda.time.format._

val parserISO = ISODateTimeFormat.dateTimeNoMillis()
val dateString = parserISO.print(new DateTime())

// dateString: String = 2014-06-30T07:13:33+09:00

上の例では実行環境のタイムゾーンになる。UTCでフォーマットしたい場合は、タイムゾーンをUTCに指定したDateTimeオブジェクトを渡す。

import org.joda.time.DateTime
import org.joda.time.format._
import org.joda.time.DateTimeZone

val parserISO = ISODateTimeFormat.dateTimeNoMillis()
val dateString = parserISO.print(new DateTime().withZone(DateTimeZone.UTC))

なお、joda-timeを使うためには、build.sbt の libraryDependenciesjoda-timejoda-convert を書いておく。

build.sbt
libraryDependencies ++= Seq(
  "joda-time" % "joda-time" % "2.3",
  "org.joda" % "joda-convert" % "1.6"
)

関連

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?