I have created this helper function to get time
public static void showTime(final Context context, final TextView textView) {
final Calendar myCalendar = Calendar.getInstance();
TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String am_pm = "";
myCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
myCalendar.set(Calendar.MINUTE, minute);
if (myCalendar.get(Calendar.AM_PM) == Calendar.AM)
am_pm = "AM";
else if (myCalendar.get(Calendar.AM_PM) == Calendar.PM)
am_pm = "PM";
String strHrsToShow = (myCalendar.get(Calendar.HOUR) == 0) ? "12" : myCalendar.get(Calendar.HOUR) + "";
//UIHelper.showLongToastInCenter(context, strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);
textView.setText(strHrsToShow + ":" + myCalendar.get(Calendar.MINUTE) + " " + am_pm);
}
};
new TimePickerDialog(context, mTimeSetListener, myCalendar.get(Calendar.HOUR), myCalendar.get(Calendar.MINUTE), false).show();
}
and How to get current time in required format, I use following code.
// PARAM Date is date of time you want to format
// PARAM currentFormat of date
// PARAM requiredFormat of date
public static String getFormattedDate(String date, String currentFormate, String requiredFormate) {
//SimpleDateFormat formatActual = new SimpleDateFormat("yyyy - MM - dd");
if (date != null) {
SimpleDateFormat formatActual = new SimpleDateFormat(currentFormate);
Date dateA = null;
try {
dateA = formatActual.parse(date);
System.out.println(dateA);
} catch (ParseException e) {
e.printStackTrace();
}
//SimpleDateFormat formatter = new SimpleDateFormat("dd MMM, yyyy");
SimpleDateFormat formatter = new SimpleDateFormat(requiredFormate);
String format = formatter.format(dateA);
System.out.println(format);
return format;
} else {
return "";
}
}
usage of getFormattedDate is:
String date = getFormattedDate(dateYouWantToFormate,"hh:mm a","hh:mm");