0

I need to call one API which having payload

JSON
{
timeAsString  :String
}

it is given in the document

timeAsString  (int64 (https://developers.google.com/discovery/v1/type-format) format) Timestamp when abc is happen

For that i converted date into milliseconds and send as a string

new Date().getTime().toString();

but it gives me error as invalid argument please let us know where i am wrong. how can we send date in string int64 format?

Saakshi Aggarwal
  • 528
  • 1
  • 11
  • 26

1 Answers1

3

java.time

First, please consider using java.time, the modern Java date and time API. The Date class that you are using is long outdated and poorly designed. The modern API is much nicer to work with.

    String timeAsString = String.valueOf(Instant.now().toEpochMilli());

This just gave:

1512197790622

Another option is String.valueOf(System.currentTimeMillis()).

Q&A

How do I learn to use java.time? You can start out from the Oracle tutorial and/or find other tutorials and other resources on the net.

Can I use the modern API with my Java version?

If using at least Java 6, you can.

What was wrong with my code? Date.getTime() returns the milliseconds as a primitive long. In Java you cannot call any methods on a primitive. In my Eclipse I got the error message ”Cannot invoke toString() on the primitive type long“. One solution is to use String.valueOf() as I did in my code above.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161