繁体   English   中英

在Android Studio中,Java.text.DateFormat出现错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM