스칼라에서 날짜와 시간으로 작업하는 표준 방법은 무엇입니까? Java 유형을 사용해야합니까, 아니면 기본 스칼라 대안이 있습니까?
스칼라에서 날짜와 시간으로 작업하는 표준 방법은 무엇입니까? java.util.Date와 같은 Java 유형을 사용해야합니까, 아니면 기본 스칼라 대안이 있습니까?
Java SE 8부터는 java.time (JSR-310)으로 마이그레이션하라는 메시지가 표시됩니다. scala-time 과 같은 scala를 위해 java.time을 랩핑하는 스칼라 라이브러리를 작성하려는 노력이 있습니다 . SE 8보다 낮은 타겟팅은 다음 중 하나를 사용하십시오. JSR-310이 Joda-Time이 아닌 이유 도 참조하십시오
Joda Time을위한 새로운 스칼라 포장지 . 이 프로젝트는 scala-time이 더 이상 유지되지 않는 것 같아 scala-time에서 분기되었습니다.
import com.github.nscala_time.time.Imports._
DateTime.now // returns org.joda.time.DateTime = 2009-04-27T13:25:42.659-07:00
DateTime.now.hour(2).minute(45).second(10) // returns org.joda.time.DateTime = 2009-04-27T02:45:10.313-07:00
DateTime.now + 2.months // returns org.joda.time.DateTime = 2009-06-27T13:25:59.195-07:00
DateTime.nextMonth < DateTime.now + 2.months // returns Boolean = true
DateTime.now to DateTime.tomorrow // return org.joda.time.Interval = > 2009-04-27T13:47:14.840/2009-04-28T13:47:14.840
(DateTime.now to DateTime.nextSecond).millis // returns Long = 1000
2.hours + 45.minutes + 10.seconds
// returns com.github.nscala_time.time.DurationBuilder
// (can be used as a Duration or as a Period)
(2.hours + 45.minutes + 10.seconds).millis
// returns Long = 9910000
2.months + 3.days
// returns Period
Joda Time 은 훌륭한 Java 라이브러리이며 Jorge Ortiz가 만든 scala-time 에서 Joda Time에 사용 가능한 Scala 래퍼 / 암시 적 변환 라이브러리가 있습니다. (참고 암시 적에는 성능에 영향이 있지만, 눈치 채면 수행하는 작업에 따라 다릅니다. 성능 문제가 발생하면 Joda 인터페이스로 되돌릴 수 있습니다)
읽어보기에서 :
USAGE:
import org.scala_tools.time.Imports._
DateTime.now
// returns org.joda.time.DateTime = 2009-04-27T13:25:42.659-07:00
DateTime.now.hour(2).minute(45).second(10)
// returns org.joda.time.DateTime = 2009-04-27T02:45:10.313-07:00
DateTime.now + 2.months
// returns org.joda.time.DateTime = 2009-06-27T13:25:59.195-07:00
DateTime.nextMonth < DateTime.now + 2.months
// returns Boolean = true
DateTime.now to DateTime.tomorrow
// return org.joda.time.Interval =
// 2009-04-27T13:47:14.840/2009-04-28T13:47:14.840
(DateTime.now to DateTime.nextSecond).millis
// returns Long = 1000
2.hours + 45.minutes + 10.seconds
// returns org.scala_tools.time.DurationBuilder
// (can be used as a Duration or as a Period)
(2.hours + 45.minutes + 10.seconds).millis
// returns Long = 9910000
2.months + 3.days
// returns Period
Java 8을 사용하는 경우 nscala
더 이상 사용할 필요가 없습니다. Joda-Time 라이브러리가 java.time
패키지 (JSR-310) 에서 Java 8로 이동되었습니다 . 해당 패키지를 Scala 프로젝트로 가져 오기만하면됩니다.
스칼라에서는 날짜를 처리하는 표준 방법 이 없습니다 . 사용 가능한 옵션은 다음과 같습니다.
- JODA 시간이 가장 우수하므로 java.time (Java 8을 사용하는 경우)을 사용하십시오. 암시 적 없음.
- Use nscala-time.
- Lamma date library (relatively new library on the scene)
I would avoid using java.util.Date due to the well-documented issues surrounding it.
MOTIVATION:
The Java Date and Calendar libraries are largely inadequate. They are mutable, not thread-safe, and very inconvenient to use.
The Joda Time library is a great replacement for Java's Date and Calendar classes. They're immutable by default, have a much richer and nicer API, and can easily be converted to Java's Date and Calendar classes when necessary.
This project provides a thin layer of convenience around the Joda Time libraries, making them more idiomatic to use within Scala.
(copied from https://github.com/jorgeortiz85/scala-time)
Everyone uses JodaTime, these Scala helper/wrapper libraries may need re-compilation with new versions of Scala. Jodatime is the only time library that's been around for a long time, and is stable and works reliably with every version of Scala.
'IT story' 카테고리의 다른 글
Git 병합 커밋 재조정 (0) | 2020.06.05 |
---|---|
Dropzone.js를 다른 필드와 함께 기존 HTML 양식에 통합 (0) | 2020.06.05 |
virtualenv와 pyenv의 관계는 무엇입니까? (0) | 2020.06.05 |
SQL Server에서 테이블이 존재하지 않으면 테이블 만들기 (0) | 2020.06.05 |
JavaScript에서 '정의'는 무엇입니까? (0) | 2020.06.05 |