tags:

views:

167

answers:

1

While reading a date column from a .csv file which is of 'dd/mm/yyyy hh:mi:ss am' format through javacode. I declared the variable as Long in javacode. how to parse in the javacode.

+4  A: 
SimpleDateFormat df1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
Date d = df1.parse(inputString);
long timeInMillis = d.getTime();

Java API reference: http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html

ar
For complete answer post link to API javadoc .
St.Shadow
Better, but why 1.4, but not 1.6?
St.Shadow
status.setStateChange((Date) df1.parse(results.getString("STATECHANGE")));It is getting the result from a .csv file where the format of the string is dd/mm/yyyy hh:mm:ss am/pm . But at the above point of code still it is showing the cast error
jyothi
Can you post up a code snippet?
ar
@jyothi: Do not import `java.sql.Date`. Import `java.util.Date`. Read the linked Java API reference for more details what exactly `parse()` method returns.
BalusC
Date stateChange = null ; public void setStateChange(Date stateChange) { this.stateChange = stateChange; } while reading from the result set:(Result Got the result from a csv file where Statechange is a date column of format mm/dd/yyyy hh:mi:ss am. status.setStateChange((Date) sdf.parse(results.getString("STATECHANGE"))); It is showing a cast error. at the above statement
jyothi
As BalusC mentioned above, there are two Date classes, one in java.util and another in java.sql.Check to see if you are importing java.sql.* as this would cause the problem that you are seeing. You need to ensure that you are using java.util.Date.
ar