In my app, I am showing time in text view as 07:00 PM. On click of the text view, a time picker dialog pops up, In that time picker, I have to show exactly the same time as what is appearing in textview. But, I am not getting how to do that.
CODE
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm a");
//int hr = 0;
Date date = null;
try
{
date = sdf.parse(resDateArray[3]);
}
catch (ParseException e) {
e.printStackTrace();
}
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//tp is reference variable for time picker
tp.setCurrentHour(calendar.get(Calendar.HOUR));
tp.setCurrentMinute(calendar.get(Calendar.MINUTE));
}//else
I have created this helper function to get time
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
usage of getFormattedDate is:
If you're using the TimePicker nowdays its probably best to check the Build Version as the setCurrentHour and setCurrentMinute has been deprecated.
You should use a
SimpleDateFormat
to get aDate
object from yourString
. Then just calland
Since the
Date
object is deprecated, you should use aCalendar
instead of it. You can instantiate aCalendar
that way :Edit : the complete code
For AM/PM you have to implement a logic for that and that is as below.. so you can get the output as you mentioned ... just try below code...
If anybody using the following format that is without using timepicker widget use this one..