如何获取日期选择器中已更改的日期

发布于 2024-12-12 13:56:01 字数 91 浏览 2 评论 0原文

我正在开发一个应用程序,它首先使用“Calendar.getInstance()”从 DatePicker 获取当前日期,然后用户更改日期。现在如何获取更改后的新日期?

Am developing an app, which first gets the current date from DatePicker using "Calendar.getInstance()", and then the user changes the date. Now how do I fetch the new date after it has been changed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱没勇气 2024-12-19 13:56:01

明确这一点。

private static final int DATE_DIALOG_ID = 0;

在 onCreate() 中添加此代码

 // add a click listener to the button
    date_text.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    // get the current date      
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH); 
    String da = Integer.toString(mMonth)+"/"+Integer.toString(mDay)+"/"+Integer.toString(mYear);
    date_text.setText(da);

,这是 onCreate() 之外的

 private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        String selectedDate = Integer.toString(mMonth)+"/"+Integer.toString(mDay)+"/"+Integer.toString(mYear);
        date_text.setText(selectedDate);
        System.out.println("selected date is:"+selectedDate);
    }
};

@Override
protected Dialog onCreateDialog(int id)  {
    switch (id) {

    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);        
    }
    return null;
}

declear this .

private static final int DATE_DIALOG_ID = 0;

add this coding in onCreate()

 // add a click listener to the button
    date_text.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    // get the current date      
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH); 
    String da = Integer.toString(mMonth)+"/"+Integer.toString(mDay)+"/"+Integer.toString(mYear);
    date_text.setText(da);

and this is out side of onCreate()

 private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        String selectedDate = Integer.toString(mMonth)+"/"+Integer.toString(mDay)+"/"+Integer.toString(mYear);
        date_text.setText(selectedDate);
        System.out.println("selected date is:"+selectedDate);
    }
};

@Override
protected Dialog onCreateDialog(int id)  {
    switch (id) {

    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);        
    }
    return null;
}
尸血腥色 2024-12-19 13:56:01
Calendar cal = Calendar.getInstance();

DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, monthOfYear);
                cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            }
        };
        return new DatePickerDialog(this, listener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));

并将日期检索为字符串(星期五,28/10/2011 15:59:00:0)

SimpleDateFormat sdfDate = new SimpleDateFormat("EEEE, d/M/yyyy HH:mm:ss:S");
String date = sdfTime.format(new Date(cal.getTimeInMillis()));
Calendar cal = Calendar.getInstance();

DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener(){
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, monthOfYear);
                cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            }
        };
        return new DatePickerDialog(this, listener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));

And to retrieve the date as String (Friday, 28/10/2011 15:59:00:0)

SimpleDateFormat sdfDate = new SimpleDateFormat("EEEE, d/M/yyyy HH:mm:ss:S");
String date = sdfTime.format(new Date(cal.getTimeInMillis()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文