Fix time parse error, that ExceptionInInitializerError

use https://github.com/dlew/joda-time-android library
This commit is contained in:
xingxing
2016-11-27 13:14:03 +08:00
parent 39ece65ec2
commit 6e1cf1b6c8
3 changed files with 23 additions and 46 deletions

View File

@@ -32,6 +32,11 @@ android {
abortOnError false
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
buildTypes {
release {
minifyEnabled false
@@ -87,4 +92,6 @@ dependencies {
compile 'com.facebook.stetho:stetho:1.4.1'
compile 'com.github.houxg:FlexLayout:1.2'
compile 'com.flurry.android:analytics:6.4.2'
compile 'net.danlew:android.joda:2.9.5'
}

View File

@@ -9,6 +9,8 @@ import com.flurry.android.FlurryAgent;
import com.raizlabs.android.dbflow.config.FlowConfig;
import com.raizlabs.android.dbflow.config.FlowManager;
import net.danlew.android.joda.JodaTimeAndroid;
import org.greenrobot.eventbus.EventBus;
public class Leamonax extends Application {
@@ -33,5 +35,6 @@ public class Leamonax extends Application {
.installDefaultEventBus();
FlowManager.init(new FlowConfig.Builder(this).build());
Stetho.initializeWithDefaults(this);
JodaTimeAndroid.init(this);
}
}

View File

@@ -1,6 +1,13 @@
package org.houxg.leamonax.utils;
import android.text.format.DateUtils;
import android.util.Log;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
@@ -8,66 +15,26 @@ import java.util.Date;
import java.util.Locale;
public class TimeUtils {
private static final SimpleDateFormat mServerWithMillsFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US);
private static final SimpleDateFormat mServerFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.US);
private static final SimpleDateFormat mTimeFormat = new SimpleDateFormat("H:mm:ss", Locale.US);
private static final SimpleDateFormat mDateFormat = new SimpleDateFormat("M-dd H:mm:ss", Locale.US);
private static final SimpleDateFormat mYearFormat = new SimpleDateFormat("yyyy-M-dd H:mm:ss", Locale.US);
public static final String TAG = "TimeUtils";
public static long toTimestamp(String serverTime) {
try {
serverTime = StringUtils.replace(serverTime,
"T\\d{2}:\\d{2}:\\d{2}.\\d+\\+",
"\\.\\d+",
new StringUtils.Replacer() {
@Override
public String replaceWith(String original, Object... extraData) {
String modified;
if (original.length() > 4) {
modified = original.substring(0, 4);
} else {
modified = original;
}
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) {
try {
return mServerFormat.parse(serverTime).getTime();
} catch (ParseException e1) {
e.printStackTrace();
return -1;
}
}
return DateTime.parse(serverTime).getMillis();
}
public static String toServerTime(long timeInMills) {
return mServerWithMillsFormat.format(new Date(timeInMills));
return DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").print(timeInMills);
}
public static String toTimeFormat(long timeInMills) {
return mTimeFormat.format(new Date(timeInMills));
return DateTimeFormat.forPattern("H:mm:ss").print(timeInMills);
}
public static String toDateFormat(long timeInMills) {
return mDateFormat.format(new Date(timeInMills));
return DateTimeFormat.forPattern("M-dd H:mm:ss").print(timeInMills);
}
public static String toYearFormat(long timeInMills) {
return mYearFormat.format(new Date(timeInMills));
return DateTimeFormat.forPattern("yyyy-M-dd H:mm:ss").print(timeInMills);
}
public static Calendar getToday() {