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

Adobe AIR Development

Reply
Developer
eugenevk
Posts: 167
Registered: ‎12-17-2010
My Device: Bold 9000

Calendar control taking into account leap years

I have browsed the forum and the internet many times now in the last one and a half year and am wondering: isn't there anybody who in the meantime has improved the example calendar control (from the guide) so it takes into account leap years? If you look at the PlayBook (OS2.0) there are multiple locations where this calendar is present and working with leap years. Why reinventing the wheel?

Eugene

My PlayBook App: Checklists
Retired
dmalik
Posts: 427
Registered: ‎02-22-2012
My Device: BlackBerry Z10, BlackBerry Dev Alpha C, BlackBerry PlayBook

Re: Calendar control taking into account leap years

Hi Eugene,

 

Here is some code that handles leap years as well as months with only 30 days. I based it off the sample provided in the documentation. It's not the most elegant solution but it should give you what you need.

 

Cheers,
Dustin

 

 

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import qnx.fuse.ui.picker.Picker;
	import qnx.ui.data.DataProvider;
	
	[SWF(height="600", width="1024", 
    frameRate="30", backgroundColor="#FFFFFF")]
	public class calendarPicker extends Sprite
	{
		private var calPicker:Picker;
		
		//Create the DataProviders
		
		private var dayDP:DataProvider;
		private var monthDP:DataProvider;
		private var yearDP:DataProvider;
		
		// Create three arrays to handle days, months, years.
		
		private var arrDay:Array=[];
		private var arrMonth:Array=[];
		private var arrYear:Array=[];
		
		//main data provider array
		private var dpp:Array = new Array();
		
		//main DataProvider
		private var updatedDataProvider:DataProvider;
		private var returnDataProvider:DataProvider;
		
		public function calendarPicker()
		{
			initializeUI();
		}
		
		private function initializeUI():void
		{
			calPicker = new Picker();
			
			calPicker.width=400;
			calPicker.height=50;
			calPicker.x=calPicker.y=300;
			calPicker.dataProvider=createDataProvider();
			addChild(calPicker);
			
			calPicker.setListWidth(0,25);
			calPicker.setListWidth(1,50);
			calPicker.setListWidth(2,25);
			
			// Set to Jan 1, 2010
			
			calPicker.selectedIndices=[0,0,110];
			
			// Add listeners for open, close and select events
			
			calPicker.addEventListener(Event.OPEN,handleOpen);
			calPicker.addEventListener(Event.CLOSE,handleClose);
			calPicker.addEventListener(Event.SELECT,handleSelect);
			
			// A simple function that displays currently selected
			// item information in the console. 
			
			traceItems();
		}
		
		
		private function createDataProvider():DataProvider
		{
			// Create the days DataProvider
			
			for (var i:int=1; i<=31; i++) {
				arrDay.push({label: i.toString()});
			}
			dayDP = new DataProvider(arrDay);
			
			// Populate and month array and create the month
			// DataProvider
			
			arrMonth.push({label: "January"});
			arrMonth.push({label: "February"});
			arrMonth.push({label: "March"});
			arrMonth.push({label: "April"});
			arrMonth.push({label: "May"});
			arrMonth.push({label: "June"});
			arrMonth.push({label: "July"});
			arrMonth.push({label: "August"});
			arrMonth.push({label: "September"});
			arrMonth.push({label: "October"});
			arrMonth.push({label: "November"});
			arrMonth.push({label: "December"});
			
			monthDP = new DataProvider(arrMonth);
			
			// Create the year DataProvider
			
			for (var l:int=1900; l<2015; l++) {
				arrYear.push({label: l.toString()});
			}
			
			yearDP=new DataProvider(arrYear);
			
			// Add the day, month and year DataProviders to the main
			// array
			
			dpp.push(dayDP);
			dpp.push(monthDP);
			dpp.push(yearDP);
			
			returnDataProvider= new DataProvider(dpp);
			return(returnDataProvider);
			
		}
		
		private function traceItems():void
		{
			for (var j:int = 0; j < calPicker.selectedItems.length; j++) {
				trace("Picker.selectedItems "+j+" = " +calPicker.selectedItems[j].label);
			}
			trace("Picker.selectedIndices "+calPicker.selectedIndices);
			maxdays(calPicker.selectedIndices[1],calPicker.selectedItems[2].label);
		}
		
		
		private function handleSelect(e:Event):void {
			traceItems();
		}
		
		private function handleOpen(e:Event):void {
			trace("Picker.open event captured");                
		}
		
		
		private function handleClose(e:Event):void {
			trace("Picker.close event captured");
		}
		
		public function updateDays(days:int):void{
			arrDay.length = 0;
			dpp.length = 0;
			for (var i:int=1; i<=days; i++) {
				arrDay.push({label: i.toString()});
			}
			dayDP.updateItemsAt(arrDay,0);
			dpp.push(dayDP);
			dpp.push(monthDP);
			dpp.push(yearDP);
			returnDataProvider.updateItemsAt(dpp,0);
		}
		
		public function maxdays(month:int,year:int):void
		{
			if (month == 1)
			{
				if ((year % 4 == 0 && year % 100 != 0) || 
					year % 400 == 0) 	// Check for leap year
				{
					updateDays(29);		// Leap year
				}
				else
				{
					updateDays(28);		// Non-Leap year
				}
			}
			else if(month==3||month==5||month==8||month==10)
			{
				updateDays(30);	// Months with 30 days
			}
			else
			{
				updateDays(31);	// Months with 31 days
			}
		}
			
	}
	

}   

 

Follow me on Twitter: @dustinmalik
-----------------------------------------------------------------------------------------------------
Keep up to date on BlackBerry development: http://devblog.blackberry.com/