Code example for Calendar

Methods: getTimeInMillis, set

0
		public void onClick(View arg0) { 
			//These force values from internal edit fields being parsed. 
			_datePicker.clearFocus(); 
			_timePicker.clearFocus(); 
			 
			_init.set(Calendar.YEAR, _datePicker.getYear()); 
			_init.set(Calendar.MONTH, _datePicker.getMonth()); 
			_init.set(Calendar.DAY_OF_MONTH, _datePicker.getDayOfMonth()); 
			 
			_init.set(Calendar.HOUR_OF_DAY, _timePicker.getCurrentHour()); 
			_init.set(Calendar.MINUTE, _timePicker.getCurrentMinute()); 
			_init.set(Calendar.SECOND, 0); 
			_init.set(Calendar.MILLISECOND, 0); 
			sendResult(_callback, _init, _opaque); 
		} 
	}; 
	 
	private OnClickListener mCancelListener = new OnClickListener() {
		public void onClick(View v) { 
			sendResult(_callback, null, _opaque); 
		} 
	}; 
 
/*	 
	private static void convertMillisecondsToDate(long millis, Date date, boolean only_date) { 
		Calendar c = new GregorianCalendar(); 
		c.setTimeInMillis(millis); 
 
		//date.setTime(0); 
		 
		int year = c.get(Calendar.YEAR); 
		int month = c.get(Calendar.MONTH); 
		int day = c.get(Calendar.DAY_OF_MONTH); 
		int hours = c.get(Calendar.HOUR_OF_DAY); 
		int minutes = c.get(Calendar.MINUTE); 
		int seconds = c.get(Calendar.SECOND); 
		 
		date.setYear(year); 
		date.setMonth(month); 
		date.setDate(day); 
		if (only_date) { 
			date.setHours(0); 
			date.setMinutes(0); 
			date.setSeconds(0); 
		} 
		else { 
			date.setHours(hours); 
			date.setMinutes(minutes); 
			date.setSeconds(seconds); 
		} 
	} 
*/	 
	 
	 
	 
	private void initialization() { 
		_init = new GregorianCalendar();
 
		_init.setTimeInMillis(_saved_time);
		 
		if (_min_time != 0) {
			_min_Date = new GregorianCalendar();
			_min_Date.setTimeInMillis(_min_time);
			_min_Date.set(Calendar.HOUR_OF_DAY, 0);
			_min_Date.set(Calendar.MINUTE, 0);
			_min_Date.set(Calendar.SECOND, 0);
			_min_Date.set(Calendar.MILLISECOND, 0);
		} 
 
		if (_max_time != 0) {
			_max_Date = new GregorianCalendar();
			_max_Date.setTimeInMillis(_max_time);
			_max_Date.set(Calendar.HOUR_OF_DAY, 0);
			_max_Date.set(Calendar.MINUTE, 0);
			_max_Date.set(Calendar.SECOND, 0);
			_max_Date.set(Calendar.MILLISECOND, 0);
		} 
		 
		this.setTitle(_title);
		 
		_datePicker = (DatePicker)findViewById(AndroidR.id.datePicker);
		_timePicker = (TimePicker)findViewById(AndroidR.id.timePicker);
		_okButton = (Button)findViewById(AndroidR.id.okButton);
		_cancelButton = (Button)findViewById(AndroidR.id.cancelButton);
		 
		_okButton.setOnClickListener(mOkListener);
		_cancelButton.setOnClickListener(mCancelListener);
		 
		_datePicker.init(_init.get(Calendar.YEAR), _init.get(Calendar.MONTH), _init.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
 
				public void	onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
					//Logger.D(TAG, "onDateChanged( year="+String.valueOf(year)+", month="+String.valueOf(monthOfYear)+", day="+dayOfMonth); 
					Calendar new_date = new GregorianCalendar();
					Calendar set_to = null;
 
					new_date.set(Calendar.YEAR, year);
					new_date.set(Calendar.MONTH, monthOfYear);
					new_date.set(Calendar.DAY_OF_MONTH, dayOfMonth);
					new_date.set(Calendar.HOUR_OF_DAY, 0);
					new_date.set(Calendar.MINUTE, 0);
					new_date.set(Calendar.SECOND, 0);
					new_date.set(Calendar.MILLISECOND, 0);
 
					//Logger.D(TAG, "            new_date: year="+String.valueOf(new_date.getYear())+", month="+String.valueOf(new_date.getMonth())+", day="+new_date.getDay()); 
					 
					if (_min_time != 0) {
						//Logger.D(TAG, "            min_date: year="+String.valueOf(_min_Date.getYear())+", month="+String.valueOf(_min_Date.getMonth())+", day="+_min_Date.getDay()); 
						if (new_date.before(_min_Date)) {
							set_to = _min_Date;
						} 
					} 
					if (_max_time != 0) {
						//Logger.D(TAG, "            max_date: year="+String.valueOf(_max_Date.getYear())+", month="+String.valueOf(_max_Date.getMonth())+", day="+_max_Date.getDay()); 
						if (new_date.after(_max_Date)) {
							set_to = _max_Date;
						} 
					} 
					if (set_to != null) {
						view.updateDate(set_to.get(Calendar.YEAR), set_to.get(Calendar.MONTH), set_to.get(Calendar.DAY_OF_MONTH));
					} 
					{ 
						Calendar s_date = new GregorianCalendar();
						s_date.setTimeInMillis(_saved_time);
						if (set_to != null) {
							s_date.set(Calendar.YEAR, set_to.get(Calendar.YEAR));
							s_date.set(Calendar.MONTH, set_to.get(Calendar.MONTH));
							s_date.set(Calendar.DAY_OF_MONTH, set_to.get(Calendar.DAY_OF_MONTH));
						} 
						else { 
							s_date.set(Calendar.YEAR, year);
							s_date.set(Calendar.MONTH, monthOfYear);
							s_date.set(Calendar.DAY_OF_MONTH, dayOfMonth);
						} 
						_saved_time = s_date.getTimeInMillis();
					} 
				} 
			} 
		); 
		 
		_timePicker.setCurrentHour(_init.get(Calendar.HOUR_OF_DAY));
		_timePicker.setCurrentMinute(_init.get(Calendar.MINUTE));
		_timePicker.setOnTimeChangedListener( new TimePicker.OnTimeChangedListener() {
				public void onTimeChanged (TimePicker view, int hourOfDay, int minute) {
					{ 
						Calendar s_date = new GregorianCalendar();
						s_date.setTimeInMillis(_saved_time);
						s_date.set(Calendar.HOUR_OF_DAY, hourOfDay);
						s_date.set(Calendar.MINUTE, minute);
						s_date.set(Calendar.SECOND, 0);
						s_date.set(Calendar.MILLISECOND, 0);
						_saved_time = s_date.getTimeInMillis();
					} 
				} 
			} 
		); 
 
		switch (_fmt) {
		case FORMAT_DATE:
			_timePicker.setVisibility(View.GONE);
			break; 
		case FORMAT_TIME:
			_datePicker.setVisibility(View.GONE);
			break; 
		case FORMAT_DATE_TIME:
			break; 
		} 
		 
	} 
	 
	@Override 
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		Logger.D(TAG, "onCreate");
		 
		setContentView(AndroidR.layout.datetime); 
		 
		Bundle extras = this.getIntent().getExtras();
		 
		if (extras.containsKey(INTENT_EXTRA_PREFIX + "callback")) 
			_callback = extras.getString(INTENT_EXTRA_PREFIX + "callback");
		 
		if (extras.containsKey(INTENT_EXTRA_PREFIX + "init")) { 
			_initial_time = extras.getLong(INTENT_EXTRA_PREFIX + "init")*((long)1000);
			_saved_time = _initial_time;		
		} 
		 
		if (extras.containsKey(INTENT_EXTRA_PREFIX + "fmt")) 
			_fmt = extras.getInt(INTENT_EXTRA_PREFIX + "fmt");
		if (extras.containsKey(INTENT_EXTRA_PREFIX + "opaque")) 
			_opaque = extras.getByteArray(INTENT_EXTRA_PREFIX + "opaque");
		 
		if (extras.containsKey(INTENT_EXTRA_PREFIX + "min_time")) 
			_min_time = extras.getLong(INTENT_EXTRA_PREFIX + "min_time")*((long)1000);
		if (extras.containsKey(INTENT_EXTRA_PREFIX + "max_time")) 
			_max_time = extras.getLong(INTENT_EXTRA_PREFIX + "max_time")*((long)1000);
		 
		 
		if (extras.containsKey(INTENT_EXTRA_PREFIX + "title")) 
			_title = extras.getString(INTENT_EXTRA_PREFIX + "title");
 
		initialization(); 
	} 
	 
	private void setFieldsEnabled(boolean v) {
		_datePicker.setEnabled(v);
		_timePicker.setEnabled(v);
		_okButton.setEnabled(v);
		_cancelButton.setEnabled(v);
	} 
	 
	private void sendResult(String callback, Calendar result, byte[] opaque) {
		this.setFieldsEnabled(false);
		long res = result == null ? 0 : result.getTimeInMillis()/((long)1000);
		Logger.D(TAG, "Return result: " + res);
		DateTimePicker.callback(callback, res, opaque, result == null);
		finish(); 
	}