In my app, I wanted to convert the selected month from `dateTimePicker` dialog to string and finally compare with the current month when `button` is clicked. This is what I've tried so far.
For Current Month
Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
String month_name = month_date.format(cal.getTime());
DateTimePicker
static int month;
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar c=Calendar.getInstance();
int year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),this,year, month,day);
}
public void onDateSet(DatePicker view,int year, int month, int day)
{
String date1=year+"-"+String.format("%02d", month+1)+"-"+String.format("%02d", day);
date.setText(date1);
date2= date.getText().toString();
return ;
}
When button is clicked, compare the current month with selected month
CompareMonth.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if(!month_name.equals(getMonthName(month)))
{
Toast.makeText(getApplicationContext(),"You select the wrong month",Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),month_arr[month],Toast.LENGTH_LONG).show();
return;
}
});
private String getMonthName(int monthOfYear)
{
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, monthOfYear);
return new SimpleDateFormat("MMMM").format(c.getTime());
}
}
No error message is displayed even the select month is not the current month.
Can someone help me out ? Thanks.