Reputation: 1036
I have an array of Strings like this:
private String time[] = {"8:00:00 AM", "8:00:00 PM"};
The array of strings is then displayed in a JComboBox
.
My question is: How can I convert/cast/parse the selected String into a Time object whilst still keeping AM and PM times distinct? Would it be best to simply use 24 hour times?
Thanks in advance for any help/advice/guidance.
Upvotes: 4
Views: 10927
Reputation: 66667
Here is sample code: You need to iterate through your array and populate yourString value.
SimpleDateFromat dt = new SimpleDateFormat("hh:mm:ss a");
Date dt2 = dt.parse(yourString);
Here is a link for Simple date formater
Upvotes: 2
Reputation: 94653
Use SimpleDateFormat
class methods.
String timeStr="8:00:00 AM";
SimpleDateFormat df=new SimpleDateFormat("hh:mm:ss a");
Date dt=df.parse(timeStr);
Upvotes: 2
Reputation: 51030
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
List<Date> dates = new ArrayList<Date>();
for(String str : time) {
dates.add(sdf.parse(str));
}
Upvotes: 2