简体   繁体   中英

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.

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. 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.

What am I doing wrong here?

Edit:

@trooper I am not able to build the project, so I can't pull a --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'

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.

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.

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]; formats[0] = new SimpleDateFormat(getString(R.string.date_format), Locale.ENGLISH);

to remove the loop just use formats = new DateFormat; formats = new SimpleDateFormat(String.valueOf(R.string.date_format), Locale.ENGLISH); final TimeZone timeZone = TimeZone.getTimeZone("Zulu"); formats.setTimeZone(timeZone);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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