Help with dates in Android

Posted by SamRowley on Stack Overflow See other posts from Stack Overflow or by SamRowley
Published on 2011-01-04T12:42:14Z Indexed on 2011/01/04 12:54 UTC
Read the original article Hit count: 179

Filed under:
|
|

Hi guys,

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about android