user3917131 user3917131 - 4 years ago 194
Android Question

Convert time (hh:mm) to (yyyy-MM-dd HH:mm:ss)

I have used time picker to select time.Now what i want is that i want to compare two time.
Code for time picker.

public class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {
public int hour;
public int min;
public String time;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();

hour = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);

// Create a new instance of DatePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, min, true);

}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
time = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);

Intent intent = new Intent();
intent.putExtra("TIME", time);

getTargetFragment().onActivityResult(getTargetRequestCode(), 1,
intent);
getDialog().hide();

}

public String onTimeSelected() {
return time;
}

}


Now in intent i am geting time in thuis format("22.00").To compare time i want time in this format("yyyy-MM-dd HH:mm:ss").
So how do i convert("22:00") to ("yyyy-MM-dd HH:mm:ss")????

What i did

try {
String time = data.getStringExtra("TIME");
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date timeCompare;
timeCompare = sdf.parse(time);
Date currentTime = new Date();
int comareResult = timeCompare.compareTo(currentTime);

if (comareResult == -1) {
time = new SimpleDateFormat(" HH:mm").format(date.getTime());
etStartTime.setText(time);
} else {
Toast.makeText(mContext, "Your cannot select future time",
Toast.LENGTH_SHORT).show();
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Answer Source

Change this code,

@Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            time = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);

            Intent intent = new Intent();
            intent.putExtra("TIME", time);

            getTargetFragment().onActivityResult(getTargetRequestCode(), 1,
                    intent);
            getDialog().hide();

        }

by

 @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            Calendar date = Calendar.getInstance();
                date.set(Calendar.HOUR_OF_DAY, hourOfDay);
                date.set(Calendar.MINUTE, minute);
                date.set(Calendar.AM_PM, date.get(Calendar.AM_PM));

                String time = new SimpleDateFormat("HH:mm").format(date.getTime());
             String time1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date.getTime());

            Intent intent = new Intent();
            intent.putExtra("TIME", time);

            getTargetFragment().onActivityResult(getTargetRequestCode(), 1,
                    intent);
            getDialog().hide();

        }

hope it will help you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download