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
'scala' 카테고리의 다른 글
[scala] elastic4s의 json 쿼리 요청 보기 (0) | 2017.11.21 |
---|---|
[scala] connection pool과 with 메소드로 간결하게 코딩하기 (0) | 2017.11.21 |
scala REPL에서 main 클래스/함수 실행시키기 (0) | 2017.11.17 |
스칼라의 접근 한정자 (0) | 2017.11.07 |
lazy val의 내부(volatile/synchronized) (0) | 2017.11.06 |