Gp2mv3
Gp2mv3

Reputation: 1451

How to pass Date between PHP and Java?

I'm building a small webservice and the client is an android client. I must use dates and I want to save it in my database.

The server side is in PHP and the client is in Java. What format is the best to send date to PHP and how can I convert it easily ?

EDIT: When i say format, i'm not talking of the data format. Just the format of the date. Because the data are sent with JSON but the dates are passed as string.

I was thinking about the timestamp but it's not a good idea since we know its size limit...

Thanks.

Upvotes: 2

Views: 1911

Answers (3)

paulsm4
paulsm4

Reputation: 121871

@Gp2mv3 -

  1. jmort253 is correct: JSON is an excellent data interchange format. Go for it!

  2. You can use getTimeStamp()" on the PHP side

  3. You can convert a PHP timestamp to a Java long timestamp as follows:

    Date d=new Date((long)phpTimeStamp*1000);

PS: If you use timestamps, you must never assume the client and the server are in any way synchronized with each other.

Upvotes: 3

jamesmortensen
jamesmortensen

Reputation: 34078

The main advantage of RESTful webservices is that you can easily send data from one application to another, using a common transport that both will understand.

Both XML and JSON have been used to facilitate communication between different applications, even if they're both written in different languages.

While XML is a great solution, JSON has been gaining traction as a strong option due to it's simplicity and out-of-the-box compatibility with JavaScript.

In Java, you could use a library called Jackson, which will serialize your classes into JSON and deserialize JSON strings back into Java classes.

Also, I'm not sure what you have against timestamps. Since they are long values, they should be really easy to work with in terms of date comparisons, and they should be easy to store since they're typically long values.

Upvotes: 2

Anurag Ramdasan
Anurag Ramdasan

Reputation: 4340

the easiest and most common way to store is using timestamps.

you can anytime change this timestamp into the required format using DateFormat. i hope that helps.

Upvotes: 0

Related Questions