add TimeFormatTest; improve time parser

This commit is contained in:
houxg
2016-11-25 11:10:19 +08:00
parent 6a2c587dd7
commit fe1e6360e1
2 changed files with 52 additions and 0 deletions

View File

@@ -32,6 +32,16 @@ public class TimeUtils {
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);
return date.getTime();
} catch (ParseException e) {

View 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));
}
}
}