< prev index next >

src/java.base/share/classes/java/time/LocalDate.java

Print this page




 321         ZoneRules rules = zone.getRules();
 322         ZoneOffset offset = rules.getOffset(instant);
 323         long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();
 324         long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
 325         return ofEpochDay(localEpochDay);
 326     }
 327 
 328     //-----------------------------------------------------------------------
 329     /**
 330      * Obtains an instance of {@code LocalDate} from the epoch day count.
 331      * <p>
 332      * This returns a {@code LocalDate} with the specified epoch-day.
 333      * The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
 334      * of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
 335      *
 336      * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
 337      * @return the local date, not null
 338      * @throws DateTimeException if the epoch day exceeds the supported date range
 339      */
 340     public static LocalDate ofEpochDay(long epochDay) {

 341         long zeroDay = epochDay + DAYS_0000_TO_1970;
 342         // find the march-based year
 343         zeroDay -= 60;  // adjust to 0000-03-01 so leap day is at end of four year cycle
 344         long adjust = 0;
 345         if (zeroDay < 0) {
 346             // adjust negative years to positive for calculation
 347             long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1;
 348             adjust = adjustCycles * 400;
 349             zeroDay += -adjustCycles * DAYS_PER_CYCLE;
 350         }
 351         long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE;
 352         long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 353         if (doyEst < 0) {
 354             // fix estimate
 355             yearEst--;
 356             doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 357         }
 358         yearEst += adjust;  // reset any negative year
 359         int marchDoy0 = (int) doyEst;
 360 




 321         ZoneRules rules = zone.getRules();
 322         ZoneOffset offset = rules.getOffset(instant);
 323         long localSecond = instant.getEpochSecond() + offset.getTotalSeconds();
 324         long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
 325         return ofEpochDay(localEpochDay);
 326     }
 327 
 328     //-----------------------------------------------------------------------
 329     /**
 330      * Obtains an instance of {@code LocalDate} from the epoch day count.
 331      * <p>
 332      * This returns a {@code LocalDate} with the specified epoch-day.
 333      * The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
 334      * of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
 335      *
 336      * @param epochDay  the Epoch Day to convert, based on the epoch 1970-01-01
 337      * @return the local date, not null
 338      * @throws DateTimeException if the epoch day exceeds the supported date range
 339      */
 340     public static LocalDate ofEpochDay(long epochDay) {
 341         EPOCH_DAY.checkValidValue(epochDay);
 342         long zeroDay = epochDay + DAYS_0000_TO_1970;
 343         // find the march-based year
 344         zeroDay -= 60;  // adjust to 0000-03-01 so leap day is at end of four year cycle
 345         long adjust = 0;
 346         if (zeroDay < 0) {
 347             // adjust negative years to positive for calculation
 348             long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1;
 349             adjust = adjustCycles * 400;
 350             zeroDay += -adjustCycles * DAYS_PER_CYCLE;
 351         }
 352         long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE;
 353         long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 354         if (doyEst < 0) {
 355             // fix estimate
 356             yearEst--;
 357             doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
 358         }
 359         yearEst += adjust;  // reset any negative year
 360         int marchDoy0 = (int) doyEst;
 361 


< prev index next >