• Post Reply Bookmark Topic Watch Topic
  • New Topic

calculate the seconds  RSS feed

 
Per Lovdinger
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to calculate the exact number of seconds between 2 times
in this format: String yyyymmddmmss.
public int secondsBeteen(String fromTime,String toTime)

Anyone that have a good approach for this ?

thanks in advance
Per
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you have to use DateFormat or better SimpleDateFormat to be able to parse your input and to get a Date. This Date you can put into a Calendar, which has the function getTimeInMillis(). This you have to do for both date values.
It is easy to substract both millis and to calculate seconds from it.
This sounds a bit strange, but in fact it will be not more than 20 lines of code or maybe less. Even much better, than calculating all by yourself.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!