Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
PrateekJ
Posts: 7
Registered: ‎04-27-2013
My Device: 9550
Accepted Solution

Unable to getDateTime() from DateTimePicker

I am 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.

 

Thanks.

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unable to getDateTime() from DateTimePicker

When you use createInstance() to get an instance of DateTimePicker, you can specify the initial date to display.  It looks like you are not setting that, and so it is taking the time 1/1/1970, UTC, and then displaying it in your local time zone.  So look at the API to see how to set the initially displayed date. 

New Contributor
PrateekJ
Posts: 7
Registered: ‎04-27-2013
My Device: 9550

Re: Unable to getDateTime() from DateTimePicker

I have set the current date while creating the DatePicker instance.

New Contributor
PrateekJ
Posts: 7
Registered: ‎04-27-2013
My Device: 9550

Re: Unable to getDateTime() from DateTimePicker

I have set the current date while creating the DatePicker instance, still issue not solved.
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unable to getDateTime() from DateTimePicker

May we see that code?

New Contributor
PrateekJ
Posts: 7
Registered: ‎04-27-2013
My Device: 9550

Re: Unable to getDateTime() from DateTimePicker

Code :-

Calendar todaysDate = Calendar.getInstance();
Calendar dateAfter7days = Calendar.getInstance();
Calendar selectedDate = null;
DateTimePicker datePicker = null;

datePicker = DateTimePicker.createInstance(todaysDate, "EEE   MMM  dd",
   "hh:mm aa");
 if (selectedDate != null) {
  datePicker.setDateTime(selectedDate);
 }
 datePicker.setIncrement(Calendar.MINUTE, 15);
 datePicker.setMinimumDate(todaysDate);
 datePicker.setMaximumDate(dateAfter7days);

SimpleDateFormat sdf = new SimpleDateFormat(
     "EEE   MMM  dd hh:mm:ss aa");
selectedDate = datePicker.getDateTime();
   timeField.setText(sdf.formatLocal(chosenDate
     .get(Calendar.MILLISECOND)));

Thanks...
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unable to getDateTime() from DateTimePicker

Sorry, I could have solved this right from the start if I had been reading your problem correctly!

 

I have changed two things in your code, well 3 really.  Here is the old code:

 

selectedDate = datePicker.getDateTime();
   timeField.setText(sdf.formatLocal(chosenDate
     .get(Calendar.MILLISECOND)));

 

And the replacement code:

 

         datePicker.doModal(); // New line
         selectedDate = datePicker.getDateTime();
            timeField.setText(sdf.formatLocal(selectedDate.getTime().getTime()));

// chosenDate.get(Calendar.MILLISECOND) ??????

 

The things I changed were:

1) added the doModal so that the DateTimePicker was displayed

2) changed chosenDate to selectedDate

3) instead of asking the selectedDate to supply the number of milliseconds set, I asked the selectedDate for its UTC time in milliseconds.  I am sure that is what you thought you were doing, but review the documentation again to see what you were actually doing.

 

Now the DateTimePicker actually displays and you can press OK.  However it does not allow me to change the date time and it does not seem to increment in 15 minute intervals, so I suspect there is an issue with the DateTimePicker parameters, but I will let you sort that out.