• 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

Time format in Java

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers
I have a program where I want to subtract 2 time formats, say (24:00:00 - 12:12:12). Of which one of the time is being fetched from the database. Which data type should I use for this type of calculations. I have tried using double data type but the format of HH:MM:SS is not supported by double and am getting a NumberFormatException. If I try to use String data type then I'm not able to perform the subtraction. The database am using here is MySQL and I cannot change the time format to some other as the other types also are in HH:MM:SS format.

How can I overcome this problem? Can anyone suggest me some way out? Your help would be much appreciated !

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.util.Date comes to mind:

Output: 12:47:48
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robs solution works fine for the times given and may well be all you need but you should check to make sure all time difference calculations will be for simple time differences regardless of dates. The problem is potentially significantly more complex if, for example, the locale supports daylight saving and you have to find the difference between 00:15 and 03:30 on a daylight saving change-over day when there will be an extra (or missing) hour to contend with.
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob
Thanks a lot for the quick response. Will apply this logic...

@ Tony
Yes Tony, your point should be taken into consideration. Thanks for reminding !
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob
I have applied the logic you have suggested, but am facing a problem now. I'm not getting the desired output. Say for example: 24:00:00 - 05:05:05 = 18:54:55. But the answer that am getting is 00:08:995. Not sure how it's getting calculated. Here's my program:

Here, Idle time is being fetched from the database. Before formatting, am able to view something like a FULL date format, Tuesday, April 12, 1969. What I actually want is difference in timings, irrespective of dates. How can I achieve this? And why am I not getting the expected result?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MM means the month. For the minutes you really must use lowercase m's.

As for the dates, if you create both dates using the same date-less date format it should give them the same date. The example code I wrote earlier nicely puts the dates at January 1st 1970 (January 2nd for 24:00:00).
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob
I'm facing problems in calculating the date difference. Here is the code snippet:


All that I want is the difference of d3 and d4, which are parsed time variables(say 23:59:59). As you have said earlier, if I use getTime() then am not getting the desired output. Now, if I'm parsing String to Double, due to colon(hh:mm:ss), am getting a NumberFormatException. And if I try to take both d3 and d4 as Strings, then am not able to perform subtraction.

I have also tried using some fields in DateFormat class like HOUR_OF_DAY0_FIELD etc to get the hour alone, so that I can subtract the hrs. But still not getting desired output. I have seen add method in Calendar class, thought it might help me. But am not able to use format method in Calendar class.

How should I move forward from here? Kindly suggest me a way out !
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi swapna,

i see in your code something like "String d3 = format.format(date1); " why?

Rob mentioned clearly . please follow the exact steps
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Seetharaman
I have tried what Rob had said prior. Here is the code and the output of it:

Output:

I actually don't need the date, I only need the time, like 23:59:59. That's the reason why I used format() again. The output should be something like (23:59:59) - (05:05:05) = 18:54:54. But am getting it as 00:24:54. Not sure why am getting this and where the error is. Would really appreciate any help !
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the exact value of machit, before you parse it?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

i know it is workaround, you can try this
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you SeethaRaman ! Sorry for the delayed response, was struck with some other error. Will let you know how it works.
 
Swapna Gouri Kalanidhi
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a bunch Seetharaman ! Your work around is working perfectly. This was what I exactly wanted. Can I know how you got to know that we can get hours, minutes and seconds by such calculation? Am eager to know how it all worked. I just wish to know how getTime() method works and how we can calculate or extract the exact time from it. Is there any rule or method for calculating? I understood how you were calculating but I want to know why you did that calculation.

Anyways, thanks a lot again !

Thanks Rob ! This was a very big puzzle for me and without your help, I wouldn't have solved this problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic