Reputation: 5552
I've a Java class User in my project and it has 2 attributes id
and created
.
User.java
import java.sql.Timestamp;
public class User {
private int id;
private Timestamp created;
public User (int id) {
this.id = id;
created = new Timestamp(System.currentTimeMillis());
}
// getters and setters...
}
Now I need to generate a JSON object representing an instance of User
. So I use the org.json.JSONObject to do it :
User user = new User(1);
JSONObject json = new JSONObject(user);
System.out.println(json.toString());
Here's the result.
{"id":1,"created":"2016-03-02 19:08:00.0"}
However, I want a format in Long, which is like
{"id":1,"created":1456945720000}
I think I might be able to change it by modifying the property in JSONObject, but I want to know if there's a better way.
Upvotes: 1
Views: 2589
Reputation: 1907
Use .getTime()
to convert in to long.
If you are getting an object in variable "created" then convert it into long :
created.getTime();
Upvotes: 2