在android中转换日期格式?

时间:2013-07-26 10:17:15

标签: java android calendar

我正在使用日历日期选择器。我收到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);

    }
};

7 个答案:

答案 0 :(得分:3)

String date = new SimpleDateFormat("yyyy/MM/dd").format(date_selected);

答案 1 :(得分:3)

使用日期格式化

    String date="2013/7/7";
    SimpleDateFormat df=new SimpleDateFormat("yyyy/MM/dd");
    System.out.println(df.format(df.parse(date)));

答案 2 :(得分:2)

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

示例代码将是这样的:

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

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

答案 3 :(得分: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);

答案 4 :(得分: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();

答案 5 :(得分: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

答案 6 :(得分: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()));
}
};