简体   繁体   English

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

[英]In Android Studio I am getting errors with Java.text.DateFormat

I am trying to change the following code, to be better used in an Android environment as part of an upcoming API. 我正在尝试更改以下代码,以便在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);
  }
}

I do not need to support v2.1 and v2.2. 我不需要支持v2.1和v2.2。 so I've been trying to remove the array, and code it for just a single instance. 因此,我一直在尝试删除数组,并仅针对单个实例进行编码。 I'm running into some errors though. 我遇到了一些错误。

Here is what I have so far: 这是我到目前为止的内容:

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);
  }
}

However, once I get it this point, I get errors. 但是,一旦我明白了这一点,我就会出错。 The main one I'm currently concerned about is getString. 我当前关注的主要对象是getString。

What am I doing wrong here? 我在这里做错了什么?

Edit: 编辑:

@trooper I am not able to build the project, so I can't pull a --stacktrace --debug @trooper我无法构建项目,所以我无法拉--stacktrace --debug

I changed the 2nd code block in the post to reflect my current code. 我更改了帖子中的第二个代码块以反映我当前的代码。 I changed; 我变了;

formats = new SimpleDateFormat(getString(R.string.date_format), Locale.ENGLISH); 

to; 至;

formats = new SimpleDateFormat(String.valueOf(R.string.date_format), Locale.ENGLISH);

and that corrected my first question. 这纠正了我的第一个问题。

So, now that is answered, moving on to my next question. 所以,现在已经回答了,继续我的下一个问题。 As you can see I'm moving from an array in the first block to a single instance in the second. 如您所见,我正在从第一个块中的数组移动到第二个块中的单个实例。 the line 线

for (DateFormat format : formats)

the "formats' is throwing a "foreach not applicable to java.text.DateFormat' “格式”抛出“ foreach不适用于java.text.DateFormat”

I know foreach is used in arrays, what I don't know is how to remove that part of the loop and achieve what i need to... this is where I get really lost. 我知道foreach用于数组中,我不知道如何删除循环的那一部分并实现我所需要的...这是我真正迷失的地方。

My end goal is to Convert the current GitHib APIv3 written in Java for the Eclipse Studio which supports API v2 & v3, over to an Android GitHub API v3, seeing as we won't need to cover v2. 我的最终目标是将支持Javascript v2和v3的Eclipse Studio当前用Java编写的GitHib APIv3转换为Android GitHub API v3,这是因为我们不需要介绍v2。

I hope this edit is enough information to be able to answer. 我希望此编辑足以提供答案。

Formats is not declared as an array. 格式未声明为数组。 You need to declare this as an array an initialize it one by one. 您需要将其声明为数组,然后对其进行初始化。 try this, 尝试这个,

private final DateFormat[] formats formats = new DateFormat[3]; 私有的最终DateFormat []格式format = new DateFormat [3]; formats[0] = new SimpleDateFormat(getString(R.string.date_format), Locale.ENGLISH); 格式[0] =新的SimpleDateFormat(getString(R.string.date_format),Locale.ENGLISH);

to remove the loop just use formats = new DateFormat; 删除循环只需使用format = new DateFormat; formats = new SimpleDateFormat(String.valueOf(R.string.date_format), Locale.ENGLISH); 格式=新的SimpleDateFormat(String.valueOf(R.string.date_format),Locale.ENGLISH); final TimeZone timeZone = TimeZone.getTimeZone("Zulu"); 最后的TimeZone timeZone = TimeZone.getTimeZone(“ Zulu”); formats.setTimeZone(timeZone); format.setTimeZone(timeZone);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.text.DateFormat中的ParseException - ParseException in java.text.DateFormat ParseException:无法解析日期 - 类:java.text.DateFormat - ParseException: Could not parse date - Class: java.text.DateFormat 这是制作java.text.DateFormat threadSafe的正确方法吗? - Is this is a right way to make java.text.DateFormat threadSafe? 如何在 java.text.DateFormat 中使用不受支持的语言环境 - How to use Unsupported Locales in java.text.DateFormat 使java.text.DateFormat解析为java.util.Calendar - Make java.text.DateFormat parse to java.util.Calendar 调用静态java.text.DateFormat的方法不可取吗? - Call to method of static java.text.DateFormat not advisable? 将java.text.DateFormat实例转换为strftime字符串? - Convert a java.text.DateFormat instance to a strftime string? 为什么java.text.DateFormat在Android上为en_US和en_GB返回相同的日期格式? - Why does java.text.DateFormat return the same date format for en_US and en_GB on Android? 为什么我在android studio中的代码中出现错误? - Why am i getting errors in my code in android studio? 我在 android 工作室中遇到错误。 语言 java,使用最新的 bumblebee android studio - I am getting an error in android studio. language java, using latest bumblebee android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM