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.time.temporal.*;
  9. import java.time.format.*;
  10. import java.math.BigDecimal;
  11. import java.math.RoundingMode;
  12. import java.util.Locale;
  13. import java.util.concurrent.TimeUnit;
  14.  
  15.  
  16. /* Name of the class has to be "Main" only if the class is public. */
  17. class Ideone {
  18.  
  19. public static void main ( String[] args ) throws java.lang.Exception {
  20.  
  21. Ideone app = new Ideone ( );
  22. app.doIt ( );
  23. }
  24.  
  25. public void doIt ( ) {
  26.  
  27. System.out.println ( "running doIt at: " + ZonedDateTime.now ( ) );
  28. BigDecimal input = new BigDecimal ( "57831.5" );
  29. Instant instant = this.convertModifiedJulianDaysToInstant ( input );
  30. BigDecimal output = this.convertInstantToModifiedJulianDays ( instant );
  31.  
  32. System.out.println ( "input.toString(): " + input );
  33. System.out.println ( "instant.toString(): " + instant );
  34. System.out.println ( "output.toString(): " + output );
  35.  
  36. }
  37.  
  38. public Instant convertModifiedJulianDaysToInstant ( BigDecimal modJulDays ) {
  39. Instant epoch = OffsetDateTime.of ( 1858, 11, 17, 0, 0, 0, 0, ZoneOffset.UTC ).toInstant ( ); // TODO: Make into a constant to optimize.
  40. long days = modJulDays.toBigInteger ( ).longValue ( );
  41. BigDecimal fractionOfADay = modJulDays.subtract ( new BigDecimal ( days ) ); // Extract the fractional number, separate from the integer number.
  42. BigDecimal secondsFractional = new BigDecimal ( TimeUnit.DAYS.toSeconds ( 1 ) ).multiply ( fractionOfADay );
  43. long secondsWhole = secondsFractional.longValue ( );
  44. long nanos = secondsFractional.subtract ( new BigDecimal ( secondsWhole ) ).multiply ( new BigDecimal ( 1_000_000_000L ) ).longValue ( );
  45. Duration duration = Duration.ofDays ( days ).plusSeconds ( secondsWhole ).plusNanos ( nanos );
  46. Instant instant = epoch.plus ( duration );
  47. return instant;
  48. }
  49.  
  50. public BigDecimal convertInstantToModifiedJulianDays ( Instant instant ) {
  51. Instant epoch = OffsetDateTime.of ( 1858, 11, 17, 0, 0, 0, 0, ZoneOffset.UTC ).toInstant ( ); // TODO: Make into a constant to optimize.
  52. Duration duration = Duration.between ( epoch, instant );
  53. long wholeDays = duration.toDays ( );
  54. Duration durationRemainder = duration.minusDays ( wholeDays );
  55. System.out.println ( "durationRemainder: " + durationRemainder );
  56.  
  57. BigDecimal wholeDaysBd = new BigDecimal ( wholeDays );
  58. BigDecimal partialDayInNanosBd = new BigDecimal ( durationRemainder.toNanos ( ) ); // Convert entire duration to a total number of nanoseconds.
  59. BigDecimal nanosInADayBd = new BigDecimal ( TimeUnit.DAYS.toNanos ( 1 ) ); // How long is a standard day in nanoseconds?
  60. System.out.println ( "nanosInaDayBd: " + nanosInADayBd + " partialDayInNanosBd: " + partialDayInNanosBd );
  61. int scale = 9; // Maximum number of digits to the right of the decimal point.
  62. BigDecimal partialDayBd = partialDayInNanosBd.divide ( nanosInADayBd ); // Get a fraction by dividing a total number of nanos in a day by our partial day of nanos.
  63. System.out.println ( "partialDayBd: " + partialDayBd );
  64. BigDecimal result = wholeDaysBd.add ( partialDayBd );
  65. return result;
  66. }
  67.  
  68. }
Success #stdin #stdout 0.15s 4386816KB
stdin
Standard input is empty
stdout
running doIt at: 2017-03-20T13:02:42.659Z[GMT]
durationRemainder: PT12H
nanosInaDayBd: 86400000000000  partialDayInNanosBd: 43200000000000
partialDayBd: 0.5
input.toString(): 57831.5
instant.toString(): 2017-03-19T12:00:00Z
output.toString(): 57831.5