1 package gr.grnet.aquarium.util.date
3 import org.joda.time.{MutableDateTime, DateMidnight}
4 import java.util.{Date, Calendar}
5 import java.text.DateFormat
11 * Utility class for date manipulations. Not that this is mutable.
13 * @author Christos KK Loverdos <loverdos@gmail.com>
16 class DateCalculator private(private[this] var dateTime: MutableDateTime) extends Cloneable {
17 def this(millis: Long) = this(new MutableDateTime(millis))
18 def this(date: Date) = this(new MutableDateTime(date))
19 def this(cal: Calendar) = this(new MutableDateTime(cal))
21 def this(year: Int, monthOfYear: Int, dayOfMonth: Int) =
22 this(new MutableDateTime(year, monthOfYear, dayOfMonth, 0, 0, 0, 0))
24 def this(year: Int, monthOfYear: Int) =
25 this(year, monthOfYear, 1)
28 override def clone(): DateCalculator = new DateCalculator(this.dateTime)
30 def copy: DateCalculator = clone()
32 def plusMonths(n: Int): this.type = {
38 def minusMonths(n: Int): this.type = {
39 dateTime.addMonths(-n)
44 def nextMonth: this.type = {
48 def previousMonth: this.type = {
52 def plusDays(n: Int): this.type = {
57 def minusDays(n: Int): this.type = {
62 def nextDay: this.type = {
66 def previousDay: this.type = {
70 def plusSeconds(n: Int): this.type = {
71 dateTime.addSeconds(n)
75 def minusSeconds(n: Int): this.type = {
79 def plusHours(n: Int): this.type = {
84 def minusHours(n: Int): this.type = {
89 def plusMillis(n: Long): this.type = {
94 def minusMillis(n: Long): this.type = {
98 def nextMilli: this.type = {
102 def previousMilli: this.type = {
111 * Months range from 1 to 12.
113 def monthOfYear: Int = {
114 dateTime.getMonthOfYear
118 * Month days start from 1
120 def dayOfMonth: Int = {
121 dateTime.getDayOfMonth
125 * Year days start from 1
127 def dayOfYear: Int = {
128 dateTime.getDayOfYear
132 * Week days start from 1, which is Sunday
134 def dayOfWeek: Int = {
135 dateTime.getDayOfWeek
138 def midnight: this.type = {
139 this.dateTime = new DateMidnight(dateTime).toMutableDateTime
144 * At the first millisecond of this month. This month is the month indicated by the
145 * state of the [[gr.grnet.aquarium.util.date.DateCalculator]] and not the real-life month.
147 def startOfThisMonth: this.type = {
149 while(dayOfMonth > 1) {
156 * At the first millisecond of the next month.
158 def startOfNextMonth: this.type = {
159 nextMonth.startOfThisMonth
163 * At the last millisecond of the month
165 def endOfThisMonth: this.type = {
166 startOfNextMonth.previousMilli
169 def isSameYearAndMonthAs(other: Long): Boolean = {
170 isSameYearAndMonthAs(new DateCalculator(other))
173 def isSameYearAndMonthAs(otherDate: DateCalculator): Boolean = {
174 this.year == otherDate.year && this.monthOfYear == otherDate.monthOfYear
177 def toMillis: Long = {
185 def beforeMillis(millis: Long): Boolean = {
189 def afterMillis(millis: Long): Boolean = {
193 def beforeEqMillis(millis: Long): Boolean = {
197 def afterEqMillis(millis: Long): Boolean = {
201 def format(fmt: String) = {
202 dateTime.formatted(fmt)
205 override def toString = {
206 dateTime.toString("yyyy-MM-dd HH:mm:ss.SSS")