/** * Returns a copy of this {@code LocalTime} with the specified period in hours added. * <p> * This adds the specified number of hours to this time, returning a new time. * The calculation wraps around midnight. * <p> * This instance is immutable and unaffected by this method call. * * @param hoursToAdd the hours to add, may be negative * @return a {@code LocalTime} based on this time with the hours added, not null */ public LocalTime plusHours(long hoursToAdd) { if (hoursToAdd == 0) { return this; } int newHour = ((int) (hoursToAdd % HOURS_PER_DAY) + hour + HOURS_PER_DAY) % HOURS_PER_DAY; return create(newHour, minute, second, nano); }
/** * Returns a copy of this {@code LocalTime} with the specified period in hours added. * <p> * This adds the specified number of hours to this time, returning a new time. * The calculation wraps around midnight. * <p> * This instance is immutable and unaffected by this method call. * * @param hoursToAdd the hours to add, may be negative * @return a {@code LocalTime} based on this time with the hours added, not null */ public LocalTime plusHours(long hoursToAdd) { if (hoursToAdd == 0) { return this; } int newHour = ((int) (hoursToAdd % HOURS_PER_DAY) + hour + HOURS_PER_DAY) % HOURS_PER_DAY; return create(newHour, minute, second, nano); }
/** * Returns a copy of this {@code LocalTime} with the specified period in minutes added. * <p> * This adds the specified number of minutes to this time, returning a new time. * The calculation wraps around midnight. * <p> * This instance is immutable and unaffected by this method call. * * @param minutesToAdd the minutes to add, may be negative * @return a {@code LocalTime} based on this time with the minutes added, not null */ public LocalTime plusMinutes(long minutesToAdd) { if (minutesToAdd == 0) { return this; } int mofd = hour * MINUTES_PER_HOUR + minute; int newMofd = ((int) (minutesToAdd % MINUTES_PER_DAY) + mofd + MINUTES_PER_DAY) % MINUTES_PER_DAY; if (mofd == newMofd) { return this; } int newHour = newMofd / MINUTES_PER_HOUR; int newMinute = newMofd % MINUTES_PER_HOUR; return create(newHour, newMinute, second, nano); }
/** * Returns a copy of this {@code LocalTime} with the second-of-minute value altered. * <p> * This instance is immutable and unaffected by this method call. * * @param second the second-of-minute to set in the result, from 0 to 59 * @return a {@code LocalTime} based on this time with the requested second, not null * @throws DateTimeException if the second value is invalid */ public LocalTime withSecond(int second) { if (this.second == second) { return this; } SECOND_OF_MINUTE.checkValidValue(second); return create(hour, minute, second, nano); }
/** * Returns a copy of this {@code LocalTime} with the nano-of-second value altered. * <p> * This instance is immutable and unaffected by this method call. * * @param nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999 * @return a {@code LocalTime} based on this time with the requested nanosecond, not null * @throws DateTimeException if the nanos value is invalid */ public LocalTime withNano(int nanoOfSecond) { if (this.nano == nanoOfSecond) { return this; } NANO_OF_SECOND.checkValidValue(nanoOfSecond); return create(hour, minute, second, nanoOfSecond); }
/** * Returns a copy of this {@code LocalTime} with the hour-of-day value altered. * <p> * This instance is immutable and unaffected by this method call. * * @param hour the hour-of-day to set in the result, from 0 to 23 * @return a {@code LocalTime} based on this time with the requested hour, not null * @throws DateTimeException if the hour value is invalid */ public LocalTime withHour(int hour) { if (this.hour == hour) { return this; } HOUR_OF_DAY.checkValidValue(hour); return create(hour, minute, second, nano); }
/** * Returns a copy of this {@code LocalTime} with the nano-of-second value altered. * <p> * This instance is immutable and unaffected by this method call. * * @param nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999 * @return a {@code LocalTime} based on this time with the requested nanosecond, not null * @throws DateTimeException if the nanos value is invalid */ public LocalTime withNano(int nanoOfSecond) { if (this.nano == nanoOfSecond) { return this; } NANO_OF_SECOND.checkValidValue(nanoOfSecond); return create(hour, minute, second, nanoOfSecond); }
/** * Obtains an instance of {@code LocalTime} from a second-of-day value. * <p> * This factory may return a cached value, but applications must not rely on this. * * @param secondOfDay the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1} * @return the local time, not null * @throws DateTimeException if the second-of-day value is invalid */ public static LocalTime ofSecondOfDay(long secondOfDay) { SECOND_OF_DAY.checkValidValue(secondOfDay); int hours = (int) (secondOfDay / SECONDS_PER_HOUR); secondOfDay -= hours * SECONDS_PER_HOUR; int minutes = (int) (secondOfDay / SECONDS_PER_MINUTE); secondOfDay -= minutes * SECONDS_PER_MINUTE; return create(hours, minutes, (int) secondOfDay, 0); }
/** * Returns a copy of this {@code LocalTime} with the hour-of-day value altered. * <p> * This instance is immutable and unaffected by this method call. * * @param hour the hour-of-day to set in the result, from 0 to 23 * @return a {@code LocalTime} based on this time with the requested hour, not null * @throws DateTimeException if the hour value is invalid */ public LocalTime withHour(int hour) { if (this.hour == hour) { return this; } HOUR_OF_DAY.checkValidValue(hour); return create(hour, minute, second, nano); }
/** * Returns a copy of this {@code LocalTime} with the minute-of-hour value altered. * <p> * This instance is immutable and unaffected by this method call. * * @param minute the minute-of-hour to set in the result, from 0 to 59 * @return a {@code LocalTime} based on this time with the requested minute, not null * @throws DateTimeException if the minute value is invalid */ public LocalTime withMinute(int minute) { if (this.minute == minute) { return this; } MINUTE_OF_HOUR.checkValidValue(minute); return create(hour, minute, second, nano); }
/** * Returns a copy of this {@code LocalTime} with the second-of-minute value altered. * <p> * This instance is immutable and unaffected by this method call. * * @param second the second-of-minute to set in the result, from 0 to 59 * @return a {@code LocalTime} based on this time with the requested second, not null * @throws DateTimeException if the second value is invalid */ public LocalTime withSecond(int second) { if (this.second == second) { return this; } SECOND_OF_MINUTE.checkValidValue(second); return create(hour, minute, second, nano); }
/** * Obtains an instance of {@code LocalTime} from a second-of-day value. * <p> * This factory may return a cached value, but applications must not rely on this. * * @param secondOfDay the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1} * @return the local time, not null * @throws DateTimeException if the second-of-day value is invalid */ public static LocalTime ofSecondOfDay(long secondOfDay) { SECOND_OF_DAY.checkValidValue(secondOfDay); int hours = (int) (secondOfDay / SECONDS_PER_HOUR); secondOfDay -= hours * SECONDS_PER_HOUR; int minutes = (int) (secondOfDay / SECONDS_PER_MINUTE); secondOfDay -= minutes * SECONDS_PER_MINUTE; return create(hours, minutes, (int) secondOfDay, 0); }
/** * Returns a copy of this {@code LocalTime} with the minute-of-hour value altered. * <p> * This instance is immutable and unaffected by this method call. * * @param minute the minute-of-hour to set in the result, from 0 to 59 * @return a {@code LocalTime} based on this time with the requested minute, not null * @throws DateTimeException if the minute value is invalid */ public LocalTime withMinute(int minute) { if (this.minute == minute) { return this; } MINUTE_OF_HOUR.checkValidValue(minute); return create(hour, minute, second, nano); }
/** * Returns a copy of this {@code LocalTime} with the specified period in nanoseconds added. * <p> * This adds the specified number of nanoseconds to this time, returning a new time. * The calculation wraps around midnight. * <p> * This instance is immutable and unaffected by this method call. * * @param nanosToAdd the nanos to add, may be negative * @return a {@code LocalTime} based on this time with the nanoseconds added, not null */ public LocalTime plusNanos(long nanosToAdd) { if (nanosToAdd == 0) { return this; } long nofd = toNanoOfDay(); long newNofd = ((nanosToAdd % NANOS_PER_DAY) + nofd + NANOS_PER_DAY) % NANOS_PER_DAY; if (nofd == newNofd) { return this; } int newHour = (int) (newNofd / NANOS_PER_HOUR); int newMinute = (int) ((newNofd / NANOS_PER_MINUTE) % MINUTES_PER_HOUR); int newSecond = (int) ((newNofd / NANOS_PER_SECOND) % SECONDS_PER_MINUTE); int newNano = (int) (newNofd % NANOS_PER_SECOND); return create(newHour, newMinute, newSecond, newNano); }
/** * Obtains an instance of {@code LocalTime} from a nanos-of-day value. * <p> * This factory may return a cached value, but applications must not rely on this. * * @param nanoOfDay the nano of day, from {@code 0} to {@code 24 * 60 * 60 * 1,000,000,000 - 1} * @return the local time, not null * @throws DateTimeException if the nanos of day value is invalid */ public static LocalTime ofNanoOfDay(long nanoOfDay) { NANO_OF_DAY.checkValidValue(nanoOfDay); int hours = (int) (nanoOfDay / NANOS_PER_HOUR); nanoOfDay -= hours * NANOS_PER_HOUR; int minutes = (int) (nanoOfDay / NANOS_PER_MINUTE); nanoOfDay -= minutes * NANOS_PER_MINUTE; int seconds = (int) (nanoOfDay / NANOS_PER_SECOND); nanoOfDay -= seconds * NANOS_PER_SECOND; return create(hours, minutes, seconds, (int) nanoOfDay); }
/** * Obtains an instance of {@code LocalTime} from a nanos-of-day value. * <p> * This factory may return a cached value, but applications must not rely on this. * * @param nanoOfDay the nano of day, from {@code 0} to {@code 24 * 60 * 60 * 1,000,000,000 - 1} * @return the local time, not null * @throws DateTimeException if the nanos of day value is invalid */ public static LocalTime ofNanoOfDay(long nanoOfDay) { NANO_OF_DAY.checkValidValue(nanoOfDay); int hours = (int) (nanoOfDay / NANOS_PER_HOUR); nanoOfDay -= hours * NANOS_PER_HOUR; int minutes = (int) (nanoOfDay / NANOS_PER_MINUTE); nanoOfDay -= minutes * NANOS_PER_MINUTE; int seconds = (int) (nanoOfDay / NANOS_PER_SECOND); nanoOfDay -= seconds * NANOS_PER_SECOND; return create(hours, minutes, seconds, (int) nanoOfDay); }
/** * Obtains an instance of {@code LocalTime} from a second-of-day value, with * associated nanos of second. * <p> * This factory may return a cached value, but applications must not rely on this. * * @param secondOfDay the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1} * @param nanoOfSecond the nano-of-second, from 0 to 999,999,999 * @return the local time, not null * @throws DateTimeException if the either input value is invalid */ static LocalTime ofSecondOfDay(long secondOfDay, int nanoOfSecond) { SECOND_OF_DAY.checkValidValue(secondOfDay); NANO_OF_SECOND.checkValidValue(nanoOfSecond); int hours = (int) (secondOfDay / SECONDS_PER_HOUR); secondOfDay -= hours * SECONDS_PER_HOUR; int minutes = (int) (secondOfDay / SECONDS_PER_MINUTE); secondOfDay -= minutes * SECONDS_PER_MINUTE; return create(hours, minutes, (int) secondOfDay, nanoOfSecond); }
/** * Obtains an instance of {@code LocalTime} from a second-of-day value, with * associated nanos of second. * <p> * This factory may return a cached value, but applications must not rely on this. * * @param secondOfDay the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1} * @param nanoOfSecond the nano-of-second, from 0 to 999,999,999 * @return the local time, not null * @throws DateTimeException if the either input value is invalid */ static LocalTime ofSecondOfDay(long secondOfDay, int nanoOfSecond) { SECOND_OF_DAY.checkValidValue(secondOfDay); NANO_OF_SECOND.checkValidValue(nanoOfSecond); int hours = (int) (secondOfDay / SECONDS_PER_HOUR); secondOfDay -= hours * SECONDS_PER_HOUR; int minutes = (int) (secondOfDay / SECONDS_PER_MINUTE); secondOfDay -= minutes * SECONDS_PER_MINUTE; return create(hours, minutes, (int) secondOfDay, nanoOfSecond); }
/** * Obtains an instance of {@code LocalTime} from an hour, minute, second and nanosecond. * <p> * This factory may return a cached value, but applications must not rely on this. * * @param hour the hour-of-day to represent, from 0 to 23 * @param minute the minute-of-hour to represent, from 0 to 59 * @param second the second-of-minute to represent, from 0 to 59 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 * @return the local time, not null * @throws DateTimeException if the value of any field is out of range */ public static LocalTime of(int hour, int minute, int second, int nanoOfSecond) { HOUR_OF_DAY.checkValidValue(hour); MINUTE_OF_HOUR.checkValidValue(minute); SECOND_OF_MINUTE.checkValidValue(second); NANO_OF_SECOND.checkValidValue(nanoOfSecond); return create(hour, minute, second, nanoOfSecond); }
/** * Obtains an instance of {@code LocalTime} from an hour, minute, second and nanosecond. * <p> * This factory may return a cached value, but applications must not rely on this. * * @param hour the hour-of-day to represent, from 0 to 23 * @param minute the minute-of-hour to represent, from 0 to 59 * @param second the second-of-minute to represent, from 0 to 59 * @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999 * @return the local time, not null * @throws DateTimeException if the value of any field is out of range */ public static LocalTime of(int hour, int minute, int second, int nanoOfSecond) { HOUR_OF_DAY.checkValidValue(hour); MINUTE_OF_HOUR.checkValidValue(minute); SECOND_OF_MINUTE.checkValidValue(second); NANO_OF_SECOND.checkValidValue(nanoOfSecond); return create(hour, minute, second, nanoOfSecond); }