Reputation: 3182
I'm trying to set the Date Picker of my app with the retrieved date value stored in my database on the OnCreate method. Everything is fine until I come to set the date picker with the year, month and day values themselves. At the moment the date picker is always being set to '01 Jan 1970'. Hopefully someone can point me int the right direction here.
As shown I parse the string to a Date object then retrieve the separate year, month and day values. They're stored as interger, then using the .init method it should set my date picker.
Here's my code so far:
DBHandlerApp dateToEdit= new DBHandlerApp(this, null, null);
dateToEdit.open();
String returnedDate = dateToEdit.getTime(passedID);
SimpleDateFormat newFormatDate = new SimpleDateFormat("dd-MMM-yyyy");
try
{
dateToEditApp = newFormat.parse(returnedDate);
} catch (ParseException e)
{
e.printStackTrace();
}
Calendar calDate = Calendar.getInstance();
calDate.setTime(dateToEditApp);
int year = calDate.get(Calendar.YEAR);
int monthOfYear = calDate.get(Calendar.MONTH);
int dayOfMonth = calDate.get(Calendar.DAY_OF_MONTH);
editDatePicker.init(year, monthOfYear, dayOfMonth, null);
Upvotes: 0
Views: 1366
Reputation: 46
There might be a small issue with your code, i think you misnamed the newFormatDate when trying to parse it, which would goto the default value of the dateToEditApp (which is the 1-1-1970 date)
shouldnt
dateToEditApp = newFormat.parse(returnedDate);
be
dateToEditApp = newFormatDate.parse(returnedDate);
?
Upvotes: 1
Reputation: 5684
the date '01 Jan 1970' is an default value, because it starts counting milliseconds from '01 Jan 1970' till today. So I guess something went wrong, when you acquire your date from the database or while parsing it. Can you check wether the parse throws an exception?
Upvotes: 0