simplified date parsing

This commit is contained in:
Mariotaku Lee 2016-04-05 17:48:03 +08:00
parent 622edb47db
commit 62a1e2653f
2 changed files with 10 additions and 26 deletions

View File

@ -19,41 +19,23 @@
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.ParseException;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
/**
* Created by mariotaku on 15/5/7.
*/
public class TwitterDateConverter extends StringBasedTypeConverter<Date> {
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);
}
public class TwitterDateConverter extends DateTypeConverter {
@Override
public Date getFromString(final String string) {
if (string == null) return null;
try {
return sFormat.parse(string);
} catch (ParseException e) {
return null;
}
public DateFormat getDateFormat() {
final DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy", Locale.ENGLISH);
format.setLenient(true);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
return format;
}
@Override
public String convertToString(final Date date) {
if (date == null) return null;
return sFormat.format(date);
}
}

View File

@ -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_2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH);
@Override
public Date parse(JsonParser jsonParser) throws IOException {
String dateString = jsonParser.getValueAsString(null);
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) {
throw new UnsupportedOperationException();
}