为什么getTime()出错?我已经尝试了一切,但无法弄清楚问题出在哪里。据我所知,我已经将String arrayOpportunity [2]转换为日期。 (它最初是一个String。)谢谢!

SimpleDateFormat df = new SimpleDateFormat();
df.applyPattern("yyyy-MM-dd HH:mm:ss");

// Calendar timestamp = Calendar.getInstance();
// timestamp.setTime(df.parse(arrayOpportunity2)[0]);

arraySuspects.add(arrayGPS[0]);
// }
// long timediff = coord2.getTimestamp().getTimeInMillis() -
// coord1.getTimestamp().getTimeInMilis();

Date convertedDate = df.parse(arrayOpportunity[2]);

Date duration = df.parse("0000-00-00 00:14:59");
Date lastTime = df.parse(arrayOpportunity[2]);

// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");

System.out.println(arrayOpportunity[2]);

//  arrayOpportunity[2].setTime(arrayOpportunity[2].getTime() + duration);
//  lastTime += duration;
arrayOpportunity[2].setTime(arrayOpportunity[2].getTime() + (((14 * 60) + 59)* 1000));

最佳答案

调用df.parse(arrayOpportunity[2]);不会将arrayOpportunity[2]转换为Date,而是将该值分配给lastTime。在您的代码调用lastTime.getTime()而不是arrayOpportunity[2].getTime()中,因为arrayOpportunity[2]仍然是String

// Create your date parser
SimpleDateFormat df = new SimpleDateFormat();
// Set the date pattern
df.applyPattern("yyyy-MM-dd HH:mm:ss");
// Create a Date object by parsing the value of arrayOpportunity[2]
Date lastTime = df.parse(arrayOpportunity[2]);
// Set a new value to the Date object by performing a calculation on the result of getTime()
lastTime.setTime(lastTime.getTime() + (((14 * 60) + 59)* 1000));

10-06 05:42