1

I have a form which calculates hours and mins between to times. I would like to convert result to value for calculating like 1hour 30 mins is result 1,5. Now I am getting result 1,3. I need to calculate at the moment and write it.

I found some converters but it doesnt calculate at the moment so it is not good for what I need.

I also want to user see hours and mins like my code shows. I know I could only split seconds with 0,6 but then show in select option 0,25 instead 0,15. Thats not an option.

  • Ura pričetka - start time,
  • Ura končanja - end time,
  • Nadure - it is just calculating the value over a certain value,
  • Ure - result which have to be calculated and converted at the moment

I am calculating in java.

Code:

 <td align="left">Ure </td>
            <td align="left"><input name="ure" id="ure" type="text" onchange="updatesum()"/></td>
          </tr>
          <tr>
            <th align="right">Ura pričetka </th>
            <td align="left"><select name="urap" id="urap" type= "time" onChange="updatesum()" value="9.00"/>
            <OPTION>
            <?php for($hours=0; $hours<24; $hours++) // the interval for hours is '1'
    for($mins=0; $mins<60; $mins+=15) // the interval for mins is '15'

        echo '<option>'.str_pad($hours,2,'0',STR_PAD_LEFT).'.'
                       .str_pad($mins,2,'0',STR_PAD_LEFT).'
                       </option>'; ?></OPTION></td>
            <td align="left">Nadure </td>
            <td align="left"><input name="nadure" id="nadure" type="text" onchange="updatesum()"  /></td>
          </tr>

          <tr>
            <th align="right">Ura končanja </th>

            <td align="left"><select name="urak" id="urak" type= "time" onChange="updatesum()" value=""/>

            <OPTION>

            <?php for($hours=0; $hours<24; $hours++) // the interval for hours is '1'
    for($mins=0; $mins<60; $mins+=15)  
     // the interval for mins is '15'

        echo '<option>'.str_pad($hours,2,'0',STR_PAD_LEFT).'.'
                       .str_pad($mins ,2,'0',STR_PAD_RIGHT).'
                       </option>';                    
                       ?>
            </OPTION>     
            </td> 




       <script type="text/javascript">

       function updatesum() {


document.loginForm.ures.value = (document.loginForm.urak.value -0) - (document.loginForm.urap.value -0);
$ures = loginForm.ures.value;
if (loginForm.ures.value > <?php echo $test ?>) {
  loginForm.ure.value ='<?php echo $test ?>'
  loginForm.nadure.value = (document.loginForm.ures.value -0) - (document.loginForm.ure.value -0);
}

if (loginForm.ures.value <= <?php echo $test ?>) {
  loginForm.ure.value = loginForm.ures.value
  loginForm.nadure.value = (document.loginForm.ures.value -0) - (document.loginForm.ure.value -0);
}

};

</script>

            <input type="text" name="ures" onChange="updatesum()"/>
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 3
    What on Earth does Java have to do with your question? – Tim Biegeleisen Nov 30 '18 at 09:06
  • I am calculating in java. I am not real expert so sorry if I make any mistake. – Mitja Cobec Nov 30 '18 at 09:11
  • I’m not sure whether your PHP code is helpful or not. You may want to either convey the important information from it in a shorter form, or if you think it’s worth including, then at least indent nicely so we can read it without too much strain. – Ole V.V. Nov 30 '18 at 10:36
  • By the way, decimal fractions is a poor way to represent a span of time. Consider using text per the [ISO 8601 standard for durations](https://en.wikipedia.org/wiki/ISO_8601#Durations). The *java.time* classes `Period` and `Duration` use these standard formats by default when parsing/generating strings. – Basil Bourque Nov 30 '18 at 18:01
  • @BasilBourque For the sake of completeness only, according to your link “The smallest value used may also have a decimal fraction, as in "P0.5Y" to indicate half a year.” So `PT1,5H` is accepted as one and a half hours in ISO 8601 (though the `Duration` class of Java cannot parse nor generate it). I’ll let the asker decide whether fractional hours are really required in his/her web app. – Ole V.V. Dec 01 '18 at 09:05

2 Answers2

1

One suggestion could be:

    String startTimeString = "08:30";
    String endTimeString = "10:00";

    LocalTime startTime = LocalTime.parse(startTimeString);
    LocalTime endTime = LocalTime.parse(endTimeString);
    long totalMinutes = ChronoUnit.MINUTES.between(startTime, endTime);
    double hours = (double) totalMinutes / (double) Duration.ofHours(1).toMinutes();
    System.out.println("Hours between start and end: " + hours);

Output from this snippet is:

Hours between start and end: 1.5

Can I use this on Android?

A duplicate question asked for this calculation on Android. It works nicely on older and newer Android devices.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

To convert minutes to an hours decimal you would simply divide the minutes by 60

e.g 1:30 = 1 + (30/60) = 1.5

or :15 = 15/60 = 0.25

public static double methodName(int hours, int minutes) {
   return hours + (minutes / 60);
}
AleksW
  • 703
  • 3
  • 12
  • I know that as I mentoned in my post. But how can I do this in java so it will calculate in my result. Just cant find right way to do this in my code. – Mitja Cobec Nov 30 '18 at 09:09
  • Create a method which takes hours and minutes, and returns the result of that calculation – AleksW Nov 30 '18 at 09:13