How to sum java.sql.Time ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to sum java.sql.Time ?

How to get total? Time t1 = new Time(08:00:00); Time t2 = new Time(09:00:00); Time total = 17:00:00

9th Sep 2020, 4:23 PM
hamid
hamid - avatar
5 Answers
+ 1
An example how you can add multiple durations. https://code.sololearn.com/cj20iaRbo9qJ/?ref=app
10th Sep 2020, 6:27 PM
Tibor Santa
Tibor Santa - avatar
+ 3
You cannot create time objects like that, your code won't work. Also, don't use java.sql.Time if at all possible. java.time.LocalTime is more modern and has much better methods. If you insist on sql.Time then you can also convert it to LocalTime and back. The following example converts both Time objects to nanoseconds (a long value) to add them, and then convert it back to Time. https://code.sololearn.com/c5TOL75zQBfF/?ref=app
9th Sep 2020, 6:42 PM
Tibor Santa
Tibor Santa - avatar
0
it does not work for me.
10th Sep 2020, 11:31 AM
hamid
hamid - avatar
0
Time time1 = “08:00:00”; Time time2 = “17:00:00”; //time1 and time2 is java.sql.Time error: invalid value for NonaofDay (valid value 0 - 86399 i think it is more than 24 hours and so dose not work. how can get sum multiple times in for loop? ex: //time is java.sql.Time Time time; for(int i=0;i<30;i++){ time += time1.gettime(); }
10th Sep 2020, 11:37 AM
hamid
hamid - avatar
0
In your original example you had 8+9=17 hours so that worked. You cannot add Time objects to each other. It is designed to represent a specific time of day. To work with durarions, I suggest to read this. https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
10th Sep 2020, 12:14 PM
Tibor Santa
Tibor Santa - avatar