I am using customized Number Picker in my app to get the month and year as an input from the user. By selecting the Month in the number picker the month value should be saved as String and the year should be saved as an integer value. So I just want to get the complete Date format from the String month name and integer year value.
This is my customized month picker.
final Dialog d = new Dialog ( getContext ( ) );
d.setContentView ( R.layout.dialog );
final NumberPicker monthNp = (NumberPicker) d.findViewById ( R.id.monthPicker );
final NumberPicker yearNp = (NumberPicker) d.findViewById ( R.id.yearPicker );
Button okDialog = (Button) d.findViewById ( R.id.ok_dialog );
Button cancelDialog = (Button) d.findViewById ( R.id.cancel_dialog );
monthNp.setMinValue ( 0 );
monthNp.setMaxValue ( monthArray.length - 1 );
monthNp.setDisplayedValues ( monthArray );
monthNp.setWrapSelectorWheel ( false );
monthNp.setValue ( imonth );
monthNp.setOnValueChangedListener ( new NumberPicker.OnValueChangeListener ( ) {
@Override
public void onValueChange ( NumberPicker picker, int oldVal, int newVal ) {
if ( oldVal != 0 ) {
changed = 1;
String getMonthFromNp = monthArray[newVal];
}
}
} );
}
if ( yearNp != null ) {
yearNp.setMinValue ( year - 15 );
yearNp.setMaxValue ( year );
yearNp.setValue ( year );
yearNp.setWrapSelectorWheel ( false );
yearNp.setOnValueChangedListener ( new
NumberPicker.OnValueChangeListener ( ) {
@Override
public void onValueChange ( NumberPicker picker, int oldVal,
int newVal ) {
if ( oldVal != 0 ) {
changed = 1;
int getYearFromNp = yearNp.getValue ( );
}
}
} );
try{
SimpleDateFormat monthFromNP = new SimpleDateFormat ( "MMMM" );
Date month = monthFromNP.parse( getMonthFromNp );
calendar = Calendar.getInstance ( );
calendar.setTime ( month );
calendar.set ( Calendar.YEAR, getYearFromNp );
calendar.set ( Calendar.MONTH, +1 );
calendar.set ( Calendar.DAY_OF_MONTH, 0 );
calendar.set ( Calendar.DATE,
calendar.getActualMinimum ( Calendar.DAY_OF_MONTH ) );
calendar.set ( Calendar.HOUR_OF_DAY, 0 );
calendar.set ( Calendar.MINUTE, 0 );
calendar.set ( Calendar.SECOND, 0 );
calendar.set ( Calendar.MILLISECOND, 0 );
startDate = calendar.getTime ( );
calendar.set ( Calendar.DATE,
calendar.getActualMaximum ( Calendar.DAY_OF_MONTH ) );
calendar.set ( Calendar.HOUR_OF_DAY, 23 );
calendar.set ( Calendar.MINUTE, 59 );
calendar.set ( Calendar.SECOND, 59 );
calendar.set ( Calendar.MILLISECOND, 999 );
endDate = calendar.getTime ( );
}
catch ( ParseException e ) {
e.printStackTrace ( );
}