fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.*;
  8. import java.math.BigDecimal;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15.  
  16. String input = "08:30:00" ;
  17. LocalTime lt = LocalTime.parse ( input );
  18. Duration d = Duration.between ( LocalTime.MIN , lt );
  19.  
  20. System.out.println( "input: " + input );
  21. System.out.println ( "d.toString(): " + d );
  22.  
  23. // I do *not* recommend representing date-time values a decimal numbers, such as `8.5` in this example.
  24. // Instead, use java.time objects in your code and standard ISO 8601 format for text `PnYnMnDTnHnMnS`.
  25. // But if you insist on a decimal fraction, use the `BigDecimal` class to maintain accuracy instead of the floating-point types.
  26. BigDecimal minutesPerHour = new BigDecimal ( 60L ); // Use var/constant for clarity of your intent.
  27. BigDecimal minutes = new BigDecimal ( d.toMinutes () );
  28. BigDecimal fractionalHours = minutes.divide ( minutesPerHour );
  29.  
  30. System.out.println ( "frationalHours.toString(): " + fractionalHours );
  31.  
  32. }
  33. }
Success #stdin #stdout 0.09s 711680KB
stdin
Standard input is empty
stdout
input: 08:30:00
d.toString(): PT8H30M
frationalHours.toString(): 8.5