Time format in Java
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 !
Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Output: 12:47:48
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks a lot for the quick response. Will apply this logic...
@ Tony
Yes Tony, your point should be taken into consideration. Thanks for reminding !
Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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).
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 !
Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
i see in your code something like "String d3 = format.format(date1); " why?
Rob mentioned clearly . please follow the exact steps
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 !
Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
i know it is workaround, you can try this
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Thanks & Regards
Swapna Gouri
I "try", I "catch" and "finally" WIN !!!
| Popeye has his spinach. I have this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |












