How to set the Date in the format 12 May,2014 and Time in the format 1:30 PM using Datepicker and Timepicker in Android?

StackOverflow https://stackoverflow.com/questions/23607514

I have done the following two things in my Android app. Firstly, on clicking an edittext I am opening a Datepicker. I click on a date and the date sets on the edittext in the format 12/5/2014. I want to set it in the format like 12 May,2014. how to do this. Secondly, on clicking another edittext I open a TimePicker which on click sets the time on the ediitext in the form 1:30. I want to set it like 1:30 PM. How to do this? I am posting my codes below, please guide me step by step.

DatePicker code

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    // TODO Auto-generated method stub
     myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
}

private void updateLabel() {
    // TODO Auto-generated method stub
    String myFormat = "MM/dd/yy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    datepick.setText(sdf.format(myCalendar.getTime()));
}

   };
 datepick.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
     new DatePickerDialog(getActivity(), date, myCalendar
             .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
             myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
   } );

TimePicker code

tasktime.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
     Calendar mcurrentTime = Calendar.getInstance();
     int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
     int minute = mcurrentTime.get(Calendar.MINUTE);

     TimePickerDialog mTimePicker;
     mTimePicker=new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
             tasktime.setText( hourOfDay + ":" + minute);
        }
    }, hour, minute, true);
     mTimePicker.setTitle("Select Time");
     mTimePicker.show();
}
  });
有帮助吗?

解决方案

Change the format of the date from "MM/dd/yy" to "dd MMM,yyyy" and in the time use simpleDateFomate also with a format "h:mm a"

其他提示

// try this way,hope this will help you....

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
    Time chosenDate = new Time();
    chosenDate.set(dayOfMonth, monthOfYear, year);
    long dt = chosenDate.toMillis(true);
    CharSequence strDate = DateFormat.format(MM/dd/yy", dt);
    datepick.setText(strDate.toString());
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
   Calendar date = Calendar.getInstance();
   Time chosenDate = new Time();
   chosenDate.set(0, minute, hourOfDay, date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.MONTH), date.get(Calendar.YEAR));
   long dt = chosenDate.toMillis(true);
   CharSequence strDate = DateFormat.format("kk:mm", dt);
   tasktime.setText(strDate);
}

Use SimpleDateFormat. Use the method below and provide your date in string format as a parameter

public static String dateFormatter(String date)

    {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");

        String s = "";
        try {
            Date dt = sdf.parse(date);
            sdf = new SimpleDateFormat("dd-MMM-yyyy");
            s = sdf.format(dt);
            // Log.v("*****", s);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return s;
    }

For Time, please refer the link below : Convert time value to format “hh:mm Am/Pm” using Android

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top