0

I'm working on calender date picker. I am getting dates in format 2013/7/7, but I want the format to be 2013/07/07

Code:

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

    }
};
jam
  • 3,640
  • 5
  • 34
  • 50
vishnu gupta
  • 1
  • 2
  • 5

7 Answers7

3
String date = new SimpleDateFormat("yyyy/MM/dd").format(date_selected);
Geralt_Encore
  • 3,721
  • 2
  • 31
  • 46
3

Use date formater

    String date="2013/7/7";
    SimpleDateFormat df=new SimpleDateFormat("yyyy/MM/dd");
    System.out.println(df.format(df.parse(date)));
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
2

You are doing things the hard way. Using SimpleDateFormat you can have an easier to write, read and maintain code. Try not to re-invent the wheel.

Sample code will be something like this:

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

And of course you can re-use the instance of the SimpleDateFormat if you need to perform the operation more than once.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
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);
Bald bcs of IT
  • 892
  • 6
  • 14
0

Check if this helps

    //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();
Niko
  • 1,367
  • 1
  • 13
  • 37
0

you can format the date as you want by using SimpleDateFormat like this :

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
Houcine
  • 24,001
  • 13
  • 56
  • 83
0

Pass the Calendar instance into DatePickerDialog, then use SimpleDateFormat to format as required.

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()));
}
};
Jon Miles
  • 9,605
  • 11
  • 46
  • 66