simplified date parsing
This commit is contained in:
parent
622edb47db
commit
62a1e2653f
|
@ -19,41 +19,23 @@
|
||||||
|
|
||||||
package org.mariotaku.twidere.api.twitter.util;
|
package org.mariotaku.twidere.api.twitter.util;
|
||||||
|
|
||||||
import com.bluelinelabs.logansquare.typeconverters.StringBasedTypeConverter;
|
import com.bluelinelabs.logansquare.typeconverters.DateTypeConverter;
|
||||||
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mariotaku on 15/5/7.
|
* Created by mariotaku on 15/5/7.
|
||||||
*/
|
*/
|
||||||
public class TwitterDateConverter extends StringBasedTypeConverter<Date> {
|
public class TwitterDateConverter extends DateTypeConverter {
|
||||||
|
|
||||||
static final DateFormat sFormat = new ThreadLocalSimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy",
|
|
||||||
Locale.ENGLISH);
|
|
||||||
|
|
||||||
static {
|
|
||||||
sFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
||||||
sFormat.setLenient(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Date getFromString(final String string) {
|
public DateFormat getDateFormat() {
|
||||||
if (string == null) return null;
|
final DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy", Locale.ENGLISH);
|
||||||
try {
|
format.setLenient(true);
|
||||||
return sFormat.parse(string);
|
format.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
} catch (ParseException e) {
|
return format;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String convertToString(final Date date) {
|
|
||||||
if (date == null) return null;
|
|
||||||
return sFormat.format(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ public class TwitterTrendsDateConverter implements TypeConverter<Date> {
|
||||||
private static final SimpleDateFormat DATE_FORMAT_1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
|
private static final SimpleDateFormat DATE_FORMAT_1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
|
||||||
private static final SimpleDateFormat DATE_FORMAT_2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
|
private static final SimpleDateFormat DATE_FORMAT_2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
|
||||||
|
|
||||||
|
@Override
|
||||||
public Date parse(JsonParser jsonParser) throws IOException {
|
public Date parse(JsonParser jsonParser) throws IOException {
|
||||||
String dateString = jsonParser.getValueAsString(null);
|
String dateString = jsonParser.getValueAsString(null);
|
||||||
if (dateString == null) throw new IOException();
|
if (dateString == null) throw new IOException();
|
||||||
|
@ -35,6 +36,7 @@ public class TwitterTrendsDateConverter implements TypeConverter<Date> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void serialize(Date object, String fieldName, boolean writeFieldNameForObject, JsonGenerator jsonGenerator) {
|
public void serialize(Date object, String fieldName, boolean writeFieldNameForObject, JsonGenerator jsonGenerator) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue