• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparing two dates

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everybody
I know this sounds like a simple question, but I could not figure out how to do this.
I have sort of token, which includes a timestamp.
I want to know if that token (timestamp) was created within, lets say 10 hours.
How would I do this?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the SimpleDateFormat class to parse the original token to a Date and compare this with another date set to 10 hours before.
 
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what did you meant by timestamp? is it java.sql.Timestamp?
 
Joris Bolsens
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Supun Lakshan Dissanayake wrote:what did you meant by timestamp? is it java.sql.Timestamp?


I haven't really decided what I want to use as a timestamp, be it a Date() or the java.sql.Timestamp as you said, or something else.
Whichever ends up being easier.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't really given enough information yet. For example, how you intend to store the date. If you use java.util.Date (or ilk), rather than any sort of string format, then it's pretty easy to figure out the duration between any two dates.
 
Joris Bolsens
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You haven't really given enough information yet. For example, how you intend to store the date. If you use java.util.Date (or ilk), rather than any sort of string format, then it's pretty easy to figure out the duration between any two dates.


That's kind of part of the question, what would be the best way to store the date.
and if I do use java.util.Date, I can parse it to/from a string, correct?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joris Bolsens wrote:That's kind of part of the question, what would be the best way to store the date.


Actually, I suspect there's more than one question there:
1. What should your timestamp (which sounds to me like it's a String) look like?
2. How should I store the information in that timestamp in Java?

and if I do use java.util.Date, I can parse it to/from a string, correct?


Correct. Have a look at SimpleDateFormat.

If indeed your timestamp is a String, then I would suggest strongly using ISO 8601, and specifically its "Combined date and time in UTC" form, eg:

2013-08-08T18:34:00Z

It's not pretty, but it IS an international standard, so if you have to send it to anyone else it will be a format that they are familiar with (or they darn well should be ).
It also contains ALL the information necessary to convert unequivocally to a Java Date. If the timezone isn't specified, you have to make assumptions.

HIH

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic