0

I'm writing a program in Java that reads in a JSON object which looks like this:

{  
  "items_count":20,
  "items":[  
    {  
      "type":"User",
      "id":1234,
      "username":"user1",
      "has_url":false,
      "record_count":1,
      "created_at":"12 hours ago"
    },
    {  
      "type":"User",
      "id":5678,
      "username":"user2",
      "has_url":false,
      "record_count":1,
      "created_at":"12 hours ago"
    }
  ]
}

Using I'm able to use a JSON parser to get the data.

My problem is that all the created_at dates are relative,i've seen that Java used to have RelativeTime and AbsoluteTime objects, but neither seems to be able to turn a relative string into an absolute time.

shachna
  • 107
  • 1
  • 5
  • What is the point of time these are relative to? – RealSkeptic Apr 14 '15 at 20:52
  • @RealSkeptic the time the query is made. – shachna Apr 14 '15 at 21:05
  • But do you have any indication of what that time is? Otherwise, how do you expect to change the relative time into absolute time? – RealSkeptic Apr 14 '15 at 21:07
  • @RealSkeptic Yes. The "program" I built is intended for use immediately after querying the database (through a bit of a hack) which returns these times relative to the time I query. – shachna Apr 14 '15 at 21:31
  • Seems like you'll need to use an existing library (or create your own) that translates word-based time differences into values you can do math with. Have a look at [PrettyTime](http://stackoverflow.com/questions/3859288/how-to-calculate-time-ago-in-java). Also I think Rails' ActiveSupport provides this functionality so you could look at how that works and use that code as a template. See http://stackoverflow.com/questions/6420371/3-days-ago-2-hours-from-now-etc-without-rails, too. – eebbesen Apr 14 '15 at 23:56
  • all time is relative ... I thought we all learned that in grade school! –  Apr 15 '15 at 00:39
  • Seriously all timestamp-ish data should be stored and transmitted in full 24 hour `ISO8601` format `UTC` only; to allow the client system to do whatever localization/conversion it needs. What you are doing is extremely poor in practice. –  Apr 15 '15 at 00:43

0 Answers0