[英]In Android Studio I am getting errors with Java.text.DateFormat
我正在嘗試更改以下代碼,以便在Android環境中更好地用作即將到來的API的一部分。
public class DateFormatter implement JsonDeserializer<Date>,
JsonSerializer<Date> {
private final DateFormat[] formats;
public DateFormatter() {
formats = new DateFormat[3];
formats[0] = new SimpleDateFormat(DATE_FORMAT);
formats[1] = new SimpleDateFormat(DATE_FORMAT_V2_1);
formats[2] = new SimpleDateFormat(DATE_FORMAT_V2_2);
final TimeZone timeZone = TimeZone.getTimeZone("Zulu"); //$NON-NLS-1$
for (DateFormat format : formats)
format.setTimeZone(timeZone);
}
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonParseException exception = null;
final String value = json.getAsString();
for (DateFormat format : formats)
try {
synchronized (format) {
return format.parse(value);
}
} catch (ParseException e) {
exception = new JsonParseException(e);
}
throw exception;
}
public JsonElement serialize(Date date, Type type,
JsonSerializationContext context) {
final DateFormat primary = formats[0];
String formatted;
synchronized (primary) {
formatted = primary.format(date);
}
return new JsonPrimitive(formatted);
}
}
我不需要支持v2.1和v2.2。 因此,我一直在嘗試刪除數組,並僅針對單個實例進行編碼。 我遇到了一些錯誤。
這是我到目前為止的內容:
class DateFormatter implements JsonDeserializer<Date>,
JsonSerializer<Date> {
private DateFormat formats;
DateFormatter() {
formats = new DateFormat;
formats = new SimpleDateFormat(String.valueOf(R.string.date_format), Locale.ENGLISH);
final TimeZone timeZone = TimeZone.getTimeZone("Zulu");
for (DateFormat format : formats)
format.setTimeZone(timeZone);
}
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonParseException exception = null;
final String value;
value = json.getAsString();
for (DateFormat format : formats)
try {
synchronized (format) {
return format.parse(value);
}
} catch (ParseException e) {
exception = new JsonParseException(e);
}
throw exception;
}
public JsonElement serialize(Date date, Type type,
JsonSerializationContext context) {
final DateFormat primary;
primary = formats;
String formatted;
synchronized (primary) {
formatted = primary.format(date);
}
return new JsonPrimitive(formatted);
}
}
但是,一旦我明白了這一點,我就會出錯。 我當前關注的主要對象是getString。
我在這里做錯了什么?
編輯:
@trooper我無法構建項目,所以我無法拉--stacktrace --debug
我更改了帖子中的第二個代碼塊以反映我當前的代碼。 我變了;
formats = new SimpleDateFormat(getString(R.string.date_format), Locale.ENGLISH);
至;
formats = new SimpleDateFormat(String.valueOf(R.string.date_format), Locale.ENGLISH);
這糾正了我的第一個問題。
所以,現在已經回答了,繼續我的下一個問題。 如您所見,我正在從第一個塊中的數組移動到第二個塊中的單個實例。 線
for (DateFormat format : formats)
“格式”拋出“ foreach不適用於java.text.DateFormat”
我知道foreach用於數組中,我不知道如何刪除循環的那一部分並實現我所需要的...這是我真正迷失的地方。
我的最終目標是將支持Javascript v2和v3的Eclipse Studio當前用Java編寫的GitHib APIv3轉換為Android GitHub API v3,這是因為我們不需要介紹v2。
我希望此編輯足以提供答案。
格式未聲明為數組。 您需要將其聲明為數組,然后對其進行初始化。 嘗試這個,
私有的最終DateFormat []格式format = new DateFormat [3]; 格式[0] =新的SimpleDateFormat(getString(R.string.date_format),Locale.ENGLISH);
刪除循環只需使用format = new DateFormat; 格式=新的SimpleDateFormat(String.valueOf(R.string.date_format),Locale.ENGLISH); 最后的TimeZone timeZone = TimeZone.getTimeZone(“ Zulu”); format.setTimeZone(timeZone);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.