Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert timestamp to double

 
Valerie H.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Honour Cook
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getTime() works. Try it yourself.
 
Valerie H.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two ways of doing it:

[ December 12, 2004: Message edited by: Barry Gaunt ]
 
Valerie H.
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic