scala와 jodatime 예제이다. 사실 상 자바이긴 한데..



build.sbt 파일에 의존성 라이브러리를 추가한다.

libraryDependencies ++= Seq(

  "joda-time" % "joda-time" % "2.3",

  "org.joda" % "joda-convert" % "1.6"

)




간단하게 오늘 시간 정보를 출력한다.

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

val dateTime = new DateTime()
val dateString = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").print(dateTime)
print(dateString)

결과는 다음과 같다.


2017-11-20 19:37:02





import org.joda.time.{DateTime, DateTimeZone}
import org.joda.time.format._
val dateTime = new DateTime()
val dateString = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss") .print(dateTime.withZone(DateTimeZone.UTC))
print(dateString)


출력은 다음과 같다.


2017-11-20 10:39:24





ISO8601 문자열을 테스트하는 예제이다.



import org.joda.time.{DateTime}
import org.joda.time.format._
val dt = ISODateTimeFormat.dateTimeNoMillis().print(new DateTime())
print(dt)


결과는 다음과 같다. ISO8601 스펙대로 출력한다.


2017-11-20T19:56:28+09:00





다음은 ISO8601 utc 타임이다. 

import org.joda.time.{DateTime, DateTimeZone}
import org.joda.time.format._
val dt = ISODateTimeFormat.dateTimeNoMillis().print(
    new DateTime().withZone(DateTimeZone.UTC))
print(dt)


결과는 다음과 같다. ISO8601 utc 스펙대로 출력한다.


2017-11-20T10:57:34Z








Posted by '김용환'
,