Created
April 11, 2014 11:16
-
-
Save kamiyaowl/10459452 to your computer and use it in GitHub Desktop.
java.util.Date : add +/-
This file contains 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
100[ms] | |
3900000[ms] | |
1000[ms] |
This file contains 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
import scala.language.postfixOps | |
import java.util.{Calendar, Date} | |
package kamiya { | |
object Utils { | |
class TimeSpan(var millisecond:Long) { | |
//getter | |
def day = hour / 24 | |
def hour = minute / 60 | |
def minute = second / 60 | |
def second = millisecond / 1000 | |
//setter | |
def day_= (d:Int) { millisecond = d * 24 * 60 * 60 * 1000 } | |
def hour_= (h:Int) { millisecond = h * 60 * 60 * 1000 } | |
def minute_= (m:Int) { millisecond = m * 60 * 1000 } | |
def second_= (s:Int) { millisecond = s * 1000 } | |
def -(t:TimeSpan) : TimeSpan = new TimeSpan(millisecond - t.millisecond) | |
def +(t:TimeSpan) : TimeSpan = new TimeSpan(millisecond + t.millisecond) | |
override def toString = millisecond + "[ms]" | |
//constructor | |
def this() = this(0) | |
def this(c:Calendar) = this(c.getTimeInMillis) | |
def this(d:Date) = this(d.getTime) | |
} | |
implicit class CalendarExtension(val self:Calendar) { | |
def -(d:Calendar) : TimeSpan = new TimeSpan(self.getTimeInMillis - d.getTimeInMillis) | |
def +(d:Calendar) : TimeSpan = new TimeSpan(self.getTimeInMillis + d.getTimeInMillis) | |
} | |
implicit class RichDateExtension(val self:Date) { | |
def -(d:Date) : TimeSpan = new TimeSpan(self.getTime - d.getTime) | |
def +(d:Date) : TimeSpan = new TimeSpan(self.getTime + d.getTime) | |
} | |
implicit class TimeSpanCall(val self:Int) { | |
def day = new TimeSpan{ day = self } | |
def hour = new TimeSpan{ hour = self } | |
def minute = new TimeSpan{ minute = self } | |
def second = new TimeSpan{ second = self } | |
def millisecond = new TimeSpan(self) | |
val d = day | |
val h = hour | |
val m = minute | |
val s = second | |
val ms = millisecond | |
} | |
} | |
} |
This file contains 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
import scala.language.postfixOps | |
import kamiya.Utils._ | |
import java.util.{Date, Calendar} | |
object TimeSpanTest { | |
def main(args:Array[String]) = { | |
println(100 ms) | |
println(65 minute) | |
val d = new Date | |
Thread.sleep(1000) | |
println(new Date - d) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment