Android Get current month

16,834

Answer for How to know the current month? is

DateFormat dateFormat = new SimpleDateFormat("MM");
Date date = new Date();
Log.d("Month",dateFormat.format(date));

Using the above function you can get the current date time using the Date class

and

Answer for How to populate the data that are related to current month? is

Write an SQL SELECT query to select all the data from the table that you want where the month is equal to the above obtained month..

Share:
16,834
89030943809
Author by

89030943809

Enthusiasm in Android Development. Just a passionate developer.

Updated on June 14, 2022

Comments

  • 89030943809
    89030943809 almost 2 years

    In my app, I want to get the current month only (no need year and day). Example. This month is January. Show the listview data from db for that related to January. For all the 12 months. Users are also can pick the any months from dialog box. I have also listed months in dialog box to pick the month for user want.

    Problems

    1. To get Current month and then populate the data from db (ex. Current month is January then populate the January data from db, if current month is April then populate the April data from data ) for every months.

    Questions

    1. How to known the current month?

    2. How to populate the data that are related to current month?

    Current code:

    public class MonthlyFragment extends Fragment {
        private int monthField;
    
        public MonthlyFragment() {
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_monthly, container, false);
            //    String currentDateString = DateFormat.getDateInstance().format(new Date());
    
            Button button = (Button) rootView.findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showAlertDialog1();
                }
            });
    
            return rootView;
        }
    
        private void showAlertDialog1() {
            final String[] Selected = new String[]{"January", "February", "March", "April",
                    "May", "June", "July", "Augest", "September", "October", "November", "December"};
    
            new AlertDialog.Builder(getActivity()).setTitle("Pick the month")
                    .setItems(Selected, new DialogInterface.OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (Selected[which] == "January") {
                                // Show the 
                            }
    
    
                            Toast.makeText(getActivity(), Selected[which], Toast.LENGTH_SHORT)
                                    .show();
    
    
                        }
                    }).setNeutralButton("Close dialog", null).show();
        }
    
    
    }
    

    Thanks,

  • 89030943809
    89030943809 over 9 years
    I have updated my question. Pls check the changed and let's me known your idea. Thanks for your answer.