Rajakumar
Rajakumar

Reputation: 937

How to get datepicker month field in two digit format in android

How to display the month field in two digit format like 1-02-2017 and didn't get the proper method to achieve this format.Suggest some possible solutions.

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(" "));
    }

}

Upvotes: 3

Views: 6436

Answers (5)

user16468508
user16468508

Reputation: 1

Calendar calendar= Calendar.getInstance();

calendar.set(year,month,day);

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

Upvotes: 0

Dhara Patel
Dhara Patel

Reputation: 359

see here

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
*/

Upvotes: 0

Saurabh Bhandari
Saurabh Bhandari

Reputation: 2458

You can achieve this as follows

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(); 

I hope this works for you

Upvotes: 1

Saurabh
Saurabh

Reputation: 1245

update your populateSetDate as :

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);
    }

Upvotes: 0

Chintan Bawa
Chintan Bawa

Reputation: 1386

Replace your populateSetDate with below written method.

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

        String monthString = String.valueOf(month);
        if (monthString.length() == 1) {
            monthString = "0" + monthString;
        }

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

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

Upvotes: 9

Related Questions