mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-15 14:51:04 +00:00
Fix time parse error, that ExceptionInInitializerError
use https://github.com/dlew/joda-time-android library
This commit is contained in:
@@ -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'
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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() {
|
||||
|
Reference in New Issue
Block a user