Android 将当前年份插入 TextView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2215069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:59:51  来源:igfitidea点击:

Inserting the current year into a TextView

androiddate

提问by Ljdawson

Just a small problem really, I want to place the current year into the footer of my apps. Each footer is a TextView, which is seen on menu screens etc. Is there a way to insert the year into this dynamically?

真的只是一个小问题,我想将当前年份放入我的应用程序的页脚中。每个页脚都是一个 TextView,可以在菜单屏幕等上看到。有没有办法动态地将年份插入其中?

Cheers, Laurence

干杯,劳伦斯

回答by Erich Douglass

You can do something like:

您可以执行以下操作:

Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);

TextView footer = findViewById(R.id.footer);
footer.setText("" + year);

回答by user1364078

Hi Guys this is the code by what you can achieve the dropdown array of last 15 years (As the vehicle registration is valid for 15 years in India). So you can show this by the below code on Spinner dropdown in android.

    ArrayList<YearModel> arrayListYear=new ArrayList<>();
    int year,startYear,endYear;

    Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
            startYear=year-15;
            endYear=startYear+14;

            arrayListYear.clear();
            for(int i=startYear;i<=endYear;i++){
                arrayListYear.add(new YearModel(""+i));
            }

            ArrayList<String> names = new ArrayList<>();
            for(YearModel model : arrayListYear) {
                names.add(model.getName());
                Log.e("Year","==>"+names);
            }

            spCarBuyYear.setAdapter(new ArrayAdapter<String>(MotorInsuranceActivity.this, R.layout.spinner_item, names));