简体   繁体   English

日期选择器格式android

[英]Date picker format android

I am kind of stuck on my project.我有点被我的项目困住了。 I am asking for help on the datepicker format I managed to display my one on a TextView in full format.我正在寻求有关datepicker格式的帮助,我设法以完整格式在 TextView 上显示我的格式。

However, after doing some coding in java, when I call in a date picker and pick a new date, it updates in short format, for example.但是,在用 Java 进行一些编码之后,当我调用日期选择器并选择一个新日期时,它会以短格式更新,例如。

Current date Friday, January, 20 2017 after using date picker it shows 20-1-17使用日期选择器后的当前日期Friday, January, 20 2017 ,它显示20-1-17

I want it to display in full, please help, below is my Java code我希望它完整显示,请帮忙,下面是我的 Java 代码

public class EntryEdit extends AppCompatActivity {

TextView textView;
int year_x, month_x, day_x;
static final int DIALOG_ID = 0;

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

    final Calendar cal = Calendar.getInstance();
    year_x = cal.get(Calendar.YEAR);
    month_x = cal.get(Calendar.MONTH);
    day_x = cal.get(Calendar.DAY_OF_MONTH);

    showDialogOnTextViewClick();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(new Date());

    TextView textView = (TextView) findViewById(R.id.date);
    textView.setText(currentDateString);

}

public void showDialogOnTextViewClick() {
textView = (TextView) findViewById(R.id.date);

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DIALOG_ID);

        }
    });
}

@Override
protected Dialog onCreateDialog(int id) {

    if (id == DIALOG_ID)
        return new DatePickerDialog(this, dpickerListener, year_x, month_x, day_x);
    return null;
}

private DatePickerDialog.OnDateSetListener dpickerListener
= new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

        year_x = year;
        month_x = month +1;
        day_x = dayOfMonth;

        String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(new Date());

        updateStartDisplay();


    }
};

private void updateStartDisplay() {
    textView.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(day_x + 1).append("-").append(month_x).append("-")
            .append(year_x).append(" "));

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu2, menu);
    return true;
}

The reason why your textView shows 20-1-2017 is because each time you pick a date in a DatePicker, you call updateStartDisplay() method, where you specifically set the text to be day-month-year .你的 textView 显示 20-1-2017 的原因是因为每次你在 DatePicker 中选择一个日期时,你都会调用updateStartDisplay()方法,在那里你专门将文本设置为day-month-year

Here's one possible solution:这是一种可能的解决方案:

private DatePickerDialog.OnDateSetListener dpickerListener
        = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

        year_x = year;
        month_x = month;
        day_x = dayOfMonth;

        Calendar calendar = Calendar.getInstance();
        calendar.set(year_x, month_x, day_x);
        String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
        textView.setText(currentDateString);

    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM