1

so I am using nodejs to send back a json object, on return I get the following value for date "2016-05-02T04:00:00.000Z" What I need to do is store this in the sqlite db as a long on my Android app - (timezone does not matter, the only things I care about are the year,month, and day).

Now I will be getting a list of 50 objects with this date format - so it has to be efficient somewhat.

I have read that timezones are not parse-able but then people stated that in the new Java it is, so please let me know how I can go about parsing it correctly.

Is SimpleDateFormat suppose to work?

Lion789
  • 4,402
  • 12
  • 58
  • 96
  • Java 8 has the `java.time.*` classes, which will help you work with timezones and such, but Android does not have these classes according to [this](http://developer.android.com/preview/j8-jack.html) despite having some of the language features. – Darth Android May 05 '16 at 18:26

3 Answers3

1

you may use Calendar class like this:

Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);

but before that you have to manually parse your String. Split by letter "T", then get array[0] and split by "-", then Integer.parseInt(..) to get day, month and year ints. For timestamp you may use c,getTimeInMillis();

snachmsm
  • 17,866
  • 3
  • 32
  • 74
0

Just simply use time in milliseconds.

In Android for getting the current time you can use System.currentTimeMillis and from that you can easily make out year, month and day.

Ashish
  • 183
  • 9
  • refer this for converting millis to date format http://stackoverflow.com/questions/7953725/how-to-convert-milliseconds-to-date-format-in-android – Ashish May 05 '16 at 18:30
  • Well the server has the date stored, so I need to grab that date and turn it into a long - I am not creating a new date. – Lion789 May 05 '16 at 18:56
0

Yes, SimpleDateFormat works.

What you need to do is parse your String timestamp into a Date object from which you could retrieve the millisecond differential since Jan. 1, 1970, midnight GMT.

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
 Date d = sdf.parse(timestamp);

and then you call the getTime() method to get the diffential between your timestamp and Jan. 1, 1970, midnight GMT.

d.getTime();

doc : http://developer.android.com/reference/java/text/SimpleDateFormat.html and http://developer.android.com/reference/java/util/Date.html#getTime()

Gauthier
  • 4,798
  • 5
  • 34
  • 44