1

我正在尝试从 Datepicker 获取日期,我的代码如下

dpResult = (DatePicker) findViewById(R.id.dpResult);
 btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Calendar current = Calendar.getInstance();
                gettingDate();

            }

            private void gettingDate() {
                // TODO Auto-generated method stub
                int   day  = dpResult.getDayOfMonth();
                int   month= dpResult.getMonth();
                int   year = dpResult.getYear();

                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 formatedDate = sdf.format(new Date(year, month, day));
            //  You can parse the String back to Date object by calling

                try {
                     date = sdf.parse(formatedDate);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Toast.makeText(getApplicationContext(), ""+formatedDate, 1000).show();
            }
        });

我的吐司正确显示日期-月份,但在年份的前两位数字为 39 即像 3913,3914 等请帮助我

4

3 回答 3

3

you have to use this format for getting perfect and right year.

dpResult.getYear()-1900;
于 2013-08-01T04:57:15.623 回答
2

利用dpResult.getYear()-1900

int   year = dpResult.getYear();
于 2013-08-01T04:58:32.187 回答
0

日期已弃用,您应该使用 Calendar 代替以下语法

Calendar cal = Calendar.getInstance()
cal.set(dpResult.getYear(), dpResult.getMonth()-1,getDayOfMonth())
于 2013-08-01T05:04:43.710 回答