无法添加时间戳

时间:2012-10-24 15:15:46

标签: java timestamp add

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

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));

1 个答案:

答案 0 :(得分:1)

调用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));