convert array objects into time
If i have an Array which contains the Strings 07:开发者_Go百科46:30 pm , 10:45:28 pm , 07:23:39 pm , .......
and I want to convert it into Time. How can i do this?
Here's how to convert an array of Strings in the given format to an array of dates:
public static Date[] toDateArray(final String[] dateStrings)
throws ParseException{
final Date[] arr = new Date[dateStrings.length];
int pos = 0;
final DateFormat df = new SimpleDateFormat("K:mm:ss a");
for(final String input : dateStrings){
arr[pos++] = df.parse(input);
}
return arr;
}
Use the SimpleDateFormat class. Here is an example:
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
for(String str : array){
Date date = formatter.parse(str);
}
Use the SimpleDateFormat class to parse the date.
You need to write your own parser. See this example:
http://www.kodejava.org/examples/101.html
If you want a array or list of time...
String[] arrString = new String[] { "07:46:30 pm", "10:45:28 pm", "07:23:39 pm" }
Time[] arrTime = new Time[strArray.lengh];
or
List<Time> listTime = new ArrayList<Time>();
Array string to array or list time:
//For array
for (int i = 0; i < arrString.length; i++) {
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Date date = formatter.parse(arrString[i]);
//Populate the ARRAY
arrTime[i] = new Time(date.getTime());
}
//For list
for (String str : arrString) {
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Date date = formatter.parse(str);
//Populate the LIST
listTime.add(new Time(date.getTime()));
}
精彩评论