Last active
December 15, 2015 23:09
-
-
Save daiksy/5338015 to your computer and use it in GitHub Desktop.
ScalaでC#っぽい Date ⇔ StringとかDate同士の大小比較とかするためのtrait
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import java.util.Date | |
import java.text.SimpleDateFormat | |
object DateUtil { | |
implicit class StringToDate(self: String) { | |
/** | |
* フォーマット指定された形式でDate型に変換する | |
* | |
* @param format 変換フォーマット | |
* @return | |
*/ | |
def toDate(format: String): Date = { | |
val sdf = new SimpleDateFormat(format) | |
sdf.parse(self) | |
} | |
} | |
implicit class DateToString(self: Date) { | |
/** | |
* フォーマット指定された形式でString型に変換する | |
* | |
* @param format | |
*/ | |
def toString(format: String): String = { | |
val sdf = new SimpleDateFormat(format) | |
sdf.format(self) | |
} | |
} | |
implicit class DateCompare(self: Date) { | |
def >(that: Date): Boolean = { | |
self.after(that) | |
} | |
def >=(that: Date): Boolean = { | |
self.after(that) || self == that | |
} | |
def <(that: Date): Boolean = { | |
self.before(that) | |
} | |
def <=(that: Date): Boolean = { | |
self.before(that) || self == that | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import java.text.SimpleDateFormat | |
import org.specs2.mutable.Specification | |
import util.DateUtil._ | |
class DateUtilSpec extends Specification { | |
val sdf = new SimpleDateFormat("yyyyMMdd") | |
val apr_1st = sdf.parse("20130401") | |
val apr_2nd = sdf.parse("20130402") | |
val apr_3rd = sdf.parse("20130403") | |
"DateUtil" should { | |
"toDate" in { | |
"20130401".toDate("yyyyMMdd") must beEqualTo(sdf.parse("20130401")) | |
} | |
"toDate parse Error" in { | |
"hoge".toDate("yyyyMMdd") must throwA[java.text.ParseException] | |
} | |
"toDate format Error" in { | |
"20130401".toDate("hoge") must throwA[IllegalArgumentException] | |
} | |
"toString" in { | |
apr_1st.toString("yyyyMMdd") must beEqualTo("20130401") | |
} | |
"toString format error" in { | |
apr_1st.toString("hoge") must throwA[IllegalArgumentException] | |
} | |
"Compare > " in { | |
(apr_1st > apr_2nd) must beEqualTo(false) | |
(apr_1st > apr_1st) must beEqualTo(false) | |
(apr_2nd > apr_1st) must beEqualTo(true) | |
} | |
"Compare >= " in { | |
(apr_1st >= apr_2nd) must beEqualTo(false) | |
(apr_2nd >= apr_2nd) must beEqualTo(true) | |
(apr_2nd >= apr_1st) must beEqualTo(true) | |
} | |
"Compare < " in { | |
(apr_2nd < apr_1st) must beEqualTo(false) | |
(apr_2nd < apr_2nd) must beEqualTo(false) | |
(apr_1st < apr_2nd) must beEqualTo(true) | |
} | |
"Compare <= " in { | |
(apr_2nd <= apr_1st) must beEqualTo(false) | |
(apr_1st <= apr_1st) must beEqualTo(true) | |
(apr_1st <= apr_2nd) must beEqualTo(true) | |
} | |
"Compare Range 'true' " in { | |
apr_1st < apr_2nd && | |
apr_2nd < apr_3rd must beEqualTo(true) | |
apr_1st <= apr_2nd && | |
apr_2nd <= apr_3rd must beEqualTo(true) | |
apr_3rd > apr_2nd && | |
apr_2nd > apr_1st must beEqualTo(true) | |
apr_3rd >= apr_2nd && | |
apr_2nd >= apr_1st must beEqualTo(true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment