I am making an application in which i am geting a string in this format
2014-11-17 13:41:25
But what i want is only this
13:41
I tried doin this but i got forcse close
String test = new SimpleDateFormat("kk:mm").format(data.getStringExtra("TIME"));
etStartTime.setText(test);
LogCat :
11-17 14:44:07.717: E/AndroidRuntime(14287): FATAL EXCEPTION: main
11-17 14:44:07.717: E/AndroidRuntime(14287): java.lang.IllegalArgumentException: Bad class: class java.lang.String
11-17 14:44:07.717: E/AndroidRuntime(14287): at java.text.DateFormat.format(DateFormat.java:359)
11-17 14:44:07.717: E/AndroidRuntime(14287): at java.text.Format.format(Format.java:93)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.megasys.im3_new.workorder.TimeCard.onActivityResult(TimeCard.java:318)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.megasys.im3_new.workorder.TimeCard$TimePickerFragment.onTimeSet(TimeCard.java:383)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:130)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.app.TimePickerDialog.onClick(TimePickerDialog.java:115)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.os.Handler.dispatchMessage(Handler.java:107)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.os.Looper.loop(Looper.java:194)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.app.ActivityThread.main(ActivityThread.java:5370)
11-17 14:44:07.717: E/AndroidRuntime(14287): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 14:44:07.717: E/AndroidRuntime(14287): at java.lang.reflect.Method.invoke(Method.java:525)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-17 14:44:07.717: E/AndroidRuntime(14287): at dalvik.system.NativeStart.main(Native Method)
what i have done
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(time);
Date testDate = null;
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-mm-dd HH:MM");
try {
testDate = sdf.parse(time);
} catch(Exception ex) {
ex.printStackTrace();
}
etStartTime.setText("" + testDate);
}
} catch(Exception ex) {
ex.printStackTrace();
}
This is what i had done ,but still am getting errors.
The time i am getting from intent
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
public int hour;
public int min;
public String time;
Calendar c;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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) {
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("yyyy-MM-dd HH:mm:ss").format(date.getTime());
Intent intent = new Intent();
intent.putExtra("TIME", time);
getTargetFragment().onActivityResult(getTargetRequestCode(), 1, intent);
getDialog().hide();
}
public String onTimeSelected() {
return time;
}
}
This will work. I just tried it now.
Try with below code:
I didn't understand why you're formatting the date as you are already having hour and minutes.
You just need to use this for getting 24 hour TimeFormat
While to send data back to the activity you can use
Handler
Try with Following code
Handler in
FragmentActivity
MainActivity.java :
TimePickerDialogFragment.java :
Use this to format the date: