在textview中显示日期戳记值

在textview中显示日期戳记值

本文介绍了在textview中显示日期戳记值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android应用程序...其中有一个datepicker出生日期被选择...但是当显示日期...例如日期是1月1日显示而不是1和2月1日显示而不是2 ..我需要在3个文本视图中的日期值...例如,如果日期是04/02/1984 ...我需要采取04一个文本视图和2到另一个和1984年另一个.... pls help ..

i am developing an android applicATION... in which there is a datepicker for date of birth to be selected... But when the date is displayed... for example date is january 0 is displayed instead of 1 and for february 1 is displayed instead of 2.. and I need to value of date in 3 textviews... for example if date is 04/02/1984 ... i need to take 04 to one textview and 2 to another and 1984 to another one.... pls help..

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


     final DatePicker date = (DatePicker) findViewById(R.id.datePicker1);
     final TextView tvDate = (TextView) findViewById(R.id.textView2);


     date.init(date.getYear(), date.getMonth(), date.getDayOfMonth(),new OnDateChangedListener()
     {


    @Override
    public void onDateChanged(DatePicker  arg0,int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub



         String date=Integer.toString(arg1);
         String month=Integer.toString(arg2);
         String year=Integer.toString(arg3);

         tvDate.setText(date + month + year);

    }



}
    );

    }
}


推荐答案

您可以执行以下操作:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, arg3);
cal.set(Calendar.MONTH, arg2);
cal.set(Calendar.YEAR, arg1);

DateFormat dayFormat = new SimpleDateFormat("dd");
DateFormat monthFormat = new SimpleDateFormat("MM");
DateFormat yearFormat = new SimpleDateFormat("yyyy");

String date = dayFormat.format(cal.getTime());
String month = monthFormat.format(cal.getTime());
String year = yearFormat.format(cal.getTime());

这篇关于在textview中显示日期戳记值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:09