fork 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.time.format.*;
  9. import java.time.temporal.*;
  10. import java.time.zone.*;
  11. import java.util.concurrent.*;
  12.  
  13. import java.math.*;
  14.  
  15. /* Name of the class has to be "Main" only if the class is public. */
  16. class Ideone
  17. {
  18. public static void main (String[] args) throws java.lang.Exception
  19. {
  20.  
  21. OffsetDateTime epochCalendarNewStyleActOf1750 = LocalDate.of ( 1752 , Month.SEPTEMBER , 14 ).atStartOfDay ().atOffset ( ZoneOffset.UTC );
  22.  
  23. String input = "95906.27600694445";
  24. BigDecimal bd = new BigDecimal ( input );
  25. long days = bd.toBigInteger ().longValue ();
  26. BigDecimal fractionOfADay = bd.subtract ( new BigDecimal ( days ) ); // Extract the fractional number, separate from the integer number.
  27. BigDecimal secondsFractional = new BigDecimal ( TimeUnit.DAYS.toSeconds ( 1 ) ).multiply ( fractionOfADay );
  28. long secondsWhole = secondsFractional.longValue ();
  29. long nanos = secondsFractional.subtract ( new BigDecimal ( secondsWhole ) ).multiply ( new BigDecimal ( 1_000_000_000L ) ).longValue ();
  30. Duration duration = Duration.ofDays ( days ).plusSeconds ( secondsWhole ).plusNanos ( nanos );
  31.  
  32. OffsetDateTime odt = epochCalendarNewStyleActOf1750.plus ( duration );
  33.  
  34. Instant instant = odt.toInstant();
  35.  
  36. System.out.println ( "bd: " + bd );
  37. System.out.println ( "days: " + days );
  38. System.out.println ( "fractionOfADay.toString(): " + fractionOfADay );
  39. System.out.println ( "secondsFractional: " + secondsFractional );
  40. System.out.println ( "secondsWhole: " + secondsWhole );
  41. System.out.println ( "nanos: " + nanos );
  42. System.out.println ( "duration.toString(): " + duration );
  43. System.out.println ( "duration.toDays(): " + duration.toDays () );
  44. System.out.println ( "odt.toString(): " + odt );
  45. System.out.println ( "instant.toString(): " + instant );
  46.  
  47.  
  48. }
  49. }
Success #stdin #stdout 0.13s 4386816KB
stdin
Standard input is empty
stdout
bd: 95906.27600694445
days: 95906
fractionOfADay.toString(): 0.27600694445
secondsFractional: 23847.00000048000
secondsWhole: 23847
nanos: 480
duration.toString(): PT2301750H37M27.00000048S
duration.toDays(): 95906
odt.toString(): 2015-04-15T06:37:27.000000480Z
instant.toString(): 2015-04-15T06:37:27.000000480Z