Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I calculate the no.of.days. between two timestamps?

 
Mike Jeya
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a timestamp which is in the form of String(retrieved from LDAP)
for example :20041118120738Z
I have to find the no.of.days different between this timestamp and currenttimestamp.
if I go for, System.currenttimeMillis() to get CurrentTime, is this locale depenedent??
Can any one assist me?
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can convert your time stamp to a java.util.Date object using the SimpleDateFormat class. Once you have the Date object, you can convert that to the milliseconds representation by calling the getTime() method.

Subtract that from the current time, and you'll have the number of milliseconds between the two time stamps. This is easily converted to days.

As for the locale question - System.currentTimeMillis() will return whatever time is reported by the system clock on the machine where your program is running. So, yes, it is locale dependent - if you run the same program at the same time on two different machines in different time zones, you will get different results.
[ November 29, 2004: Message edited by: Fletcher Estes ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic