Reputation: 6587
I am using DateTimePicker
to Select Date and Time and when I press the ok button getting Date time by using getDateTime() and assigning it to Calender object(selectedDate
). I want to display the Time in the format (fri apr 27 06:00:00 am).
So I am using below code to format.
selectedDate = datePicker.getDateTime();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss aa");
timeField.setText(sdf.formatLocal(selectedDate.get(Calendar.MILLISECOND)));
Don't know whether it is a parsing error or DateTimePicker
returning the wrong time but Text displayed for any date time select is
Thu Jan 01 05:00:00 AM.
As I have to target Most devices, My app targeting OS5 and testing on Simulator 9550.
Upvotes: 1
Views: 186
Reputation: 31045
The problem is that you are retrieving just the value of the current date's MILLISECOND
field, not the whole date. You probably want millisecond precision, but this code
selectedDate.get(Calendar.MILLISECOND)
is simply extracting the milliseconds field from the current date/time. This is only the number of milliseconds since the previous second. Similar to how
selectedDate.get(Calendar.MONTH);
would simply return 4 today (or maybe it returns 3, I can't remember if it's 0-based).
You should use
timeField.setText(sdf.formatLocal(selectedDate.getTime().getTime()));
Upvotes: 4
Reputation: 13123
It is difficult to understand your question, since you don't tell us what happens that you don't expect and/or any error message that you get; further, I cannot find formatLocal() as a method on SimpleDateFormat anywhere.
I do notice you appear to be passing it the portion of the date that is in milliseconds. If you want the date represented as milliseconds, the method for that is Date.getTime().
Upvotes: 0