Reputation: 3485
Looking for a bit of help with taking a dat from a DatePicker Widget and storing it in an sqlite database within the app. I have the following code:
java.util.Date utilDate = null;
String y = Integer.toString(date.getDayOfMonth()) + "/" + (date.getMonth()+1) + "/" + Integer.toString(date.getYear());
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyy");
utilDate = formatter.parse(y);
java.sql.Date z = new java.sql.Date(utilDate.getDate());
x = z.toString();
Log.v("The date: ", x);
}
Where date is the DatePicker widget. If I output the utilDate variable (i.e. the Java version of date) using logCat it seems to work fine and gives me a format like: Tue Jan 04 00:00:00 GMT 2011 which I am expecting but using the code above to get the sql version of the date, it always gives me the date 1970-01-01. I'm pretty sure the solution is very simple but I just can't see it.
Upvotes: 0
Views: 261
Reputation: 47213
This part: utilDate.getDate()
is wrong.
Quote from Javadoc:
Returns the day of the month represented by this Date object. The value returned is between 1 and 31 representing the day of the month that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
So, you get a number between 1 and 31, and you expect a long that represents number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.
Use
java.sql.Date z = new java.sql.Date(utilDate.getTime());
and everything should be OK.
Upvotes: 2