* This method takes a formatted string and turns it into a calendar. This
* approach is trimmed for performance, using high-speed substring access to
* the string rather than dumb parsing
*
* @param date
* the String to read the date from
* @return a calendar representing the date found in the string
*/
private static Calendar getStringToCal(String date) {
final String year = date.substring(0, 4);
final String month = date.substring(5, 7);
final String day = date.substring(8, 10);
final String hour = date.substring(11, 13);
final String minute = date.substring(14, 16);
final String second = date.substring(17, 19);
final int millisecond = Integer.valueOf(date.substring(20, 23));
Calendar result =
new GregorianCalendar(Integer.valueOf(year),
Integer.valueOf(month) - 1, Integer.valueOf(day),
Integer.valueOf(hour), Integer.valueOf(minute),
Integer.valueOf(second));
result.set(Calendar.MILLISECOND, millisecond);
result.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
return result;
}
如參數是2012-06-26T08:25:49.670,
try {
Calendar cal=getStringToCal(time);
date = new Date(cal.getTimeInMillis());
} catch (Exception e) {
e = null;
}
date爲轉換後的時間