在android和我中连接一个字符串正确的结果

时间:2015-01-23 23:44:57

标签: java android

public void populateSetDate(int yyyy, int mm, int dd) {
    String a = " ";
    if (dd < 10) {
        a = "0" + dd;
    }
    else {

    }
    if (mm < 10) {
        a = "0" + mm;
    }   
   else
   {   

   }   
    EditText aEdit = (EditText) findViewById(R.id.editText1);
    aEdit.setText(a + "/" + a +  "/" + yyyy);
}

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yyyy = calendar.get(Calendar.YEAR);
        int mm =  calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);

3 个答案:

答案 0 :(得分:1)

你需要一个“月”字符串和一个“日”字符串,并且你没有将“else”字符串设置为任何东西,所以超过“10”的字符串都是空白的:

public void populateSetDate(int yyyy, int mm, int dd) {
    String a = " ";
    if (dd < 10) {
        a = "0" + dd;
    }
    else {
        a = "" + dd;
    }
    String b = " ";
    if (mm < 10) {
        b = "0" + mm;
    else {
        b = "" + dd;
    }      
    EditText aEdit = (EditText) findViewById(R.id.editText1);
    aEdit.setText(b + "/" + a +  "/" + yyyy);
}

答案 1 :(得分:0)

我不明白你想要做什么,但是当你接触两个字符串然后这样做。

text=text+"add new text in string"

答案 2 :(得分:-1)

那就是各种各样的错误。我建议使用SimpleDateFormat:http://developer.android.com/reference/java/text/SimpleDateFormat.html

    SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
    return output.format(date);

日期是Date对象。

编辑:修复原始代码:

public void populateSetDate(int yyyy, int mm, int dd) {
    EditText aEdit = (EditText) findViewById(R.id.editText1);
    aEdit.setText(String.format("%02d/%02d/%d", dd, mm, yyyy);
}