DateTime cleanup

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

@ -7,7 +7,6 @@ import org.tasks.time.DateTime;
import java.util.Locale;
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.thaw;
@ -30,33 +29,33 @@ public class RelativeDayTest extends AndroidTestCase {
}
public void testRelativeDayIsToday() {
checkRelativeDay(now(), "Today", "Today");
checkRelativeDay(new DateTime(), "Today", "Today");
}
public void testRelativeDayIsTomorrow() {
checkRelativeDay(now().plusDays(1), "Tomorrow", "Tmrw");
checkRelativeDay(new DateTime().plusDays(1), "Tomorrow", "Tmrw");
}
public void testRelativeDayIsYesterday() {
checkRelativeDay(now().minusDays(1), "Yesterday", "Yest");
checkRelativeDay(new DateTime().minusDays(1), "Yesterday", "Yest");
}
public void testRelativeDayTwo() {
checkRelativeDay(now().minusDays(2), "Sunday", "Sun");
checkRelativeDay(now().plusDays(2), "Thursday", "Thu");
checkRelativeDay(new DateTime().minusDays(2), "Sunday", "Sun");
checkRelativeDay(new DateTime().plusDays(2), "Thursday", "Thu");
}
public void testRelativeDaySix() {
checkRelativeDay(now().minusDays(6), "Wednesday", "Wed");
checkRelativeDay(now().plusDays(6), "Monday", "Mon");
checkRelativeDay(new DateTime().minusDays(6), "Wednesday", "Wed");
checkRelativeDay(new DateTime().plusDays(6), "Monday", "Mon");
}
public void testRelativeDayOneWeek() {
checkRelativeDay(now().minusDays(7), "Dec 24", "Dec 24");
checkRelativeDay(new DateTime().minusDays(7), "Dec 24", "Dec 24");
}
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) {

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