Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

To calculate Time diffrence  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a timestamp as a result of executing a query. EG:2006-01-09 15:36:26.
I need to compute the diffrence between the current time and this timestamp. The function should return false if the difference is more than 30 minutes.
 
Java Cowboy
Sheriff
Posts: 16083
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you need to check if the timestamp is between a certain interval: [now - 30 minutes, now + 30 minutes]. Right? Or is it [now, now + 30 minutes]?

You could do it like this:

- Do you get the timestamp as a String? Then you have to parse it into a Calendar object first. Use java.text.SimpleDateFormat to do that (see the API documentation).

- Next, create two Calendar objects, one for the lower boundary of the interval, one for the upper boundary.

- Finally, use the methods before() and after() of class Calendar to check whether the timestamp is in the interval.

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just parse the two strings as Dates & see if they differ by more than the number of milliseconds you're interested in.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by chance you have available the actual time in milliseconds I'd see if the difference is greater than 30*60*1000. If you just have a timestamp available in that format which you need to parse I'd go with what's already mentioned.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!