vishnu gupta
vishnu gupta

Reputation: 1

Convert date format in android?

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

    }
};

Upvotes: 0

Views: 3348

Answers (7)

Bald bcs of IT
Bald bcs of IT

Reputation: 892

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

Upvotes: 1

Jon Miles
Jon Miles

Reputation: 9863

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

Upvotes: 0

Geralt_Encore
Geralt_Encore

Reputation: 3771

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

Upvotes: 3

Houcine
Houcine

Reputation: 24181

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

Upvotes: 0

Niko
Niko

Reputation: 1377

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

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70931

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.

Upvotes: 2

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

Use date formater

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

Upvotes: 3

Related Questions