0

I save a Entitiy with a new() date in java.util.Date type, on Google DataStore, and then I got it to the Android Client, and I get it in this format:

yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

example: '2015-12-27T09:49:51.013Z'

I save the date on shared preferences in Android client in string with this method:

.getDate().toStringRfc3339()

Then later, when I need send it to the server, I try to set the date of the entity in this way:

.setDate(stringDateInServerFormatToDate(date));

public com.google.api.client.util.DateTime stringDateInServerFormatToDate(String dateInServerFormat){  
                googleDate = new com.google.api.client.util.DateTime(dateInServerFormat);

        return googleDate;
    }

And that works, but when I try put it on the server by Endpoints, I get this error:

"message": "com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Can not construct instance of int from String value '2015-12-27T09:49:51.013Z': not a valid Integer value\n at [Source: N/A; line: -1, column: -1] (through reference chain: com.imaginamos.caapturesupervisor.backend.Entities.User[\"userUpdate\"])"

I have tried many fromas and have not been successful. How can I fix this?

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130

1 Answers1

2

Maybe the Endpoints API that you're talking to expects the timestamp to be a simple integer, like seconds or milliseconds. It depends on how the Cloud Endpoints API declared the timestamp field in the Bean class.

If it used com.google.api.server.spi.types.DateAndTime, it should accept the RFC3339 string that you're passing. (The same may be true for java.util.Date, though I've never tried it.) For example:

import com.google.api.server.spi.types.DateAndTime;

public class ExampleBean {
  private DateAndTime timestamp = null;

  public DateAndTime getTimestamp() {
    return timestamp;
  }
  public void setTimestamp(DateAndTime timestamp) {
    this.timestamp = timestamp;
  }
}

When building with a Bean like that, the REST JSON discovery doc advertises the field as a date-time string, which means it should accept an RFC3339 string as its value:

"timestamp": {
  "type": "string",
  "format": "date-time"
}

DateAndTime is pretty dumb, just a wrapper for the string. When working with it, I immediately turn it into a Joda DateTime:

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
...
DateTime timestamp = new DateTime(
    exampleBean.getTimestamp().toRfc3339String(),
    DateTimeZone.UTC);

But, if the Endpoints API declared the field as an int or long, it won't know how to convert your RFC3339 string into a number, and you would need to send the number directly.

If you have access to the code of the Endpoints project, try taking a look. Or, see if the API Explorer works for you, at https://PROJECT_APP_ID.appspot.com/_ah/api/explorer; it should help you figure out the expected field type.

dbort
  • 962
  • 8
  • 15
  • Hi @dbort I am developing de Endpoint too, Do you work with com.google.api.server.spi.types.DateAndTime? can you show me examples with that type, of implementation in server and client, for parsing it, thx! – Carlos Alberto Murillo Dec 23 '15 at 03:12
  • I added more info to my answer; take a look and let me know if it help! – dbort Dec 29 '15 at 23:48
  • Thanks, I will more late if works for me, then I will accept the answers – Carlos Alberto Murillo Dec 30 '15 at 15:40
  • I tray set a date: DateAndTime now = DateAndTime.parseRfc3339String(new DateTime().toString()); but server say "message": "java.lang.IllegalArgumentException: DateAndTime string is not valid RFC3339: Mon Jan 04 00:53:10 UTC 2016" – Carlos Alberto Murillo Jan 04 '16 at 04:13
  • In your example, what's the fully-qualified type of `DateTime`? Its `.toString()` is producing "Mon Jan 04 00:53:10 UTC 2016" which is not RFC3339. I'm using JodaTime (org.joda.time.DateTime) which, in my experience, produces RFC3339 strings; [this comment](http://stackoverflow.com/questions/289311/output-rfc-3339-timestamp-in-java#comment34703009_289927) agrees, but maybe things are different in your environment. – dbort Jan 05 '16 at 21:17
  • is org.joda.time.DateTime – Carlos Alberto Murillo Jan 05 '16 at 21:28
  • Maybe your environment is different from mine. You could try using an explicit date formatter like in http://stackoverflow.com/a/289927/150001 (`ISODateTimeFormat`) – dbort Jan 09 '16 at 00:10