use joda-time

This commit is contained in:
Milos Kozak 2017-12-22 02:55:40 +01:00
parent 74abdc0271
commit 31975396a2
4 changed files with 23 additions and 39 deletions

View file

@ -188,4 +188,6 @@ dependencies {
compile 'com.google.code.gson:gson:2.7'
compile 'com.google.guava:guava:20.0'
compile 'net.danlew:android.joda:2.9.9.1'
testCompile 'joda-time:joda-time:2.9.4.2'
}

View file

@ -15,6 +15,8 @@ import com.squareup.otto.Bus;
import com.squareup.otto.LoggingBus;
import com.squareup.otto.ThreadEnforcer;
import net.danlew.android.joda.JodaTimeAndroid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -96,6 +98,7 @@ public class MainApp extends Application {
super.onCreate();
Fabric.with(this, new Crashlytics());
Fabric.with(this, new Answers());
JodaTimeAndroid.init(this);
Crashlytics.setString("BUILDVERSION", BuildConfig.BUILDVERSION);
log.info("Version: " + BuildConfig.VERSION_NAME);
log.info("BuildVersion: " + BuildConfig.BUILDVERSION);

View file

@ -2,10 +2,12 @@ package info.nightscout.utils;
import android.support.v4.util.LongSparseArray;
import android.text.format.DateUtils;
import android.util.SparseIntArray;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@ -29,10 +31,6 @@ public class DateUtil {
*/
private static String FORMAT_DATE_ISO_OUT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static String FORMAT_DATE_ISO = "yyyy-MM-dd'T'HH:mm:ssZ";
private static String FORMAT_DATE_ISO_MSEC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
private static String FORMAT_DATE_ISO_MSEC_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
/**
* Takes in an ISO date string of the following format:
* yyyy-mm-ddThh:mm:ss.ms+HoMo
@ -43,33 +41,10 @@ public class DateUtil {
*/
public static Date fromISODateString(String isoDateString)
throws Exception {
SimpleDateFormat f = new SimpleDateFormat(FORMAT_DATE_ISO, Locale.getDefault());
Date date;
f.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
date = f.parse(isoDateString);
return date;
} catch (ParseException e) {
}
f = new SimpleDateFormat(FORMAT_DATE_ISO_MSEC, Locale.getDefault());
f.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
date = f.parse(isoDateString);
return date;
} catch (ParseException e) {
}
f = new SimpleDateFormat(FORMAT_DATE_ISO_MSEC_UTC, Locale.getDefault());
f.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
date = f.parse(isoDateString);
return date;
} catch (ParseException e) {
}
throw new ParseException("Unparseable date: " + isoDateString, 0);
DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
DateTime dateTime = DateTime.parse(isoDateString, parser);
return dateTime.toDate();
}
/**
@ -99,11 +74,8 @@ public class DateUtil {
public static Date toDate(Integer seconds) {
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.HOUR_OF_DAY, seconds / 60 / 60);
String a = calendar.getTime().toString();
calendar.set(Calendar.MINUTE, (seconds / 60) % 60);
String b = calendar.getTime().toString();
calendar.set(Calendar.SECOND, 0);
String c = calendar.getTime().toString();
return calendar.getTime();
}

View file

@ -3,7 +3,9 @@ package info.nightscout.utils;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.Date;
import static org.junit.Assert.assertEquals;
/**
* Created by mike on 20.11.2017.
@ -17,10 +19,15 @@ public class DateUtilTest {
@Test
public void fromISODateStringTest() throws Exception {
assertEquals( 1511124634417L, DateUtil.fromISODateString("2017-11-19T22:50:34.417+0200").getTime());
assertEquals( 1511124634000L, DateUtil.fromISODateString("2017-11-19T22:50:34+0200").getTime());
assertEquals( 1512317365000L, DateUtil.fromISODateString("2017-12-03T16:09:25.000Z").getTime());
assertEquals(1511124634417L, DateUtil.fromISODateString("2017-11-19T22:50:34.417+0200").getTime());
assertEquals(1511124634000L, DateUtil.fromISODateString("2017-11-19T22:50:34+0200").getTime());
assertEquals(1512317365000L, DateUtil.fromISODateString("2017-12-03T16:09:25.000Z").getTime());
assertEquals(1513902750000L, DateUtil.fromISODateString("2017-12-22T00:32:30Z").getTime());
}
@Test
public void toISOStringTest() throws Exception {
assertEquals("2017-12-22T00:32:30Z", DateUtil.toISOString(new Date(1513902750000L)));
}
}