Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
OrganizationAdvent CalendarQiitadon (β)
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Scala: joda-timeで日付をyyyy-MM-dd HH:mm:ss形式にフォーマットしたい

More than 5 years have passed since last update.

Scalaでjoda-timeライブラリを用いて、現在日時をyyyy-MM-dd HH:mm:ss形式で整形する方法。

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

val dateTime = new DateTime()
// dateTime: org.joda.time.DateTime = 2014-10-30T09:29:29.899Z

val dateString = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").print(dateTime)
// dateString: String = 2014-10-30 09:28:09


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

val dateTime = new DateTime()
// dateTime: org.joda.time.DateTime = 2014-10-30T09:30:11.634Z

val dateString = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").print(dateTime.withZone(DateTimeZone.UTC))
// dateString: String = 2014-10-30 09:30:11

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

libraryDependencies ++= Seq(
  "joda-time" % "joda-time" % "2.3",
  "org.joda" % "joda-convert" % "1.6"
Qiita 4位/TypeScript入門書執筆中/TypeScripterのための座談会「YYTypeScript」主催/『実践ドメイン駆動設計』書籍邦訳レビュア/分報Slack考案/YYPHP主催/CodeIQマガジン執筆/株式会社クラフトマンソフトウェア創設/Web自動テスト「ShouldBee」の開発/TypeScript/DDD/OOP
「インフラの心配は、もうおしまい」 インフラ運用を自動化し、手作業を限りなくゼロにする会社
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away