显示日期1990-04-03没有字母

时间:2014-10-16 03:04:36

标签: android

我想显示这个日期1990-04-03,但我得到了显示4月3日星期五

private static final String DATE_FORMAT = "yyyy-MM-dd";
String date=jMemInfo.getString("dob");
SimpleDateFormat sdf= new SimpleDateFormat(DATE_FORMAT);
Date dob=sdf.parse(date);

底线是从顶部检索

textDob.setText(String.valueOf(info.getDob().toString().substring(0,10)));

2 个答案:

答案 0 :(得分:0)

Date是即时的,没有固有格式(超出toString())。要获得所需的格式,可以通过更改此

来使用DateFormat实例
textDob.setText(String.valueOf(info.getDob().toString().substring(0,10)));

DateFormat.format(Date)类似的内容

textDob.setText(sdf.format(info.getDob()));

答案 1 :(得分:0)

我附加了一个代码,用于在按钮上显示所选日期(当您单击此按钮并在其上显示所选日期时,日期对话框将打开)。

 private Button btnChangeDate;

    private int year;
    private int month;
    private int day;

    static final int DATE_DIALOG_ID = 999;

@Override
    protected void onCreate(Bundle savedInstanceState) {

//Date Button Instance
        btnChangeDate = (Button) findViewById(R.id.makepayact_btn_ChangeDate);

        //Date picker Dialog
        setCurrentDateOnView();
        addListenerOnButton();
}

// display current date
    public void setCurrentDateOnView() {
        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        // set current date into textView
        btnChangeDate.setText("Select");
    }

    //Button Listener
    public void addListenerOnButton() {

        btnChangeDate = (Button) findViewById(R.id.makepayact_btn_ChangeDate);
        btnChangeDate.setOnClickListener(new OnClickListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            return new DatePickerDialog(this, datePickerListener, year, month,day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            //getting current date instance
            final Calendar c = Calendar.getInstance();
            int current_year = c.get(Calendar.YEAR);
            int current_month = c.get(Calendar.MONTH);
            int current_day = c.get(Calendar.DAY_OF_MONTH);

            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;


                // set selected date into TextView
                btnChangeDate.setText(new StringBuilder().append(day)
                        .append("-").append(month + 1).append("-").append(year)
                        .append(" "));


        }
    };

您可以以字符串格式获取日期,然后将其转换为日期。

如果这可以解决您的问题,请告诉我。 :)

另外,如果您希望格式为yyyy-mm-dd,请更改StringBuilder格式。