DateTime cleanup

pull/322/head
Alex Baker 10 years ago
parent 9f3bc5fa84
commit 0387d8dfbd

@ -7,7 +7,6 @@ import org.tasks.time.DateTime;
import java.util.Locale; import java.util.Locale;
import static com.todoroo.andlib.utility.DateUtilities.getRelativeDay; import static com.todoroo.andlib.utility.DateUtilities.getRelativeDay;
import static org.tasks.time.DateTime.now;
import static org.tasks.Freeze.freezeAt; import static org.tasks.Freeze.freezeAt;
import static org.tasks.Freeze.thaw; import static org.tasks.Freeze.thaw;
@ -30,33 +29,33 @@ public class RelativeDayTest extends AndroidTestCase {
} }
public void testRelativeDayIsToday() { public void testRelativeDayIsToday() {
checkRelativeDay(now(), "Today", "Today"); checkRelativeDay(new DateTime(), "Today", "Today");
} }
public void testRelativeDayIsTomorrow() { public void testRelativeDayIsTomorrow() {
checkRelativeDay(now().plusDays(1), "Tomorrow", "Tmrw"); checkRelativeDay(new DateTime().plusDays(1), "Tomorrow", "Tmrw");
} }
public void testRelativeDayIsYesterday() { public void testRelativeDayIsYesterday() {
checkRelativeDay(now().minusDays(1), "Yesterday", "Yest"); checkRelativeDay(new DateTime().minusDays(1), "Yesterday", "Yest");
} }
public void testRelativeDayTwo() { public void testRelativeDayTwo() {
checkRelativeDay(now().minusDays(2), "Sunday", "Sun"); checkRelativeDay(new DateTime().minusDays(2), "Sunday", "Sun");
checkRelativeDay(now().plusDays(2), "Thursday", "Thu"); checkRelativeDay(new DateTime().plusDays(2), "Thursday", "Thu");
} }
public void testRelativeDaySix() { public void testRelativeDaySix() {
checkRelativeDay(now().minusDays(6), "Wednesday", "Wed"); checkRelativeDay(new DateTime().minusDays(6), "Wednesday", "Wed");
checkRelativeDay(now().plusDays(6), "Monday", "Mon"); checkRelativeDay(new DateTime().plusDays(6), "Monday", "Mon");
} }
public void testRelativeDayOneWeek() { public void testRelativeDayOneWeek() {
checkRelativeDay(now().minusDays(7), "Dec 24", "Dec 24"); checkRelativeDay(new DateTime().minusDays(7), "Dec 24", "Dec 24");
} }
public void testRelativeDayOneWeekNextYear() { public void testRelativeDayOneWeekNextYear() {
checkRelativeDay(now().plusDays(7), "Jan 7\n2014", "Jan 7\n2014"); checkRelativeDay(new DateTime().plusDays(7), "Jan 7\n2014", "Jan 7\n2014");
} }
private void checkRelativeDay(DateTime now, String full, String abbreviated) { private void checkRelativeDay(DateTime now, String full, String abbreviated) {

@ -30,10 +30,6 @@ public class DateTime {
this.timeZone = timeZone; this.timeZone = timeZone;
} }
public static DateTime now() {
return new DateTime();
}
public DateTime() { public DateTime() {
this(DateTimeUtils.currentTimeMillis()); this(DateTimeUtils.currentTimeMillis());
} }
@ -70,10 +66,16 @@ public class DateTime {
return timestamp; return timestamp;
} }
public DateTime plusMonths(int interval) { public int getMillisOfDay() {
Calendar calendar = getCalendar(); return (int) (timestamp - withMillisOfDay(0).getMillis());
calendar.add(Calendar.MONTH, interval); }
return new DateTime(calendar);
public int getYear() {
return getCalendar().get(Calendar.YEAR);
}
public int getMonthOfYear() {
return getCalendar().get(Calendar.MONTH) + 1;
} }
public int getDayOfMonth() { public int getDayOfMonth() {
@ -84,24 +86,28 @@ public class DateTime {
return getCalendar().get(Calendar.DAY_OF_WEEK); return getCalendar().get(Calendar.DAY_OF_WEEK);
} }
public DateTime plusDays(int interval) { public int getHourOfDay() {
return add(Calendar.DATE, interval); return getCalendar().get(Calendar.HOUR_OF_DAY);
} }
public int getMinuteOfHour() { public int getMinuteOfHour() {
return getCalendar().get(Calendar.MINUTE); return getCalendar().get(Calendar.MINUTE);
} }
public boolean isLastDayOfMonth() { public int getSecondOfMinute() {
return getDayOfMonth() == getNumberOfDaysInMonth(); return getCalendar().get(Calendar.SECOND);
} }
public int getNumberOfDaysInMonth() { public DateTime withYear(int year) {
return getCalendar().getActualMaximum(Calendar.DAY_OF_MONTH); return with(Calendar.YEAR, year);
} }
public DateTime withMillisOfSecond(int millisOfSecond) { public DateTime withMonthOfYear(int monthOfYear) {
return with(Calendar.MILLISECOND, millisOfSecond); return with(Calendar.MONTH, monthOfYear - 1);
}
public DateTime withDayOfMonth(int dayOfMonth) {
return with(Calendar.DAY_OF_MONTH, dayOfMonth);
} }
public DateTime withHourOfDay(int hourOfDay) { public DateTime withHourOfDay(int hourOfDay) {
@ -116,68 +122,54 @@ public class DateTime {
return with(Calendar.SECOND, secondOfMinute); return with(Calendar.SECOND, secondOfMinute);
} }
public int getYear() { public DateTime withMillisOfSecond(int millisOfSecond) {
return getCalendar().get(Calendar.YEAR); return with(Calendar.MILLISECOND, millisOfSecond);
}
public DateTime minusMinutes(int minutes) {
return subtract(Calendar.MINUTE, minutes);
}
public boolean isBefore(DateTime dateTime) {
return timestamp < dateTime.getMillis();
}
public int getMillisOfDay() {
return (int) (timestamp - withMillisOfDay(0).getMillis());
} }
public int getMonthOfYear() { public DateTime plusMonths(int interval) {
return getCalendar().get(Calendar.MONTH) + 1; Calendar calendar = getCalendar();
calendar.add(Calendar.MONTH, interval);
return new DateTime(calendar);
} }
public boolean isAfter(DateTime dateTime) { public DateTime plusWeeks(int weeks) {
return timestamp > dateTime.getMillis(); return add(Calendar.WEEK_OF_MONTH, weeks);
} }
public DateTime withYear(int year) { public DateTime plusDays(int interval) {
return with(Calendar.YEAR, year); return add(Calendar.DATE, interval);
} }
public DateTime withMonthOfYear(int monthOfYear) { public DateTime plusHours(int hours) {
return with(Calendar.MONTH, monthOfYear - 1); return add(Calendar.HOUR_OF_DAY, hours);
} }
public int getHourOfDay() { public DateTime plusMinutes(int minutes) {
return getCalendar().get(Calendar.HOUR_OF_DAY); return add(Calendar.MINUTE, minutes);
} }
public DateTime withDayOfMonth(int dayOfMonth) { public DateTime minusDays(int days) {
return with(Calendar.DAY_OF_MONTH, dayOfMonth); return subtract(Calendar.DATE, days);
} }
public DateTime plusMinutes(int minutes) { public DateTime minusMinutes(int minutes) {
return add(Calendar.MINUTE, minutes); return subtract(Calendar.MINUTE, minutes);
} }
public DateTime plusHours(int hours) { public DateTime minusMillis(int millis) {
return add(Calendar.HOUR_OF_DAY, hours); return new DateTime(timestamp - millis, timeZone);
} }
public DateTime plusWeeks(int weeks) { public boolean isAfter(DateTime dateTime) {
return add(Calendar.WEEK_OF_MONTH, weeks); return timestamp > dateTime.getMillis();
} }
public boolean isBeforeNow() { public boolean isBeforeNow() {
return timestamp < DateTimeUtils.currentTimeMillis(); return timestamp < DateTimeUtils.currentTimeMillis();
} }
public DateTime minusMillis(int millis) { public boolean isBefore(DateTime dateTime) {
return new DateTime(timestamp - millis, timeZone); return timestamp < dateTime.getMillis();
}
public DateTime minusDays(int days) {
return subtract(Calendar.DATE, days);
} }
public DateTime toUTC() { public DateTime toUTC() {
@ -188,6 +180,14 @@ public class DateTime {
return toTimeZone(TimeZone.getDefault()); return toTimeZone(TimeZone.getDefault());
} }
public boolean isLastDayOfMonth() {
return getDayOfMonth() == getNumberOfDaysInMonth();
}
public int getNumberOfDaysInMonth() {
return getCalendar().getActualMaximum(Calendar.DAY_OF_MONTH);
}
private DateTime toTimeZone(TimeZone timeZone) { private DateTime toTimeZone(TimeZone timeZone) {
Calendar current = getCalendar(); Calendar current = getCalendar();
Calendar target = new GregorianCalendar(timeZone); Calendar target = new GregorianCalendar(timeZone);
@ -195,10 +195,6 @@ public class DateTime {
return new DateTime(target); return new DateTime(target);
} }
public int getSecondOfMinute() {
return getCalendar().get(Calendar.SECOND);
}
private DateTime with(int field, int value) { private DateTime with(int field, int value) {
Calendar calendar = getCalendar(); Calendar calendar = getCalendar();
calendar.set(field, value); calendar.set(field, value);
@ -251,12 +247,4 @@ public class DateTime {
public String toString() { public String toString() {
return toString("yyyy-MM-dd HH:mm:ss.SSSZ"); return toString("yyyy-MM-dd HH:mm:ss.SSSZ");
} }
public int getTimezoneOffset() {
return timeZone.getOffset(timestamp);
}
public DateTime plusMillis(int millis) {
return new DateTime(timestamp + millis, timeZone);
}
} }

Loading…
Cancel
Save