/** * Obtains an instance of {@code OffsetDateTime} from a date-time and offset. * <p> * This creates an offset date-time with the specified local date-time and offset. * * @param dateTime the local date-time, not null * @param offset the zone offset, not null * @return the offset date-time, not null */ public static OffsetDateTime of(LocalDateTime dateTime, ZoneOffset offset) { return new OffsetDateTime(dateTime, offset); }
/** * Obtains an instance of {@code OffsetDateTime} from a date-time and offset. * <p> * This creates an offset date-time with the specified local date-time and offset. * * @param dateTime the local date-time, not null * @param offset the zone offset, not null * @return the offset date-time, not null */ public static OffsetDateTime of(LocalDateTime dateTime, ZoneOffset offset) { return new OffsetDateTime(dateTime, offset); }
/** * Returns a new date-time based on this one, returning {@code this} where possible. * * @param dateTime the date-time to create with, not null * @param offset the zone offset to create with, not null */ private OffsetDateTime with(LocalDateTime dateTime, ZoneOffset offset) { if (this.dateTime == dateTime && this.offset.equals(offset)) { return this; } return new OffsetDateTime(dateTime, offset); }
/** * Returns a new date-time based on this one, returning {@code this} where possible. * * @param dateTime the date-time to create with, not null * @param offset the zone offset to create with, not null */ private OffsetDateTime with(LocalDateTime dateTime, ZoneOffset offset) { if (this.dateTime == dateTime && this.offset.equals(offset)) { return this; } return new OffsetDateTime(dateTime, offset); }
/** * Obtains an instance of {@code OffsetDateTime} from a date, time and offset. * <p> * This creates an offset date-time with the specified local date, time and offset. * * @param date the local date, not null * @param time the local time, not null * @param offset the zone offset, not null * @return the offset date-time, not null */ public static OffsetDateTime of(LocalDate date, LocalTime time, ZoneOffset offset) { LocalDateTime dt = LocalDateTime.of(date, time); return new OffsetDateTime(dt, offset); }
/** * Obtains an instance of {@code OffsetDateTime} from a date, time and offset. * <p> * This creates an offset date-time with the specified local date, time and offset. * * @param date the local date, not null * @param time the local time, not null * @param offset the zone offset, not null * @return the offset date-time, not null */ public static OffsetDateTime of(LocalDate date, LocalTime time, ZoneOffset offset) { LocalDateTime dt = LocalDateTime.of(date, time); return new OffsetDateTime(dt, offset); }
/** * Obtains an instance of {@code OffsetDateTime} from a year, month, day, * hour, minute, second, nanosecond and offset. * <p> * This creates an offset date-time with the seven specified fields. * <p> * This method exists primarily for writing test cases. * Non test-code will typically use other methods to create an offset time. * {@code LocalDateTime} has five additional convenience variants of the * equivalent factory method taking fewer arguments. * They are not provided here to reduce the footprint of the API. * * @param year the year to represent, from MIN_YEAR to MAX_YEAR * @param month the month-of-year to represent, from 1 (January) to 12 (December) * @param dayOfMonth the day-of-month to represent, from 1 to 31 * @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 * @param offset the zone offset, not null * @return the offset date-time, not null * @throws DateTimeException if the value of any field is out of range, or * if the day-of-month is invalid for the month-year */ public static OffsetDateTime of( int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) { LocalDateTime dt = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); return new OffsetDateTime(dt, offset); }
/** * Obtains an instance of {@code OffsetDateTime} from a year, month, day, * hour, minute, second, nanosecond and offset. * <p> * This creates an offset date-time with the seven specified fields. * <p> * This method exists primarily for writing test cases. * Non test-code will typically use other methods to create an offset time. * {@code LocalDateTime} has five additional convenience variants of the * equivalent factory method taking fewer arguments. * They are not provided here to reduce the footprint of the API. * * @param year the year to represent, from MIN_YEAR to MAX_YEAR * @param month the month-of-year to represent, from 1 (January) to 12 (December) * @param dayOfMonth the day-of-month to represent, from 1 to 31 * @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 * @param offset the zone offset, not null * @return the offset date-time, not null * @throws DateTimeException if the value of any field is out of range, or * if the day-of-month is invalid for the month-year */ public static OffsetDateTime of( int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) { LocalDateTime dt = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); return new OffsetDateTime(dt, offset); }
/** * Returns a copy of this {@code OffsetDateTime} with the specified offset ensuring * that the result is at the same instant. * <p> * This method returns an object with the specified {@code ZoneOffset} and a {@code LocalDateTime} * adjusted by the difference between the two offsets. * This will result in the old and new objects representing the same instant. * This is useful for finding the local time in a different offset. * For example, if this time represents {@code 2007-12-23T10:30+02:00} and the offset specified is * {@code +03:00}, then this method will return {@code 2007-12-23T11:30+03:00}. * <p> * To change the offset without adjusting the local time use {@link #withOffsetSameLocal}. * <p> * This instance is immutable and unaffected by this method call. * * @param offset the zone offset to change to, not null * @return an {@code OffsetDateTime} based on this date-time with the requested offset, not null * @throws DateTimeException if the result exceeds the supported date range */ public OffsetDateTime withOffsetSameInstant(ZoneOffset offset) { if (offset.equals(this.offset)) { return this; } int difference = offset.getTotalSeconds() - this.offset.getTotalSeconds(); LocalDateTime adjusted = dateTime.plusSeconds(difference); return new OffsetDateTime(adjusted, offset); }
/** * Returns a copy of this {@code OffsetDateTime} with the specified offset ensuring * that the result is at the same instant. * <p> * This method returns an object with the specified {@code ZoneOffset} and a {@code LocalDateTime} * adjusted by the difference between the two offsets. * This will result in the old and new objects representing the same instant. * This is useful for finding the local time in a different offset. * For example, if this time represents {@code 2007-12-03T10:30+02:00} and the offset specified is * {@code +03:00}, then this method will return {@code 2007-12-03T11:30+03:00}. * <p> * To change the offset without adjusting the local time use {@link #withOffsetSameLocal}. * <p> * This instance is immutable and unaffected by this method call. * * @param offset the zone offset to change to, not null * @return an {@code OffsetDateTime} based on this date-time with the requested offset, not null * @throws DateTimeException if the result exceeds the supported date range */ public OffsetDateTime withOffsetSameInstant(ZoneOffset offset) { if (offset.equals(this.offset)) { return this; } int difference = offset.getTotalSeconds() - this.offset.getTotalSeconds(); LocalDateTime adjusted = dateTime.plusSeconds(difference); return new OffsetDateTime(adjusted, offset); }
/** * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID. * <p> * This creates an offset date-time with the same instant as that specified. * Finding the offset from UTC/Greenwich is simple as there is only one valid * offset for each instant. * * @param instant the instant to create the date-time from, not null * @param zone the time-zone, which may be an offset, not null * @return the offset date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) { Jdk8Methods.requireNonNull(instant, "instant"); Jdk8Methods.requireNonNull(zone, "zone"); ZoneRules rules = zone.getRules(); ZoneOffset offset = rules.getOffset(instant); LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset); return new OffsetDateTime(ldt, offset); }
/** * Obtains an instance of {@code OffsetDateTime} from an {@code Instant} and zone ID. * <p> * This creates an offset date-time with the same instant as that specified. * Finding the offset from UTC/Greenwich is simple as there is only one valid * offset for each instant. * * @param instant the instant to create the date-time from, not null * @param zone the time-zone, which may be an offset, not null * @return the offset date-time, not null * @throws DateTimeException if the result exceeds the supported range */ public static OffsetDateTime ofInstant(Instant instant, ZoneId zone) { Jdk8Methods.requireNonNull(instant, "instant"); Jdk8Methods.requireNonNull(zone, "zone"); ZoneRules rules = zone.getRules(); ZoneOffset offset = rules.getOffset(instant); LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset); return new OffsetDateTime(ldt, offset); }