I'm trying to write a method to return a Time in util.Date to a string. I am getting '0' when i am trying to return whether the time is in AM or PM and the minutes are not displaying the 0 in front. I am trying to return the time in the format of HH:mm am_pm
Here is my method:
public String getTime() {
Calendar cal = Calendar.getInstance();
int hr = cal.get(Calendar.HOUR);
int min = cal.get(Calendar.MINUTE);
return String.valueOf(hr) + ":" + String.valueOf(min) + " " + cal.get(Calendar.AM_PM);
}
Input: 2:00 (HH:mm SimpleDateFormat in a JFormattedTextField)
Output: 2:0 0
EDIT: tried using the SimpleDateFormat but i am getting a type mistmatch error
public String getTime()
{
SimpleDateFormat format = new SimpleDateFormat("HH:mm aa");
date = format.toString().toString();
return date;
}