Reputation: 144
I am having a date parse problem. In my app I retrieve new comments from our server. The retrieved epoch long timestamps are correct, but when I try to save them to the sqlite db sometimes the last comments parses the date wrong.
SimpleDateFormat:
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Parsing:
Log.v("GET COMMENT TIME A", ""+cu.getString(cu.getColumnIndex("creation_time")));
try {
Log.v("GET COMMENT TIME B",""+dateFormat.parse(cu.getString(cu.getColumnIndex("creation_time"))));
c.setCreation_time(dateFormat.parse(cu.getString(cu.getColumnIndex("creation_time"))));
Log.v("GET COMMENT TIME C", ""+c.getCreation_time());
} catch (ParseException e) {}
Logcat: previous comments parsed GOOD
01-13 13:01:58.009: V/GET COMMENT TIME A(10536): 2013-01-13 12:01:37
01-13 13:01:58.017: V/GET COMMENT TIME B(10536): Sun Jan 13 13:01:37 CET 2013
01-13 13:01:58.017: V/GET COMMENT TIME C(10536): Sun Jan 13 13:01:37 CET 2013
Logcat: last comment parsed WRONG:
01-13 13:01:58.064: V/GET COMMENT TIME A(10536): 2013-01-13 12:01:41
01-13 13:01:58.064: V/GET COMMENT TIME B(10536): Sun Jan 13 13:01:41 CET 2013
01-13 13:01:58.064: V/GET COMMENT TIME C(10536): Tue Jan 01 13:01:41 CET 2013
So when you look at logcat GET COMMENT TIME B and C, you can see that
dateFormat.parse(cu.getString(cu.getColumnIndex("creation_time")))
first returns a corrected parsed time and 1 line further it parses another wrong time? Or why does the getCreation_time() return a wrongly parsed date sometimes?
edit: the comment just has getter ans setters
public Date getCreation_time() {
return creation_time;
}
public void setCreation_time(Date creationTime) {
this.creation_time = creationTime;
}
Upvotes: 3
Views: 1515
Reputation: 17735
So, parsing 2 times the exact same String
sometimes gives different results ? The only reason I can think of is that the SimpleDateFormat
is not threadsafe.
Is it frequent ? What happens if you use a variable instead of an instance field ?
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.v("GET COMMENT TIME A", ""+cu.getString(cu.getColumnIndex("creation_time")));
try {
Log.v("GET COMMENT TIME B",""+dateFormat.parse(cu.getString(cu.getColumnIndex("creation_time"))));
c.setCreation_time(dateFormat.parse(cu.getString(cu.getColumnIndex("creation_time"))));
Log.v("GET COMMENT TIME C", ""+c.getCreation_time());
} catch (ParseException e) {}
Upvotes: 3