Get a raw <date> marker

26 views
Skip to first unread message

Jorisk

unread,
Sep 3, 2014, 4:32:44 AM9/3/14
to plist-...@googlegroups.com
Hi,

I am using dd-plist to parse a .plist file and store the datas in an Android SQLite Database. Everything works well, except the dates.
When I parse a <date> marker with a SimpleDateFormat, the date is already transformed. Example:

.plist Input: <date>2014-09-01T11:07:20Z</date>
Output: Mon Sep 01 11:07:20 UTC+00:00 2014

But the problem is that I cannot parse the date with a SimpleDateFormat to store it inside the database (with an INTEGER type), because the format depends on the device.
This is what I get on my Samsung Galaxy Tab:
Mon Sep 01 11:07:20 UTC+00:00 2014

This is what I get on my Nexus 7:
Mon Sep 01 11:07:20 CEST 2014

As you can see, the TimeZone format is not the same, and few other tests confirm that it changes depending on the device configuration.

So my question is: is it possible to parse a <date> marker as a "raw" String (and not as formatted date), to get the following format:  2014-09-01T11:07:20Z and not the device date: Mon Sep 01 11:07:20 UTC+00:00 2014 ?

Thanks by advance!

Jorisk

Daniel Dreibrodt

unread,
Sep 3, 2014, 7:01:46 AM9/3/14
to plist-...@googlegroups.com
Hi Jorisk,

the format in which the date is stored in the property list is well defined (as you have written it is stored like 2014-09-01T11:07:20Z).

The plist library parses these values as Java Date objects. To turn them again into a string with the same format you need to use a SimpleDateFormat object.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
NSDate date = (NSDate)someDictionary.get("someDateId");
string dateString = sdf.format(date);

Then you can store the dateString in your database consistently.


I hope I understood your problem correctly.

Regards,
Daniel



--
You received this message because you are subscribed to the Google Groups "plist-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plist-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jorisk

unread,
Sep 3, 2014, 8:35:48 AM9/3/14
to plist-...@googlegroups.com
Thanks for your fast answer Daniel!

I think you well understand my problem, but this solution is not working.
I get a parsing error because dateString return the date in this format:
Sep 01 11:07:20 UTC+00:00 2014 
...and not in the format I want: 
2014-09-01T11:07:20Z

I also tried to put a Locale parameter in my SimpleDateFormat, but the problem seems to be that when you get the date (in your example), the printed dateString is already converted in the following format: Sep 01 11:07:20 UTC+00:00 2014.

But thanks to your answer, I will be able to do something like this:
Date time = date.getDate().getTime();

This will probably help me to store the time in INTEGER in my SQLite database (I read that it is always better to store a Date in INTEGER that in TEXT in a SQLite database) without making "date parsing" problems.

If you think about another solution, don't hesitate to post here but I think this getTime() function will solve my problem.

Thanks again! 

Jorisk

Daniel Dreibrodt

unread,
Sep 3, 2014, 8:42:33 AM9/3/14
to plist-...@googlegroups.com
Hi,

Strange, sdf.format(...) should never return a string like "Sep 01 11:07:20 UTC+00:00 2014" when you construct it with new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"). Or are you talking about what the SQLite DB returns? Anyway if you can work with date.getTime() then just use that.

Regards

Jorisk

unread,
Sep 4, 2014, 9:43:01 AM9/4/14
to plist-...@googlegroups.com
Daniel,

The problem is not the SimpleDateFormat, but the format of the NSDate before the parsing function. If I take your example:

NSDate date = (NSDate)someDictionary.get("someDateId");
string dateString = sdf.format(date);

Even if the date in my .plist is 2014-09-01T11:07:20Z, the NSDate date variable will already have the format: Sep 01 11:07:20 UTC+00:00 2014, that is why it is impossible to convert it with a SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") because as you can see, it is not the same format in the SDF parameter, it is already changed. That is why I had to use this SDF parameter instead: SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy")
But the problem with this method is that it worked on few devices which have a zzzz TimeZone format (CEST for example), but other devices which had a Zzzzz TimeZone (such as UTC+00:00 for example) or other TimeZone formats couldn't parse the date, it caused ParseExceptions.

Anyway, when you do the method I told you in my previous comment, it seems to be universal (probably because it gets milliseconds time, which is perfect to store in a SQLite database):
date.getDate().getTime();

Thanks again for your help, I really appreciate it, and I hope that you understood the problem here, that is maybe focused on the NSDate class, which automatically parse a .plist date in the device format.

All the best,

Jorisk
Reply all
Reply to author
Forward
0 new messages