如果我有数组,其中包含 07:46:30 pm,10:45:28 pm,07:23:39 pm ,... ....
我希望将其转换为时间。我怎么能这样做?
答案 0 :(得分:4)
以下是如何将给定格式的字符串数组转换为日期数组:
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;
}
答案 1 :(得分:3)
使用SimpleDateFormat类。这是一个例子:
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
for(String str : array){
Date date = formatter.parse(str);
}
答案 2 :(得分:2)
使用SimpleDateFormat类来解析日期。
答案 3 :(得分:1)
您需要编写自己的解析器。见这个例子:
答案 4 :(得分:1)
如果你想要一个数组或时间列表......
String[] arrString = new String[] { "07:46:30 pm", "10:45:28 pm", "07:23:39 pm" }
Time[] arrTime = new Time[strArray.lengh];
或
List<Time> listTime = new ArrayList<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()));
}