0

我正在研究日历日期选择器。我正在获取格式的日期2013/7/7,但我希望格式为2013/07/07

代码:

protected Dialog onCreateDialog(int id)  
{

    Calendar c = Calendar.getInstance();
    int cyear = c.get(Calendar.YEAR);
    int cmonth = c.get(Calendar.MONTH);
    int cday = c.get(Calendar.DAY_OF_MONTH);

    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
    }


    private DatePickerDialog.OnDateSetListener mDateSetListene = new DatePickerDialog.OnDateSetListener() 
    {
        // onDateSet method
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        String date_selected = String.valueOf(year)+" /"+String.valueOf(monthOfYear+1)+" /"+String.valueOf(dayOfMonth);

        dob.setText(date_selected);

    }
};
4

7 回答 7

3

使用日期格式化程序

    String date="2013/7/7";
    SimpleDateFormat df=new SimpleDateFormat("yyyy/MM/dd");
    System.out.println(df.format(df.parse(date)));
于 2013-07-26T10:20:48.560 回答
3
String date = new SimpleDateFormat("yyyy/MM/dd").format(date_selected);
于 2013-07-26T10:20:18.460 回答
2

你正在以艰难的方式做事。使用SimpleDateFormat,您可以更轻松地编写、阅读和维护代码。尽量不要重新发明轮子。

示例代码将是这样的:

String date_string = new SimpleDateFormat("yyyy/MM/dd").format(original_date);

当然,SimpleDateFormat如果您需要多次执行操作,您可以重复使用 的实例。

于 2013-07-26T10:21:11.407 回答
1
String strDate="2013/7/7";
                String[] strsplit=strDate.split("/");
                int year=Integer.parseInt(strsplit[0]);
                int month=Integer.parseInt(strsplit[1]);
                int day=Integer.parseInt(strsplit[2]);

                if((month)<=9)
                    strDate+="0";
                strDate+=(month )+"/";
                if(day<=9)
                    strDate+="0";
                strDate+=(day)+"/";
                strDate+=(year);
于 2013-07-26T10:54:58.097 回答
0

将 Calendar 实例传递给 DatePickerDialog,然后根据需要使用 SimpleDateFormat 进行格式化。

private DatePickerDialog.OnDateSetListener mDateSetListene = new DatePickerDialog.OnDateSetListener() {
// onDateSet method
public void onDateSet(DatePicker view, Calendar cal) {
    dob.setText(new SimpleDateFormat("yyyy/MM/dd").format(cal.getTime()));
}
};
于 2013-07-26T10:29:23.023 回答
0

检查这是否有帮助

    //Calender Variables
int mYear;
int mDay;
int mHour;
int mMinute;
c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH)+1;
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
TextView t = (TextView)findViewById(R.id.time);
TextView d = (TextView)findViewById(R.id.date);
d.setText("Date: " + mDay+" / "+mMonth + " / "+mYear+" ");
t.setText("Time: " + mHour+" : "+mMinute);
date = d.getText().toString();
time = t.getText().toString();
于 2013-07-26T10:24:40.313 回答
0

您可以使用SimpleDateFormat以下方式根据需要格式化日期:

String format = "yyyy/MM/dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
      Date date = sdf.parse("12/31/2013"); // this date is in format MM/dd/yyyy and it will formatted to the format yyyy/MM/dd ( 2013/12/31 )
      System.out.println(date);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    // formatting
    System.out.println(sdf.format(new Date())); // this will format the current date to the format yyyy/MM/dd
于 2013-07-26T10:25:34.410 回答