简体   繁体   中英

Convert String into Time

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.

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

Use SimpleDateFormat class methods.

String timeStr="8:00:00 AM"; 
SimpleDateFormat df=new SimpleDateFormat("hh:mm:ss a");
Date dt=df.parse(timeStr);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
List<Date> dates = new ArrayList<Date>();
for(String str : time) {
    dates.add(sdf.parse(str));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM