如何在android中以两位数格式获取datepicker month字段

时间:2017-01-25 07:31:12

标签: android datepicker

如何以两位数格式显示月份字段,例如1- 02 -2017,并且没有获得实现此格式的正确方法。建议一些可能的解决方案。

public static class SelectDateFragmentForJourneyDate extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

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

        DatePickerDialog d = new DatePickerDialog(getActivity(), this, yy,
            mm, dd);
        DatePicker dp = d.getDatePicker();

        dp.setMinDate(c.getTimeInMillis());
        return d;
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {

        populateSetDate(yy, mm + 1, dd);
    }

    public void populateSetDate(int year, int month, int day) {


        etPickupDate.setText(new StringBuilder().append(day).append("-")
            .append(monthof).append("-").append(year).append(" "));

        etDropDate.setText(new StringBuilder().append(day).append("-")
            .append(monthof).append("-").append(year).append(" "));
    }

}

5 个答案:

答案 0 :(得分:4)

用以下书面方法替换level

populateSetDate

答案 1 :(得分:1)

您可以按照以下方式实现此目的

Calandar calendar = Calendar.getInstance();
           int mDay = calendar.get(Calendar.DAY_OF_MONTH);
           int mMonth = calendar.get(Calendar.MONTH);
           int mYear = calendar.get(Calendar.YEAR);
            datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    monthOfYear+=1;
                    String mt,dy;   //local variable 
                    if(monthOfYear<10)
                        mt="0"+monthOfYear; //if month less than 10 then ad 0 before month
                    else mt=String.valueOf(monthOfYear);

                    if(dayOfMonth<10)
                        dy = "0"+dayOfMonth;
                    else dy = String.valueOf(dayOfMonth);
                    date.setText(dy+"-"+mt+"-"+year);
                }
            },mYear,mMonth,mDay);
            datePickerDialog.show(); 

我希望这对你有用

答案 2 :(得分:0)

将您的populateSetDate更新为:

public void populateSetDate(int year, int month, int day) {

    Calendar calendar= Calendar.getInstance();
    calendar.set(year,month,day);
    String s =String.format(Locale.getDefault(),"%1$te-%1$tm-%1$tY",calendar);
        etPickupDate.setText(s);

        etDropDate.setText(s);
    }

答案 3 :(得分:0)

见这里

public class FormattingMonth {

  public static void main(String[] args) {

    //create Date object
    Date date = new Date();

     //formatting month in M format like 1,2 etc
     String strDateFormat = "M";
     SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

     System.out.println("Current Month in M format : " + sdf.format(date));

     //formatting Month in MM format like 01, 02 etc.
     strDateFormat = "MM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MM format : " + sdf.format(date));

     //formatting Month in MMM format like Jan, Feb etc.
     strDateFormat = "MMM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MMM format : " + sdf.format(date));

     //formatting Month in MMMM format like January, February etc.
     strDateFormat = "MMMM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MMMM format : " + sdf.format(date));

  }
}

/*
Typical output would be
Current Month in M format : 2
Current Month in MM format : 02
Current Month in MMM format : Feb
Current Month in MMMM format : February
*/

答案 4 :(得分:0)

Calendar calendar= Calendar.getInstance();

calendar.set(year,month,day);

String mDate=DateFormat.getDateInstance(DateFormat.SHORT).format(calendar.getTime());