Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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



Java
Calendar cal=Calendar.getInstance();
   SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
   String month_name = month_date.format(cal.getTime());


DateTimePicker
Java
// I set the month in global as static
static int month;

public Dialog onCreateDialog(Bundle savedInstanceState) //display calender
       {

           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

Java
 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.
Posted
Updated 28-Dec-15 7:07am
v2

1 solution

Pass your month int value to this method and then compare..
C#
private String getMonthName(int monthOfYear) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MONTH, monthOfYear);
    return new SimpleDateFormat("MMMM").format(c.getTime()); 
}
 
Share this answer
 
Comments
wseng 27-Dec-15 21:26pm    
Where should I add getMonthName() ? Inside onDateSet ?
ridoy 28-Dec-15 1:31am    
Not necessary. While comparing you can use
if(month_name == getMonthName(month))
{
// do action
}
wseng 28-Dec-15 12:57pm    
I wrote like this 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;
}
But it does not display the wrong message. How can I check the month value ? Thanks
wseng 28-Dec-15 13:09pm    
Kindly check my post again.
ridoy 28-Dec-15 15:06pm    
I don't get you, what do you mean by "But it does not display the wrong message"? Do your code should display a wrong message or not?! One more thing, you should give getMonthName(month+1) value rather than justgetMonthName(month) while comparing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900