| Author |
convert timestamp to double
|
Valerie H.
Greenhorn
Joined: Dec 11, 2004
Posts: 6
|
|
Hello. I apologize in advance if my topic is too advanced for this forum. I am completely new to java but my professor has just sprung an advanced java program on us. I need to convert a timestamp to a double. The code I created is below: String tstamp; double y; tstamp = new Date().toString(); y = Double.parseDouble(tstamp); When I run this, I get the error message: java.lang.NumberFormatException: For input string: "Sun Dec 12 01:24:20 EST 2004" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1207) at java.lang.Double.parseDouble(Double.java:220) I have gone through my books and searched the internet but everything I try gives me the same error message. Any help would be appreciated- even an article link that describes how to do this. Thanks. Valerie
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
From the Java 1.5.0 API spec for class Date:
getTime public long getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. Returns: the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.
|
Mike Gershman
SCJP 1.4, SCWCD in process
|
 |
Honour Cook
Greenhorn
Joined: Nov 01, 2004
Posts: 9
|
|
|
I dont think it is possible to convert a Date to double. In order to convert a string to a numeric type, the string must have appropriate format, such as "5" or "0.5".
|
 |
Mike Gershman
Ranch Hand
Joined: Mar 13, 2004
Posts: 1272
|
|
|
getTime() works. Try it yourself.
|
 |
Valerie H.
Greenhorn
Joined: Dec 11, 2004
Posts: 6
|
|
Mike, Thanks for your suggestion. I have been trying to implement it but can't seem to code it properly. Do I use getTime() by itself or do I use getTime to convert my date? tstamp = new getTime().toGMTString(); or tstamp = new Date().toString(); tstamp = tstamp.getTime(); Thanks, Valerie
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Two ways of doing it: [ December 12, 2004: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Valerie H.
Greenhorn
Joined: Dec 11, 2004
Posts: 6
|
|
Barry, Thanks for your help. What data type is nowMilliseconds? Date now2 = new Date(); nowMilliseconds = now2.getTime(); System.out.println(nowMilliseconds); Valerie
|
 |
Valerie H.
Greenhorn
Joined: Dec 11, 2004
Posts: 6
|
|
Barry- Never mind. I think I have just been so frustrated, I miss the obvious when it is staring me in the face. Thanks again everyone for your help. Valerie
|
 |
 |
|
|
subject: convert timestamp to double
|
|
|