mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-16 07:19:28 +00:00
add TimeFormatTest; improve time parser
This commit is contained in:
@@ -32,6 +32,16 @@ public class TimeUtils {
|
|||||||
return modified;
|
return modified;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
serverTime = StringUtils.replace(serverTime,
|
||||||
|
"\\+\\d+:\\d+",
|
||||||
|
"\\d+:\\d+",
|
||||||
|
new StringUtils.Replacer() {
|
||||||
|
@Override
|
||||||
|
public String replaceWith(String original, Object... extraData) {
|
||||||
|
String[] vals = original.split(":");
|
||||||
|
return String.format(Locale.US, "%02d:%02d", Integer.valueOf(vals[0]), Integer.valueOf(vals[1]));
|
||||||
|
}
|
||||||
|
});
|
||||||
Date date = mServerWithMillsFormat.parse(serverTime);
|
Date date = mServerWithMillsFormat.parse(serverTime);
|
||||||
return date.getTime();
|
return date.getTime();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
|
42
app/src/test/java/org/houxg/leamonax/TimeFormatTest.java
Normal file
42
app/src/test/java/org/houxg/leamonax/TimeFormatTest.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package org.houxg.leamonax;
|
||||||
|
|
||||||
|
|
||||||
|
import org.houxg.leamonax.utils.TimeUtils;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TimeFormatTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCompose() throws Exception {
|
||||||
|
long time = 1480040991786l;
|
||||||
|
String expectTime = "2016-11-25T10:29:51.786+08:00";
|
||||||
|
Assert.assertEquals(expectTime, TimeUtils.toServerTime(time));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParse() throws Exception {
|
||||||
|
String[] formatCases = new String[]{
|
||||||
|
"2016-11-25T02:29:51.861+08:00",
|
||||||
|
"2016-11-25T02:29:51.861+8:00",
|
||||||
|
"2016-11-25T02:29:51.861+8:0",
|
||||||
|
"2016-11-25T02:29:51.8612348761+08:00",
|
||||||
|
"2016-11-25T02:29:51.8612348761+8:00",
|
||||||
|
"2016-11-25T02:29:51.8612348761+8:0",
|
||||||
|
};
|
||||||
|
long expectTime = 1480012191861l;
|
||||||
|
for (String format : formatCases) {
|
||||||
|
Assert.assertEquals("test " + format, expectTime, TimeUtils.toTimestamp(format));
|
||||||
|
}
|
||||||
|
|
||||||
|
formatCases = new String[] {
|
||||||
|
"2016-11-25T02:29:51+08:00",
|
||||||
|
"2016-11-25T02:29:51+8:00",
|
||||||
|
"2016-11-25T02:29:51+8:0"
|
||||||
|
};
|
||||||
|
expectTime = 1480012191000l;
|
||||||
|
for (String format : formatCases) {
|
||||||
|
Assert.assertEquals("test " + format, expectTime, TimeUtils.toTimestamp(format));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user