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
Highlighted
Developer
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company
Accepted Solution

Setting the limit for DatePicker

Can anyone tell me how to set the maximum and minumum date for date picker....I tried the setMaximumDate()

and setMinimumDate() methods but it is not ristricting the day and month of date picker . I want the date picker

to show only 10days from the current date. Means if this 10 days are fallen between 2months then only that

2months only has to show in the picker...Please help me as shown as possible.

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

Re: Setting the limit for DatePicker

In my experience, setting limits is pretty easy and works correctly. 

 

Can you give us a code snippet that demonstrates the problem (one we can use immediately without needing any of your classes).  Also tell us the test environment - which Simulator and which level of OS. 

 

Developer
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company

Re: Setting the limit for DatePicker

This is my code(to display days from 31stDec2014 to 3Jan2015)

====================================================

public void datepicker()
{  
    Calendar minDate = Calendar.getInstance();
    minDate.set(Calendar.DAY_OF_MONTH, 31);
    minDate.set(Calendar.MONTH, 11);
    minDate.set(Calendar.YEAR, 2014);
    Date d=new Date();
    Calendar cal = null ;
    DateTimePicker datePicker = DateTimePicker.createInstance(cal,DateFormat.DATE_DEFAULT,DateFormat.MINUTE_FIELD);
    
    long tmp=((24*3600*1000)*4);
    long endTimeMill=( minDate.getTime().getTime())+tmp;
     Calendar calenderE= Calendar.getInstance();
     StringBuffer calStringE = new StringBuffer();
     calenderE.setTime(new Date(endTimeMill));  
     StringBuffer sb = new StringBuffer();
     sb = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calenderE, sb, null);
    String currentTime = sb.toString();
    int current_year = Integer.parseInt(currentTime.substring(0, 4));
    int current_month = Integer.parseInt(currentTime.substring(5, 7));
    int current_day = Integer.parseInt(currentTime.substring(8, 10));
    
    Calendar c1 = Calendar.getInstance();
     c1.set(Calendar.YEAR, current_year);
     c1.set(Calendar.MONTH, current_month - 1);
     c1.set(Calendar.DAY_OF_MONTH, current_day);
     //setting the limit here///
     datePicker.setMaximumDate(calenderE);
     datePicker.setMinimumDate(minDate);  
    if(datePicker.doModal()){
         cal = datePicker.getDateTime();
         Date date = cal.getTime();
           
             dateL.setText(""+cal.get(cal.DAY_OF_MONTH)+"-"+(cal.get(cal.MONTH)+1)+"-"+cal.get(cal.YEAR));
    }
    
    
}

=============================================================

now i want the date picker should display only 4day and its related months and year.

**code is for OS5,OS6,OS7 and i tried in simulator

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

Re: Setting the limit for DatePicker

Unfortuntely your code was just too confusing for me to try to figure out what you had done wrong. 

 

As a general rule, I suggest that you try to simplify all your code, especially when it doesn't work, and look for alternative ways of doing the same thing.  You should also look at the supplied API to see if you are in fact re-inventing the wheel.

 

In addition, you need to be aware that 4 days is not always the same as (24*3600*1000)*4 - over a Daylight Saving time change, 4 days could be one hour longer or shorter. 

 

Also you should also watch out for overflow on integer variables.

 

Anyway, the following code seems to work. 

 

Sorry code formatting is not working for me, so I have just pasted the code in...

 

    public void datepicker() {

     // create min and max dates
     Calendar minDateCal = Calendar.getInstance(); // will have date/time now
        DateTimeUtilities.zeroCalendarTime(minDateCal); // So only have Date...
        minDateCal.set(Calendar.HOUR_OF_DAY, 12); // Set middle of the day so DST changes don't impact us.
        Calendar maxDateCal = DateTimePicker.cloneCalendar(minDateCal);
        // Set Calendar 4 days forward of now. 
        Date tempDate = maxDateCal.getTime();
        long currentDateLong = tempDate.getTime();
        long currentDateInFourDaysLong = currentDateLong + 4 * DateTimeUtilities.ONEDAY;
        tempDate.setTime(currentDateInFourDaysLong);
        maxDateCal.setTime(tempDate);
       
        // create Picker
        Calendar cal = Calendar.getInstance();
        DateTimePicker datePicker = DateTimePicker.createInstance(cal, DateFormat.DATE_DEFAULT,DateFormat.MINUTE_FIELD);
        datePicker.setMaximumDate(maxDateCal);
        datePicker.setMinimumDate(minDateCal);

        if (datePicker.doModal()) {
         // User did not cancel Picker - extract selected Date...
             cal = datePicker.getDateTime();
             System.out.println(""+cal.get(cal.DAY_OF_MONTH)+"-"+(cal.get(cal.MONTH)+1)+"-"+cal.get(cal.YEAR));
        }

    }

 

 

 

 

 

Developer
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company

Re: Setting the limit for DatePicker

I replaced my datepicker() with your code....

but now also the all the months are there in the picker and i can select it also. Only for the year ,the min &max

method is working . So finall it is allowing me to select any date of the year 2014.

Please check and let me know where the code is going wrong

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

Re: Setting the limit for DatePicker

Does not do that in my testing.  I am developing using OS 5.0 and tested on OS 5-7.1. 

 

Can you tell us the OS level you are developng using and the OS Level you are testing with?  Can you try various SImulators to test whether it is specific to one? 

Developer
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company

Re: Setting the limit for DatePicker

I tested only  in OS5 simulator.

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

Re: Setting the limit for DatePicker

Can you please answer all my questions?

Developer
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company

Re: Setting the limit for DatePicker

Sorry for giving you incomplete information.

I am using Blackberrry JRE 5.0.0 for developing the app and the simulator in which i tested is Blackberry-SDK-9700. Presently my client is going to test the app for 480x360 resolution, thats why i used this simulator.

Developer
Posts: 109
Registered: ‎08-21-2012
My Device: Blackberry curve
My Carrier: company

Re: Setting the limit for DatePicker

I tested in OS7 simulator the same code it is working fine....Thankyou very much...

Can u please help me to explain why the above code is not working properly in9700 simulator.