1

I'm having an issue with joda-time formatting a date time string that Parse (parse.com) has stored in an sqlite table.

Sqlite creation string: "createdDate date time"

Storing parse date into table: "insert... parseObject.getCreatedAt()"

If i then use a SQLite browser to inspect the table, I see the date stored like this:

Sat Jun 15 15:44:52 PDT 2013

So going along with that, I wrote the following to convert it back into a DateTime object to give to parse as part of a query to get items that are newer than the last inserted in my table:

 DateTimeFormatter format = DatetimeFormat.forPattern("yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'");
 DateTime dt = formatter.parseDateTime(datahelper.getLastInsertDate(..));

The formatter is this way, because in Parse's databrowser, I can see dateTimes being stored like this:

2013-06-24T08:11:45.280Z

I get an ANR though, so I tried using the following formatter:

DateTimeFormatter format = DatetimeFormat.forPattern("EEE' 'MMM' 'dd' 'HH':'mm':'ss 'z'' 'YYYY");
DateTime dt = formatter.parseDateTime(datahelper.getLastInsertDate(..));

and I still get an ANR. The trace in eclipse shows the following:

Caused by: java.lang.IllegalArgumentException: invalid format: "Tue Jun 25 00:13:29 PDT 2013" at org.joda.time.format.DateTimeFormatter.parseDateTime"

The second ANR trace shows:

Invalid format: "Tue Jun 25 00:13:29 PDT 2013" is malformed at "PDT 2013"

I've tried getting around that, as joda time does not parse "z" to PDT/PST, so I've put 'PDT' in my formatter to hopefully get it to work, but nothing seems to work.

Any ideas?

Edit 1: Using the accepted answer, I have a timezone formatting issue)

DateFormat originalFormat = new SimpleDateFormat("EEE MMM DDD HH:mm:ss z yyyy");
Date originaldate = originalFormat.parse(datahelper.getLastInsertdate);
Log.i("converted date: ", String.valueOf(originalDate);
Log.i("a real date: ", "String.valueOf(new Date(new Date().getTime)));

I get two outputs:

Fri Jan 25 15:14:11 PST 2013

Tue Jun 25 17:11:44 PDT 2013

why does the converted date show PST, and a standard Date shows PDT?

Evan R.
  • 1,210
  • 1
  • 27
  • 42
  • Have your checked or tried this? http://stackoverflow.com/questions/1525932/joda-time-bug-or-my-mistake-java-joda-time-dates-as-strings-parsing?rq=1 – VikramV Jun 25 '13 at 22:27

2 Answers2

0

It seems to be a known problem Joda cannot parse Timezone names sadly. In the documentation before all the pattern syntaxes you will see this line:

The pattern syntax is mostly compatible with java.text.SimpleDateFormat - time zone names cannot be parsed and a few more symbols are supported. All ASCII letters are reserved as pattern letters, which are defined as follows:

You can see that in the documentation link here

Now the solution to your answer can be found in this answer by @BalusC located here

Hope this helps.

Community
  • 1
  • 1
Brosa
  • 1,169
  • 9
  • 19
  • Accepted. I didn't know about the limitation (well I did, but I thought maybe I could get around it by escaping the timezone as 'PDT', but that didn't work). I really was hoping to not have to include both types of date methods as joda-time seems to be more efficient, but I guess I have no choice. thanks! – Evan R. Jun 25 '13 at 22:41
  • Actually, this somewhat didn't work. Please see the edited subject – Evan R. Jun 26 '13 at 00:12
  • Try specifying the locale in the constructor of SimpleDateFormat, ex. new SimpleDateFormat("EEE MMM DDD HH:mm:ss z yyy",Locale.US); – Brosa Jun 26 '13 at 00:31
0

I think that with SQLite, because the date type is somewhat broken, the best thing to do is to store the long that you get from Date.getTime() or related Joda methods.

When you get the long from the database, re-construct your date object (e.g. new Date(long)), and then format that.

Above all, remember that (IMHO) the only sensible way to store a date is in reference to UTC, which is what you get with Date.getTime() and new Date(long) : milliseconds since Jan 1, 1970 UTC.

Once you retrieve your date, format it with whatever timezone is appropriate.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • Thanks for the idea to store dates as long. interestingly enough, I've tried submitting just a regular "Date" object back to parse, and it didn't seem to work either. I've talked with the owners of the gym I'm doing the app for, and we've determined that the amount of data we'll ever need to transmit is very low (less than 30KB once a day) so we've decided to just empty the tables and fetch all records each time the app opens, as we're not looking at more than maybe 100 rows max. I need to keep looking at Parse's documentation though, as some of their ways of accessing data is confusing. – Evan R. Jul 02 '13 at 00:03