Use GregorianCalendar instead of Joda

pull/322/head
Alex Baker 9 years ago
parent bb5c1496ef
commit ddfee16bde

@ -92,7 +92,6 @@ dependencies {
compile group: 'com.android.support', name: 'design', version: '22.2.1' compile group: 'com.android.support', name: 'design', version: '22.2.1'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.7' compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.7'
compile group: 'joda-time', name: 'joda-time', version: '2.8.1', transitive: false
compile group: 'com.rubiconproject.oss', name: 'jchronic', version: '0.2.6', transitive: false compile group: 'com.rubiconproject.oss', name: 'jchronic', version: '0.2.6', transitive: false
compile group: 'org.scala-saddle', name: 'google-rfc-2445', version: '20110304', transitive: false compile group: 'org.scala-saddle', name: 'google-rfc-2445', version: '20110304', transitive: false

@ -176,6 +176,10 @@ public class DateTimeTest extends AndroidTestCase {
assertEquals( assertEquals(
new DateTime(2015, 11, 6, 16, 19, 16), new DateTime(2015, 11, 6, 16, 19, 16),
new DateTime(2015, 12, 4, 16, 19, 16).minusDays(28)); new DateTime(2015, 12, 4, 16, 19, 16).minusDays(28));
assertEquals(
new DateTime(2015, 11, 6, 16, 19, 16),
new DateTime(2015, 11, 7, 16, 19, 16).minusDays(1));
} }
public void testGetSecondOfMinute() { public void testGetSecondOfMinute() {

@ -1,13 +1,18 @@
package org.tasks.time; package org.tasks.time;
import org.joda.time.DateTimeZone; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public class DateTime { public class DateTime {
private final org.joda.time.DateTime dateTime; private static final int MAX_MILLIS_PER_DAY = (int) TimeUnit.DAYS.toMillis(1);
private final TimeZone timeZone;
private final long timestamp;
public DateTime(int year, int month, int day, int hour, int minute, int second) { public DateTime(int year, int month, int day, int hour, int minute, int second) {
this(year, month, day, hour, minute, second, 0); this(year, month, day, hour, minute, second, 0);
@ -18,7 +23,11 @@ public class DateTime {
} }
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeZone timeZone) { public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeZone timeZone) {
this(new org.joda.time.DateTime(year, month, day, hour, minute, second, millisecond, DateTimeZone.forTimeZone(timeZone))); GregorianCalendar gregorianCalendar = new GregorianCalendar(timeZone);
gregorianCalendar.set(year, month - 1, day, hour, minute, second);
gregorianCalendar.set(Calendar.MILLISECOND, millisecond);
timestamp = gregorianCalendar.getTimeInMillis();
this.timeZone = timeZone;
} }
public static DateTime now() { public static DateTime now() {
@ -29,8 +38,8 @@ public class DateTime {
this(DateTimeUtils.currentTimeMillis()); this(DateTimeUtils.currentTimeMillis());
} }
public DateTime(org.joda.time.DateTime dateTime) { public DateTime(Date d) {
this.dateTime = dateTime; this(d.getTime());
} }
public DateTime(long timestamp) { public DateTime(long timestamp) {
@ -38,135 +47,170 @@ public class DateTime {
} }
public DateTime(long timestamp, TimeZone timeZone) { public DateTime(long timestamp, TimeZone timeZone) {
dateTime = new org.joda.time.DateTime(timestamp, DateTimeZone.forTimeZone(timeZone)); this.timestamp = timestamp;
this.timeZone = timeZone;
} }
public DateTime(Date d) { private DateTime(Calendar calendar) {
dateTime = new org.joda.time.DateTime(d, DateTimeZone.forTimeZone(TimeZone.getDefault())); this(calendar.getTimeInMillis(), calendar.getTimeZone());
}
public DateTime startOfDay() {
return withHourOfDay(0)
.withMinuteOfHour(0)
.withSecondOfMinute(0)
.withMillisOfSecond(0);
} }
public DateTime withMillisOfDay(int millisOfDay) { public DateTime withMillisOfDay(int millisOfDay) {
return new DateTime(dateTime.withMillisOfDay(millisOfDay)); if (millisOfDay >= MAX_MILLIS_PER_DAY) {
throw new RuntimeException("Illegal millis of day: " + millisOfDay);
}
return new DateTime(startOfDay().getMillis() + millisOfDay, timeZone);
} }
public long getMillis() { public long getMillis() {
return dateTime.getMillis(); return timestamp;
} }
public DateTime plusMonths(int interval) { public DateTime plusMonths(int interval) {
return new DateTime(dateTime.plusMonths(interval)); Calendar calendar = getCalendar();
calendar.add(Calendar.MONTH, interval);
return new DateTime(calendar);
} }
public int getDayOfMonth() { public int getDayOfMonth() {
return dateTime.getDayOfMonth(); return getCalendar().get(Calendar.DATE);
} }
public DateTime plusDays(int interval) { public DateTime plusDays(int interval) {
return new DateTime(dateTime.plusDays(interval)); return add(Calendar.DATE, interval);
} }
public int getMinuteOfHour() { public int getMinuteOfHour() {
return dateTime.getMinuteOfHour(); return getCalendar().get(Calendar.MINUTE);
} }
public String toString(String format) { @Deprecated public Date toDate() {
return dateTime.toString(format); return new Date(timestamp);
}
public Date toDate() {
return dateTime.toDate();
} }
public int getNumberOfDaysInMonth() { public int getNumberOfDaysInMonth() {
return dateTime.dayOfMonth().getMaximumValue(); return getCalendar().getActualMaximum(Calendar.DAY_OF_MONTH);
} }
public DateTime withMillisOfSecond(int millisOfSecond) { public DateTime withMillisOfSecond(int millisOfSecond) {
return new DateTime(dateTime.withMillisOfSecond(millisOfSecond)); return with(Calendar.MILLISECOND, millisOfSecond);
} }
public DateTime withHourOfDay(int hourOfDay) { public DateTime withHourOfDay(int hourOfDay) {
return new DateTime(dateTime.withHourOfDay(hourOfDay)); return with(Calendar.HOUR_OF_DAY, hourOfDay);
} }
public DateTime withMinuteOfHour(int minuteOfHour) { public DateTime withMinuteOfHour(int minuteOfHour) {
return new DateTime(dateTime.withMinuteOfHour(minuteOfHour)); return with(Calendar.MINUTE, minuteOfHour);
} }
public DateTime withSecondOfMinute(int secondOfMinute) { public DateTime withSecondOfMinute(int secondOfMinute) {
return new DateTime(dateTime.withSecondOfMinute(secondOfMinute)); return with(Calendar.SECOND, secondOfMinute);
} }
public int getYear() { public int getYear() {
return dateTime.getYear(); return getCalendar().get(Calendar.YEAR);
} }
public DateTime minusMinutes(int minutes) { public DateTime minusMinutes(int minutes) {
return new DateTime(dateTime.minusMinutes(minutes)); return subtract(Calendar.MINUTE, minutes);
} }
public boolean isBefore(DateTime dateTime) { public boolean isBefore(DateTime dateTime) {
return this.dateTime.isBefore(dateTime.dateTime); return timestamp < dateTime.getMillis();
} }
public int getMillisOfDay() { public int getMillisOfDay() {
return dateTime.getMillisOfDay(); return (int) (timestamp - withMillisOfDay(0).getMillis());
} }
public int getMonthOfYear() { public int getMonthOfYear() {
return dateTime.getMonthOfYear(); return getCalendar().get(Calendar.MONTH) + 1;
} }
public boolean isAfter(DateTime dateTime) { public boolean isAfter(DateTime dateTime) {
return this.dateTime.isAfter(dateTime.dateTime); return timestamp > dateTime.getMillis();
} }
public DateTime withYear(int year) { public DateTime withYear(int year) {
return new DateTime(dateTime.withYear(year)); return with(Calendar.YEAR, year);
} }
public DateTime withMonthOfYear(int monthOfYear) { public DateTime withMonthOfYear(int monthOfYear) {
return new DateTime(dateTime.withMonthOfYear(monthOfYear)); return with(Calendar.MONTH, monthOfYear - 1);
} }
public int getHourOfDay() { public int getHourOfDay() {
return dateTime.getHourOfDay(); return getCalendar().get(Calendar.HOUR_OF_DAY);
} }
public DateTime withDayOfMonth(int dayOfMonth) { public DateTime withDayOfMonth(int dayOfMonth) {
return new DateTime(dateTime.withDayOfMonth(dayOfMonth)); return with(Calendar.DAY_OF_MONTH, dayOfMonth);
} }
public DateTime plusMinutes(int minutes) { public DateTime plusMinutes(int minutes) {
return new DateTime(dateTime.plusMinutes(minutes)); return add(Calendar.MINUTE, minutes);
} }
public DateTime plusHours(int hours) { public DateTime plusHours(int hours) {
return new DateTime(dateTime.plusHours(hours)); return add(Calendar.HOUR_OF_DAY, hours);
} }
public DateTime plusWeeks(int weeks) { public DateTime plusWeeks(int weeks) {
return new DateTime(dateTime.plusWeeks(weeks)); return add(Calendar.WEEK_OF_MONTH, weeks);
} }
public boolean isBeforeNow() { public boolean isBeforeNow() {
return dateTime.isBefore(DateTimeUtils.currentTimeMillis()); return timestamp < DateTimeUtils.currentTimeMillis();
} }
public DateTime minusMillis(int millis) { public DateTime minusMillis(int millis) {
return new DateTime(dateTime.minusMillis(millis)); return subtract(Calendar.MILLISECOND, millis);
} }
public DateTime minusDays(int days) { public DateTime minusDays(int days) {
return new DateTime(dateTime.minusDays(days)); return subtract(Calendar.DATE, days);
} }
public DateTime toUTC() { public DateTime toUTC() {
return new DateTime(dateTime.toDateTime(DateTimeZone.UTC)); Calendar local = getCalendar();
Calendar utc = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
utc.setTimeInMillis(local.getTimeInMillis());
return new DateTime(utc);
} }
public int getSecondOfMinute() { public int getSecondOfMinute() {
return dateTime.getSecondOfMinute(); return getCalendar().get(Calendar.SECOND);
}
private DateTime with(int field, int value) {
Calendar calendar = getCalendar();
calendar.set(field, value);
return new DateTime(calendar);
}
private DateTime subtract(int field, int value) {
return add(field, -value);
}
private DateTime add(int field, int value) {
Calendar calendar = getCalendar();
calendar.add(field, value);
return new DateTime(calendar);
}
private Calendar getCalendar() {
Calendar calendar = new GregorianCalendar(timeZone);
calendar.setTimeInMillis(timestamp);
return calendar;
} }
@Override @Override
@ -174,19 +218,29 @@ public class DateTime {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
DateTime dateTime1 = (DateTime) o; DateTime dateTime = (DateTime) o;
return !(dateTime != null ? !dateTime.equals(dateTime1.dateTime) : dateTime1.dateTime != null); if (timestamp != dateTime.timestamp) return false;
return !(timeZone != null ? !timeZone.equals(dateTime.timeZone) : dateTime.timeZone != null);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return dateTime != null ? dateTime.hashCode() : 0; int result = timeZone != null ? timeZone.hashCode() : 0;
result = 31 * result + (int) (timestamp ^ (timestamp >>> 32));
return result;
}
public String toString(String format) {
Calendar calendar = getCalendar();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
simpleDateFormat.setCalendar(calendar);
return simpleDateFormat.format(calendar.getTime());
} }
@Override @Override
public String toString() { public String toString() {
return dateTime.toString(); return toString("yyyy-MM-dd HH:mm:ss.SSSZ");
} }
} }

Loading…
Cancel
Save