jdk/src/share/classes/java/time/LocalDateTime.java
author mduigou
Tue, 05 Nov 2013 19:44:41 -0800
changeset 21615 0231a565a5b7
parent 20873 e91d5b1cb8e6
child 22081 86eb26ff8f2b
permissions -rw-r--r--
8021309: replace test/Makefile jdk_* targets with jtreg groups 8015068: Use jtreg -exclude for handling problemList.txt exclusions Reviewed-by: jjg, smarks, chegar, alanb, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    64
import static java.time.LocalTime.HOURS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.LocalTime.MICROS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.LocalTime.MILLIS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import static java.time.LocalTime.MINUTES_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import static java.time.LocalTime.NANOS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import static java.time.LocalTime.NANOS_PER_HOUR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import static java.time.LocalTime.NANOS_PER_MINUTE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import static java.time.LocalTime.NANOS_PER_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    72
import static java.time.LocalTime.SECONDS_PER_DAY;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    73
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.io.DataInput;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.io.DataOutput;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.io.IOException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.io.InvalidObjectException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.io.Serializable;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    80
import java.time.chrono.ChronoLocalDateTime;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.time.format.DateTimeFormatter;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.time.temporal.ChronoField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.time.temporal.ChronoUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.time.temporal.Temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
import java.time.temporal.TemporalAdjuster;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    88
import java.time.temporal.TemporalAmount;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
    90
import java.time.temporal.TemporalQueries;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
import java.time.temporal.TemporalQuery;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
import java.time.temporal.TemporalUnit;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    93
import java.time.temporal.UnsupportedTemporalTypeException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
import java.time.zone.ZoneRules;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * A date-time without a time-zone in the ISO-8601 calendar system,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * such as {@code 2007-12-03T10:15:30}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 * {@code LocalDateTime} is an immutable date-time object that represents a date-time,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 * often viewed as year-month-day-hour-minute-second. Other date and time fields,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 * such as day-of-year, day-of-week and week-of-year, can also be accessed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * Time is represented to nanosecond precision.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 * For example, the value "2nd October 2007 at 13:45.30.123456789" can be
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * stored in a {@code LocalDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
 * This class does not store or represent a time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
 * Instead, it is a description of the date, as used for birthdays, combined with
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   111
 * the local time as seen on a wall clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
 * It cannot represent an instant on the time-line without additional information
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
 * such as an offset or time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
 * The ISO-8601 calendar system is the modern civil calendar system used today
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
 * in most of the world. It is equivalent to the proleptic Gregorian calendar
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
 * system, in which today's rules for leap years are applied for all time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
 * For most applications written today, the ISO-8601 rules are entirely suitable.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
 * However, any application that makes use of historical dates, and requires them
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
 * to be accurate will find the ISO-8601 approach unsuitable.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   122
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
public final class LocalDateTime
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   128
        implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
     * The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
     * This is the local date-time of midnight at the start of the minimum date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
     * This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
     * This could be used by an application as a "far past" date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
    public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
     * The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
     * This is the local date-time just before midnight at the end of the maximum date.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
     * This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
     * This could be used by an application as a "far future" date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   142
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   143
    public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   146
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   147
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   148
    private static final long serialVersionUID = 6207766400415563566L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   149
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   150
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   151
     * The date part.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   152
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   153
    private final LocalDate date;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   154
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   155
     * The time part.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   156
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   157
    private final LocalTime time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   158
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   159
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   160
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   161
     * Obtains the current date-time from the system clock in the default time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   162
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   163
     * This will query the {@link Clock#systemDefaultZone() system clock} in the default
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   164
     * time-zone to obtain the current date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   165
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   166
     * Using this method will prevent the ability to use an alternate clock for testing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   167
     * because the clock is hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   168
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   169
     * @return the current date-time using the system clock and default time-zone, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   170
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   171
    public static LocalDateTime now() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   172
        return now(Clock.systemDefaultZone());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   174
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   175
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   176
     * Obtains the current date-time from the system clock in the specified time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   178
     * This will query the {@link Clock#system(ZoneId) system clock} to obtain the current date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   179
     * Specifying the time-zone avoids dependence on the default time-zone.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   180
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
     * Using this method will prevent the ability to use an alternate clock for testing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   182
     * because the clock is hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   183
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   184
     * @param zone  the zone ID to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
     * @return the current date-time using the system clock, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   186
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   187
    public static LocalDateTime now(ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   188
        return now(Clock.system(zone));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   189
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
     * Obtains the current date-time from the specified clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
     * This will query the specified clock to obtain the current date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
     * Using this method allows the use of an alternate clock for testing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
     * The alternate clock may be introduced using {@link Clock dependency injection}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
     * @param clock  the clock to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   199
     * @return the current date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
    public static LocalDateTime now(Clock clock) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
        Objects.requireNonNull(clock, "clock");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
        final Instant now = clock.instant();  // called once
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
        ZoneOffset offset = clock.getZone().getRules().getOffset(now);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
        return ofEpochSecond(now.getEpochSecond(), now.getNano(), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
     * Obtains an instance of {@code LocalDateTime} from year, month,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     * day, hour and minute, setting the second and nanosecond to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   213
     * This returns a {@code LocalDateTime} with the specified year, month,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   214
     * day-of-month, hour and minute.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * The day must be valid for the year and month, otherwise an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     * The second and nanosecond fields will be set to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * @param month  the month-of-year to represent, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * @param dayOfMonth  the day-of-month to represent, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
     * @param hour  the hour-of-day to represent, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     * @param minute  the minute-of-hour to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
     * @return the local date-time, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   224
     * @throws DateTimeException if the value of any field is out of range,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   225
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
    public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
        LocalDate date = LocalDate.of(year, month, dayOfMonth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
        LocalTime time = LocalTime.of(hour, minute);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
        return new LocalDateTime(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
     * Obtains an instance of {@code LocalDateTime} from year, month,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
     * day, hour, minute and second, setting the nanosecond to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   237
     * This returns a {@code LocalDateTime} with the specified year, month,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   238
     * day-of-month, hour, minute and second.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     * The day must be valid for the year and month, otherwise an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     * The nanosecond field will be set to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     * @param month  the month-of-year to represent, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     * @param dayOfMonth  the day-of-month to represent, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
     * @param hour  the hour-of-day to represent, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
     * @param minute  the minute-of-hour to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
     * @param second  the second-of-minute to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
     * @return the local date-time, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   249
     * @throws DateTimeException if the value of any field is out of range,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   250
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
    public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
        LocalDate date = LocalDate.of(year, month, dayOfMonth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
        LocalTime time = LocalTime.of(hour, minute, second);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
        return new LocalDateTime(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
     * Obtains an instance of {@code LocalDateTime} from year, month,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
     * day, hour, minute, second and nanosecond.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   262
     * This returns a {@code LocalDateTime} with the specified year, month,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   263
     * day-of-month, hour, minute, second and nanosecond.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     * The day must be valid for the year and month, otherwise an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     * @param month  the month-of-year to represent, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
     * @param dayOfMonth  the day-of-month to represent, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     * @param hour  the hour-of-day to represent, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     * @param minute  the minute-of-hour to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * @param second  the second-of-minute to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
     * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
     * @return the local date-time, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   274
     * @throws DateTimeException if the value of any field is out of range,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   275
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
    public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
        LocalDate date = LocalDate.of(year, month, dayOfMonth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
        LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
        return new LocalDateTime(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
     * Obtains an instance of {@code LocalDateTime} from year, month,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
     * day, hour and minute, setting the second and nanosecond to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   288
     * This returns a {@code LocalDateTime} with the specified year, month,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   289
     * day-of-month, hour and minute.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
     * The day must be valid for the year and month, otherwise an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
     * The second and nanosecond fields will be set to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
     * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
     * @param dayOfMonth  the day-of-month to represent, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * @param hour  the hour-of-day to represent, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * @param minute  the minute-of-hour to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     * @return the local date-time, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   299
     * @throws DateTimeException if the value of any field is out of range,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   300
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
    public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
        LocalDate date = LocalDate.of(year, month, dayOfMonth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
        LocalTime time = LocalTime.of(hour, minute);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
        return new LocalDateTime(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
     * Obtains an instance of {@code LocalDateTime} from year, month,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
     * day, hour, minute and second, setting the nanosecond to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   312
     * This returns a {@code LocalDateTime} with the specified year, month,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   313
     * day-of-month, hour, minute and second.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
     * The day must be valid for the year and month, otherwise an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   315
     * The nanosecond field will be set to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
     * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
     * @param dayOfMonth  the day-of-month to represent, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
     * @param hour  the hour-of-day to represent, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
     * @param minute  the minute-of-hour to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     * @param second  the second-of-minute to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     * @return the local date-time, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   324
     * @throws DateTimeException if the value of any field is out of range,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   325
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
    public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
        LocalDate date = LocalDate.of(year, month, dayOfMonth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
        LocalTime time = LocalTime.of(hour, minute, second);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
        return new LocalDateTime(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
     * Obtains an instance of {@code LocalDateTime} from year, month,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
     * day, hour, minute, second and nanosecond.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   337
     * This returns a {@code LocalDateTime} with the specified year, month,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   338
     * day-of-month, hour, minute, second and nanosecond.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
     * The day must be valid for the year and month, otherwise an exception will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
     * @param year  the year to represent, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
     * @param month  the month-of-year to represent, from 1 (January) to 12 (December)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
     * @param dayOfMonth  the day-of-month to represent, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
     * @param hour  the hour-of-day to represent, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
     * @param minute  the minute-of-hour to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
     * @param second  the second-of-minute to represent, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
     * @param nanoOfSecond  the nano-of-second to represent, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
     * @return the local date-time, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   349
     * @throws DateTimeException if the value of any field is out of range,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   350
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
    public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
        LocalDate date = LocalDate.of(year, month, dayOfMonth);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
        LocalTime time = LocalTime.of(hour, minute, second, nanoOfSecond);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
        return new LocalDateTime(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
     * Obtains an instance of {@code LocalDateTime} from a date and time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
     * @param date  the local date, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
     * @param time  the local time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
     * @return the local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
    public static LocalDateTime of(LocalDate date, LocalTime time) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
        Objects.requireNonNull(date, "date");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
        Objects.requireNonNull(time, "time");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
        return new LocalDateTime(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   369
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   370
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   371
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   372
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   373
     * Obtains an instance of {@code LocalDateTime} from an {@code Instant} and zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   374
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   375
     * This creates a local date-time based on the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   376
     * First, the offset from UTC/Greenwich is obtained using the zone ID and instant,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
     * which is simple as there is only one valid offset for each instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
     * Then, the instant and offset are used to calculate the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
     * @param instant  the instant to create the date-time from, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
     * @param zone  the time-zone, which may be an offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
     * @return the local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
     * @throws DateTimeException if the result exceeds the supported range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
    public static LocalDateTime ofInstant(Instant instant, ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
        Objects.requireNonNull(instant, "instant");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
        Objects.requireNonNull(zone, "zone");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
        ZoneRules rules = zone.getRules();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
        ZoneOffset offset = rules.getOffset(instant);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
        return ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
     * Obtains an instance of {@code LocalDateTime} using seconds from the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
     * epoch of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
     * This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
     * to be converted to a local date-time. This is primarily intended for
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
     * low-level conversions rather than general application usage.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
     * @param epochSecond  the number of seconds from the epoch of 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
     * @param nanoOfSecond  the nanosecond within the second, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
     * @param offset  the zone offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
     * @return the local date-time, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   405
     * @throws DateTimeException if the result exceeds the supported range,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   406
     *  or if the nano-of-second is invalid
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
    public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
        Objects.requireNonNull(offset, "offset");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   410
        NANO_OF_SECOND.checkValidValue(nanoOfSecond);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
        long localSecond = epochSecond + offset.getTotalSeconds();  // overflow caught later
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
        long localEpochDay = Math.floorDiv(localSecond, SECONDS_PER_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
        int secsOfDay = (int)Math.floorMod(localSecond, SECONDS_PER_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
        LocalDate date = LocalDate.ofEpochDay(localEpochDay);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   415
        LocalTime time = LocalTime.ofNanoOfDay(secsOfDay * NANOS_PER_SECOND + nanoOfSecond);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
        return new LocalDateTime(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     * Obtains an instance of {@code LocalDateTime} from a temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   423
     * This obtains an offset time based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   424
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   425
     * which this factory converts to an instance of {@code LocalDateTime}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   427
     * The conversion extracts and combines the {@code LocalDate} and the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   428
     * {@code LocalTime} from the temporal object.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   429
     * Implementations are permitted to perform optimizations such as accessing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   430
     * those fields that are equivalent to the relevant objects.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
     * This method matches the signature of the functional interface {@link TemporalQuery}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
     * allowing it to be used as a query via method reference, {@code LocalDateTime::from}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
     * @param temporal  the temporal object to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
     * @return the local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     * @throws DateTimeException if unable to convert to a {@code LocalDateTime}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
    public static LocalDateTime from(TemporalAccessor temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
        if (temporal instanceof LocalDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
            return (LocalDateTime) temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
        } else if (temporal instanceof ZonedDateTime) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   443
            return ((ZonedDateTime) temporal).toLocalDateTime();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   444
        } else if (temporal instanceof OffsetDateTime) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   445
            return ((OffsetDateTime) temporal).toLocalDateTime();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
        try {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
            LocalDate date = LocalDate.from(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
            LocalTime time = LocalTime.from(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
            return new LocalDateTime(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
        } catch (DateTimeException ex) {
20747
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20520
diff changeset
   452
            throw new DateTimeException("Unable to obtain LocalDateTime from TemporalAccessor: " +
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20520
diff changeset
   453
                    temporal + " of type " + temporal.getClass().getName(), ex);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
     * Obtains an instance of {@code LocalDateTime} from a text string such as {@code 2007-12-03T10:15:30}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
     * The string must represent a valid date-time and is parsed using
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   462
     * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
     * @param text  the text to parse such as "2007-12-03T10:15:30", not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
     * @return the parsed local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   466
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   467
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   468
    public static LocalDateTime parse(CharSequence text) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   469
        return parse(text, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   470
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   471
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   472
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   473
     * Obtains an instance of {@code LocalDateTime} from a text string using a specific formatter.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   474
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   475
     * The text is parsed using the formatter, returning a date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   476
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   477
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   478
     * @param formatter  the formatter to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   479
     * @return the parsed local date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   480
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   481
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   482
    public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   483
        Objects.requireNonNull(formatter, "formatter");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   484
        return formatter.parse(text, LocalDateTime::from);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   485
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   486
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   487
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   488
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   489
     * Constructor.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   490
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   491
     * @param date  the date part of the date-time, validated not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   492
     * @param time  the time part of the date-time, validated not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   493
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   494
    private LocalDateTime(LocalDate date, LocalTime time) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   495
        this.date = date;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   496
        this.time = time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   497
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   498
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   499
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
     * Returns a copy of this date-time with the new date and time, checking
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
     * to see if a new object is in fact required.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
     * @param newDate  the date of the new date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
     * @param newTime  the time of the new date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
     * @return the date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
    private LocalDateTime with(LocalDate newDate, LocalTime newTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
        if (date == newDate && time == newTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
        return new LocalDateTime(newDate, newTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   515
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
     * Checks if the specified field is supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   517
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
     * This checks if this date-time can be queried for the specified field.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   519
     * If false, then calling the {@link #range(TemporalField) range},
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   520
     * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   521
     * methods will throw an exception.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   522
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   523
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   524
     * The supported fields are:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   525
     * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   526
     * <li>{@code NANO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   527
     * <li>{@code NANO_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
     * <li>{@code MICRO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   529
     * <li>{@code MICRO_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   530
     * <li>{@code MILLI_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   531
     * <li>{@code MILLI_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   532
     * <li>{@code SECOND_OF_MINUTE}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
     * <li>{@code SECOND_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   534
     * <li>{@code MINUTE_OF_HOUR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
     * <li>{@code MINUTE_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
     * <li>{@code HOUR_OF_AMPM}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   537
     * <li>{@code CLOCK_HOUR_OF_AMPM}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
     * <li>{@code HOUR_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   539
     * <li>{@code CLOCK_HOUR_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   540
     * <li>{@code AMPM_OF_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   541
     * <li>{@code DAY_OF_WEEK}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   542
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   543
     * <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   544
     * <li>{@code DAY_OF_MONTH}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   545
     * <li>{@code DAY_OF_YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   546
     * <li>{@code EPOCH_DAY}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   547
     * <li>{@code ALIGNED_WEEK_OF_MONTH}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   548
     * <li>{@code ALIGNED_WEEK_OF_YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   549
     * <li>{@code MONTH_OF_YEAR}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   550
     * <li>{@code PROLEPTIC_MONTH}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   551
     * <li>{@code YEAR_OF_ERA}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   552
     * <li>{@code YEAR}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   553
     * <li>{@code ERA}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   554
     * </ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   555
     * All other {@code ChronoField} instances will return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   556
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   557
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   558
     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   559
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   560
     * Whether the field is supported is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   561
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   562
     * @param field  the field to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   563
     * @return true if the field is supported on this date-time, false if not
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   564
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   565
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   566
    public boolean isSupported(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   567
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   568
            ChronoField f = (ChronoField) field;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   569
            return f.isDateBased() || f.isTimeBased();
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   570
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   571
        return field != null && field.isSupportedBy(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   572
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   573
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   574
    /**
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   575
     * Checks if the specified unit is supported.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   576
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   577
     * This checks if the specified unit can be added to, or subtracted from, this date-time.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   578
     * If false, then calling the {@link #plus(long, TemporalUnit)} and
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   579
     * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   580
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   581
     * If the unit is a {@link ChronoUnit} then the query is implemented here.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   582
     * The supported units are:
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   583
     * <ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   584
     * <li>{@code NANOS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   585
     * <li>{@code MICROS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   586
     * <li>{@code MILLIS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   587
     * <li>{@code SECONDS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   588
     * <li>{@code MINUTES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   589
     * <li>{@code HOURS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   590
     * <li>{@code HALF_DAYS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   591
     * <li>{@code DAYS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   592
     * <li>{@code WEEKS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   593
     * <li>{@code MONTHS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   594
     * <li>{@code YEARS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   595
     * <li>{@code DECADES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   596
     * <li>{@code CENTURIES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   597
     * <li>{@code MILLENNIA}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   598
     * <li>{@code ERAS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   599
     * </ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   600
     * All other {@code ChronoUnit} instances will return false.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   601
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   602
     * If the unit is not a {@code ChronoUnit}, then the result of this method
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   603
     * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   604
     * passing {@code this} as the argument.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   605
     * Whether the unit is supported is determined by the unit.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   606
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   607
     * @param unit  the unit to check, null returns false
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   608
     * @return true if the unit can be added/subtracted, false if not
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   609
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   610
    @Override  // override for Javadoc
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   611
    public boolean isSupported(TemporalUnit unit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   612
        return ChronoLocalDateTime.super.isSupported(unit);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   613
    }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   614
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   615
    //-----------------------------------------------------------------------
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   616
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   617
     * Gets the range of valid values for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   618
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   619
     * The range object expresses the minimum and maximum valid values for a field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
     * This date-time is used to enhance the accuracy of the returned range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   621
     * If it is not possible to return the range, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   622
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   623
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   624
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   625
     * The {@link #isSupported(TemporalField) supported fields} will return
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   626
     * appropriate range instances.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   627
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   628
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   629
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   630
     * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   631
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   632
     * Whether the range can be obtained is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   633
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   634
     * @param field  the field to query the range for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   635
     * @return the range of valid values for the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   636
     * @throws DateTimeException if the range for the field cannot be obtained
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   637
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   638
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   639
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   640
    public ValueRange range(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
            ChronoField f = (ChronoField) field;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   643
            return (f.isTimeBased() ? time.range(field) : date.range(field));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   644
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   645
        return field.rangeRefinedBy(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   646
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   647
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   648
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   649
     * Gets the value of the specified field from this date-time as an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   650
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   651
     * This queries this date-time for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   652
     * The returned value will always be within the valid range of values for the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   653
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   657
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   658
     * values based on this date-time, except {@code NANO_OF_DAY}, {@code MICRO_OF_DAY},
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   659
     * {@code EPOCH_DAY} and {@code PROLEPTIC_MONTH} which are too large to fit in
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   660
     * an {@code int} and throw a {@code DateTimeException}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   661
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   662
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   663
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   664
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
     * @return the value for the field
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   670
     * @throws DateTimeException if a value for the field cannot be obtained or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   671
     *         the value is outside the range of valid values for the field
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   672
     * @throws UnsupportedTemporalTypeException if the field is not supported or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   673
     *         the range of values exceeds an {@code int}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   676
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   677
    public int get(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
            ChronoField f = (ChronoField) field;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   680
            return (f.isTimeBased() ? time.get(field) : date.get(field));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   681
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   682
        return ChronoLocalDateTime.super.get(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   684
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
     * Gets the value of the specified field from this date-time as a {@code long}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   687
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
     * This queries this date-time for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   690
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   691
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
     * values based on this date-time.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   695
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   696
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   697
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   698
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   700
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   701
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   702
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   703
     * @return the value for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   704
     * @throws DateTimeException if a value for the field cannot be obtained
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   705
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   706
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   707
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   709
    public long getLong(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   710
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   711
            ChronoField f = (ChronoField) field;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   712
            return (f.isTimeBased() ? time.getLong(field) : date.getLong(field));
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   713
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   714
        return field.getFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   715
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   716
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   717
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   718
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   719
     * Gets the {@code LocalDate} part of this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   720
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   721
     * This returns a {@code LocalDate} with the same year, month and day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   722
     * as this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   723
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   724
     * @return the date part of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   725
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   726
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   727
    public LocalDate toLocalDate() {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   728
        return date;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   729
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   730
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   731
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   732
     * Gets the year field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   733
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   734
     * This method returns the primitive {@code int} value for the year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   735
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   736
     * The year returned by this method is proleptic as per {@code get(YEAR)}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   737
     * To obtain the year-of-era, use {@code get(YEAR_OF_ERA)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   738
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   739
     * @return the year, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   740
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   741
    public int getYear() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   742
        return date.getYear();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   743
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   744
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   745
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   746
     * Gets the month-of-year field from 1 to 12.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   747
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   748
     * This method returns the month as an {@code int} from 1 to 12.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   749
     * Application code is frequently clearer if the enum {@link Month}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   750
     * is used by calling {@link #getMonth()}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   751
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   752
     * @return the month-of-year, from 1 to 12
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   753
     * @see #getMonth()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   754
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   755
    public int getMonthValue() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   756
        return date.getMonthValue();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   757
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   758
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   759
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   760
     * Gets the month-of-year field using the {@code Month} enum.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   761
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   762
     * This method returns the enum {@link Month} for the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
     * This avoids confusion as to what {@code int} values mean.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   764
     * If you need access to the primitive {@code int} value then the enum
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   765
     * provides the {@link Month#getValue() int value}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   766
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   767
     * @return the month-of-year, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   768
     * @see #getMonthValue()
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   769
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   770
    public Month getMonth() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   771
        return date.getMonth();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   772
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   773
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   774
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   775
     * Gets the day-of-month field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   776
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   777
     * This method returns the primitive {@code int} value for the day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   778
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   779
     * @return the day-of-month, from 1 to 31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   780
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   781
    public int getDayOfMonth() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   782
        return date.getDayOfMonth();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   783
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   784
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   786
     * Gets the day-of-year field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   788
     * This method returns the primitive {@code int} value for the day-of-year.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   790
     * @return the day-of-year, from 1 to 365, or 366 in a leap year
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   791
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   792
    public int getDayOfYear() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   793
        return date.getDayOfYear();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   794
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   795
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   796
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   797
     * Gets the day-of-week field, which is an enum {@code DayOfWeek}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   798
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   799
     * This method returns the enum {@link DayOfWeek} for the day-of-week.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   800
     * This avoids confusion as to what {@code int} values mean.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   801
     * If you need access to the primitive {@code int} value then the enum
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   802
     * provides the {@link DayOfWeek#getValue() int value}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   803
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   804
     * Additional information can be obtained from the {@code DayOfWeek}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   805
     * This includes textual names of the values.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   806
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   807
     * @return the day-of-week, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   808
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   809
    public DayOfWeek getDayOfWeek() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   810
        return date.getDayOfWeek();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   811
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   812
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   813
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   814
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   815
     * Gets the {@code LocalTime} part of this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   816
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   817
     * This returns a {@code LocalTime} with the same hour, minute, second and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   818
     * nanosecond as this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   819
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   820
     * @return the time part of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   821
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   822
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   823
    public LocalTime toLocalTime() {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   824
        return time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   825
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   826
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   827
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   828
     * Gets the hour-of-day field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   829
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   830
     * @return the hour-of-day, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   831
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   832
    public int getHour() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   833
        return time.getHour();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   834
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   835
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   836
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   837
     * Gets the minute-of-hour field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   838
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   839
     * @return the minute-of-hour, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   840
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   841
    public int getMinute() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   842
        return time.getMinute();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   843
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   844
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   845
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   846
     * Gets the second-of-minute field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   847
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   848
     * @return the second-of-minute, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   849
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   850
    public int getSecond() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   851
        return time.getSecond();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   852
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   853
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   854
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   855
     * Gets the nano-of-second field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   856
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   857
     * @return the nano-of-second, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   858
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   859
    public int getNano() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   860
        return time.getNano();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   861
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   862
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   863
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   864
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   865
     * Returns an adjusted copy of this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   866
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   867
     * This returns a {@code LocalDateTime}, based on this one, with the date-time adjusted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   868
     * The adjustment takes place using the specified adjuster strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   869
     * Read the documentation of the adjuster to understand what adjustment will be made.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   870
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   871
     * A simple adjuster might simply set the one of the fields, such as the year field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   872
     * A more complex adjuster might set the date to the last day of the month.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   873
     * A selection of common adjustments is provided in {@link TemporalAdjuster}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   874
     * These include finding the "last day of the month" and "next Wednesday".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   875
     * Key date-time classes also implement the {@code TemporalAdjuster} interface,
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   876
     * such as {@link Month} and {@link java.time.MonthDay MonthDay}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   877
     * The adjuster is responsible for handling special cases, such as the varying
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   878
     * lengths of month and leap years.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   879
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   880
     * For example this code returns a date on the last day of July:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   881
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   882
     *  import static java.time.Month.*;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   883
     *  import static java.time.temporal.Adjusters.*;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   884
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   885
     *  result = localDateTime.with(JULY).with(lastDayOfMonth());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   886
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   888
     * The classes {@link LocalDate} and {@link LocalTime} implement {@code TemporalAdjuster},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
     * thus this method can be used to change the date, time or offset:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
     *  result = localDateTime.with(date);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   892
     *  result = localDateTime.with(time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   893
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   896
     * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   897
     * specified adjuster passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   898
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   899
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   900
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   901
     * @param adjuster the adjuster to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   902
     * @return a {@code LocalDateTime} based on {@code this} with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   903
     * @throws DateTimeException if the adjustment cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
    public LocalDateTime with(TemporalAdjuster adjuster) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
        // optimizations
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
        if (adjuster instanceof LocalDate) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
            return with((LocalDate) adjuster, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   911
        } else if (adjuster instanceof LocalTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
            return with(date, (LocalTime) adjuster);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
        } else if (adjuster instanceof LocalDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
            return (LocalDateTime) adjuster;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   915
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
        return (LocalDateTime) adjuster.adjustInto(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   917
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   918
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   920
     * Returns a copy of this date-time with the specified field set to a new value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   921
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   922
     * This returns a {@code LocalDateTime}, based on this one, with the value
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   923
     * for the specified field changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   924
     * This can be used to change any supported field, such as the year, month or day-of-month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   925
     * If it is not possible to set the value, because the field is not supported or for
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   926
     * some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   927
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   928
     * In some cases, changing the specified field can cause the resulting date-time to become invalid,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   929
     * such as changing the month from 31st January to February would make the day-of-month invalid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   930
     * In cases like this, the field is responsible for resolving the date. Typically it will choose
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   931
     * the previous valid date, which would be the last valid day of February in this example.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   932
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   933
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   934
     * The {@link #isSupported(TemporalField) supported fields} will behave as per
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   935
     * the matching method on {@link LocalDate#with(TemporalField, long) LocalDate}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   936
     * or {@link LocalTime#with(TemporalField, long) LocalTime}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   937
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   938
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   939
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   940
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   941
     * passing {@code this} as the argument. In this case, the field determines
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   942
     * whether and how to adjust the instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   943
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   944
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   945
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   946
     * @param field  the field to set in the result, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   947
     * @param newValue  the new value of the field in the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   948
     * @return a {@code LocalDateTime} based on {@code this} with the specified field set, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   949
     * @throws DateTimeException if the field cannot be set
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   950
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   951
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   952
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   953
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   954
    public LocalDateTime with(TemporalField field, long newValue) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   955
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   956
            ChronoField f = (ChronoField) field;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   957
            if (f.isTimeBased()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   958
                return with(date, time.with(field, newValue));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   959
            } else {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   960
                return with(date.with(field, newValue), time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   961
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   962
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   963
        return field.adjustInto(this, newValue);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   964
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   965
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   966
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   967
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   968
     * Returns a copy of this {@code LocalDateTime} with the year altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   969
     * The time does not affect the calculation and will be the same in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   970
     * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   971
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   972
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   973
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   974
     * @param year  the year to set in the result, from MIN_YEAR to MAX_YEAR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   975
     * @return a {@code LocalDateTime} based on this date-time with the requested year, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   976
     * @throws DateTimeException if the year value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   977
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   978
    public LocalDateTime withYear(int year) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   979
        return with(date.withYear(year), time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   980
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   981
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   982
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   983
     * Returns a copy of this {@code LocalDateTime} with the month-of-year altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   984
     * The time does not affect the calculation and will be the same in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   985
     * If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   986
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   987
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   988
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   989
     * @param month  the month-of-year to set in the result, from 1 (January) to 12 (December)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   990
     * @return a {@code LocalDateTime} based on this date-time with the requested month, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   991
     * @throws DateTimeException if the month-of-year value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   992
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   993
    public LocalDateTime withMonth(int month) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   994
        return with(date.withMonth(month), time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   995
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   996
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   997
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   998
     * Returns a copy of this {@code LocalDateTime} with the day-of-month altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   999
     * If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1000
     * The time does not affect the calculation and will be the same in the result.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1001
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1002
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1003
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1004
     * @param dayOfMonth  the day-of-month to set in the result, from 1 to 28-31
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1005
     * @return a {@code LocalDateTime} based on this date-time with the requested day, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1006
     * @throws DateTimeException if the day-of-month value is invalid,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1007
     *  or if the day-of-month is invalid for the month-year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1008
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1009
    public LocalDateTime withDayOfMonth(int dayOfMonth) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1010
        return with(date.withDayOfMonth(dayOfMonth), time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1011
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1012
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1013
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1014
     * Returns a copy of this {@code LocalDateTime} with the day-of-year altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1015
     * If the resulting {@code LocalDateTime} is invalid, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1016
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1017
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1018
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1019
     * @param dayOfYear  the day-of-year to set in the result, from 1 to 365-366
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1020
     * @return a {@code LocalDateTime} based on this date with the requested day, not null
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1021
     * @throws DateTimeException if the day-of-year value is invalid,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1022
     *  or if the day-of-year is invalid for the year
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1023
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1024
    public LocalDateTime withDayOfYear(int dayOfYear) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1025
        return with(date.withDayOfYear(dayOfYear), time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1026
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1027
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1028
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1029
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1030
     * Returns a copy of this {@code LocalDateTime} with the hour-of-day value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1031
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1032
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1033
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1034
     * @param hour  the hour-of-day to set in the result, from 0 to 23
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1035
     * @return a {@code LocalDateTime} based on this date-time with the requested hour, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1036
     * @throws DateTimeException if the hour value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1037
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1038
    public LocalDateTime withHour(int hour) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1039
        LocalTime newTime = time.withHour(hour);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1040
        return with(date, newTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1041
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1042
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1043
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1044
     * Returns a copy of this {@code LocalDateTime} with the minute-of-hour value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1045
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1046
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1047
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1048
     * @param minute  the minute-of-hour to set in the result, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1049
     * @return a {@code LocalDateTime} based on this date-time with the requested minute, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1050
     * @throws DateTimeException if the minute value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1051
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1052
    public LocalDateTime withMinute(int minute) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1053
        LocalTime newTime = time.withMinute(minute);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1054
        return with(date, newTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1055
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1056
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1057
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1058
     * Returns a copy of this {@code LocalDateTime} with the second-of-minute value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1059
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1060
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1061
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1062
     * @param second  the second-of-minute to set in the result, from 0 to 59
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1063
     * @return a {@code LocalDateTime} based on this date-time with the requested second, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1064
     * @throws DateTimeException if the second value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1065
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1066
    public LocalDateTime withSecond(int second) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1067
        LocalTime newTime = time.withSecond(second);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1068
        return with(date, newTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1069
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1070
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1071
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1072
     * Returns a copy of this {@code LocalDateTime} with the nano-of-second value altered.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1073
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1074
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1075
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1076
     * @param nanoOfSecond  the nano-of-second to set in the result, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1077
     * @return a {@code LocalDateTime} based on this date-time with the requested nanosecond, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1078
     * @throws DateTimeException if the nano value is invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1079
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1080
    public LocalDateTime withNano(int nanoOfSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1081
        LocalTime newTime = time.withNano(nanoOfSecond);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1082
        return with(date, newTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1083
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1084
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1085
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1086
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1087
     * Returns a copy of this {@code LocalDateTime} with the time truncated.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1088
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1089
     * Truncation returns a copy of the original date-time with fields
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1090
     * smaller than the specified unit set to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1091
     * For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1092
     * will set the second-of-minute and nano-of-second field to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1093
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1094
     * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1095
     * that divides into the length of a standard day without remainder.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1096
     * This includes all supplied time units on {@link ChronoUnit} and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1097
     * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1098
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1099
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1100
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1101
     * @param unit  the unit to truncate to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1102
     * @return a {@code LocalDateTime} based on this date-time with the time truncated, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1103
     * @throws DateTimeException if unable to truncate
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1104
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1105
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1106
    public LocalDateTime truncatedTo(TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1107
        return with(date, time.truncatedTo(unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1108
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1109
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1110
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1111
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1112
     * Returns a copy of this date-time with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1113
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1114
     * This returns a {@code LocalDateTime}, based on this one, with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1115
     * The amount is typically {@link Period} or {@link Duration} but may be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1116
     * any other type implementing the {@link TemporalAmount} interface.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1117
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1118
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1119
     * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1120
     * to implement the addition in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1121
     * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1122
     * of the amount implementation to determine if it can be successfully added.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1123
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1124
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1125
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1126
     * @param amountToAdd  the amount to add, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1127
     * @return a {@code LocalDateTime} based on this date-time with the addition made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1128
     * @throws DateTimeException if the addition cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1129
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1130
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1131
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1132
    public LocalDateTime plus(TemporalAmount amountToAdd) {
20517
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1133
        if (amountToAdd instanceof Period) {
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1134
            Period periodToAdd = (Period) amountToAdd;
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1135
            return with(date.plus(periodToAdd), time);
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1136
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1137
        Objects.requireNonNull(amountToAdd, "amountToAdd");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1138
        return (LocalDateTime) amountToAdd.addTo(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1139
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1140
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1141
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1142
     * Returns a copy of this date-time with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1143
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1144
     * This returns a {@code LocalDateTime}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1145
     * in terms of the unit added. If it is not possible to add the amount, because the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1146
     * unit is not supported or for some other reason, an exception is thrown.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1147
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1148
     * If the field is a {@link ChronoUnit} then the addition is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1149
     * Date units are added as per {@link LocalDate#plus(long, TemporalUnit)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1150
     * Time units are added as per {@link LocalTime#plus(long, TemporalUnit)} with
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1151
     * any overflow in days added equivalent to using {@link #plusDays(long)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1152
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1153
     * If the field is not a {@code ChronoUnit}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1154
     * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1155
     * passing {@code this} as the argument. In this case, the unit determines
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1156
     * whether and how to perform the addition.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1157
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1158
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1159
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1160
     * @param amountToAdd  the amount of the unit to add to the result, may be negative
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1161
     * @param unit  the unit of the amount to add, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1162
     * @return a {@code LocalDateTime} based on this date-time with the specified amount added, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1163
     * @throws DateTimeException if the addition cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1164
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1165
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1166
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1167
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1168
    public LocalDateTime plus(long amountToAdd, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1169
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1170
            ChronoUnit f = (ChronoUnit) unit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1171
            switch (f) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1172
                case NANOS: return plusNanos(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1173
                case MICROS: return plusDays(amountToAdd / MICROS_PER_DAY).plusNanos((amountToAdd % MICROS_PER_DAY) * 1000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1174
                case MILLIS: return plusDays(amountToAdd / MILLIS_PER_DAY).plusNanos((amountToAdd % MILLIS_PER_DAY) * 1000_000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1175
                case SECONDS: return plusSeconds(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1176
                case MINUTES: return plusMinutes(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1177
                case HOURS: return plusHours(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1178
                case HALF_DAYS: return plusDays(amountToAdd / 256).plusHours((amountToAdd % 256) * 12);  // no overflow (256 is multiple of 2)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1179
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1180
            return with(date.plus(amountToAdd, unit), time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1181
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1182
        return unit.addTo(this, amountToAdd);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1183
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1184
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1185
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1186
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1187
     * Returns a copy of this {@code LocalDateTime} with the specified period in years added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1188
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1189
     * This method adds the specified amount to the years field in three steps:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1190
     * <ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1191
     * <li>Add the input years to the year field</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1192
     * <li>Check if the resulting date would be invalid</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1193
     * <li>Adjust the day-of-month to the last valid day if necessary</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1194
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1195
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1196
     * For example, 2008-02-29 (leap year) plus one year would result in the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1197
     * invalid date 2009-02-29 (standard year). Instead of returning an invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1198
     * result, the last valid day of the month, 2009-02-28, is selected instead.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1199
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1200
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1201
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1202
     * @param years  the years to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1203
     * @return a {@code LocalDateTime} based on this date-time with the years added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1204
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1205
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1206
    public LocalDateTime plusYears(long years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1207
        LocalDate newDate = date.plusYears(years);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1208
        return with(newDate, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1209
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1210
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1211
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1212
     * Returns a copy of this {@code LocalDateTime} with the specified period in months added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1213
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1214
     * This method adds the specified amount to the months field in three steps:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1215
     * <ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1216
     * <li>Add the input months to the month-of-year field</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1217
     * <li>Check if the resulting date would be invalid</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1218
     * <li>Adjust the day-of-month to the last valid day if necessary</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1219
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1220
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1221
     * For example, 2007-03-31 plus one month would result in the invalid date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1222
     * 2007-04-31. Instead of returning an invalid result, the last valid day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1223
     * of the month, 2007-04-30, is selected instead.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1224
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1225
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1226
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1227
     * @param months  the months to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1228
     * @return a {@code LocalDateTime} based on this date-time with the months added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1229
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1230
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1231
    public LocalDateTime plusMonths(long months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1232
        LocalDate newDate = date.plusMonths(months);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1233
        return with(newDate, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1234
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1235
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1236
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1237
     * Returns a copy of this {@code LocalDateTime} with the specified period in weeks added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1238
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1239
     * This method adds the specified amount in weeks to the days field incrementing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1240
     * the month and year fields as necessary to ensure the result remains valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1241
     * The result is only invalid if the maximum/minimum year is exceeded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1242
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1243
     * For example, 2008-12-31 plus one week would result in 2009-01-07.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1244
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1245
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1246
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1247
     * @param weeks  the weeks to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1248
     * @return a {@code LocalDateTime} based on this date-time with the weeks added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1249
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1250
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1251
    public LocalDateTime plusWeeks(long weeks) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1252
        LocalDate newDate = date.plusWeeks(weeks);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1253
        return with(newDate, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1254
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1255
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1256
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1257
     * Returns a copy of this {@code LocalDateTime} with the specified period in days added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1258
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1259
     * This method adds the specified amount to the days field incrementing the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1260
     * month and year fields as necessary to ensure the result remains valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1261
     * The result is only invalid if the maximum/minimum year is exceeded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1262
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1263
     * For example, 2008-12-31 plus one day would result in 2009-01-01.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1264
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1265
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1266
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1267
     * @param days  the days to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1268
     * @return a {@code LocalDateTime} based on this date-time with the days added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1269
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1270
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1271
    public LocalDateTime plusDays(long days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1272
        LocalDate newDate = date.plusDays(days);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1273
        return with(newDate, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1274
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1275
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1276
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1277
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1278
     * Returns a copy of this {@code LocalDateTime} with the specified period in hours added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1279
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1280
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1281
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1282
     * @param hours  the hours to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1283
     * @return a {@code LocalDateTime} based on this date-time with the hours added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1284
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1285
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1286
    public LocalDateTime plusHours(long hours) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1287
        return plusWithOverflow(date, hours, 0, 0, 0, 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1288
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1290
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1291
     * Returns a copy of this {@code LocalDateTime} with the specified period in minutes added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1292
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1293
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1294
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1295
     * @param minutes  the minutes to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1296
     * @return a {@code LocalDateTime} based on this date-time with the minutes added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1297
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1298
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1299
    public LocalDateTime plusMinutes(long minutes) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1300
        return plusWithOverflow(date, 0, minutes, 0, 0, 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1301
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1302
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1303
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1304
     * Returns a copy of this {@code LocalDateTime} with the specified period in seconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1305
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1306
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1307
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1308
     * @param seconds  the seconds to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1309
     * @return a {@code LocalDateTime} based on this date-time with the seconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1310
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1311
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1312
    public LocalDateTime plusSeconds(long seconds) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1313
        return plusWithOverflow(date, 0, 0, seconds, 0, 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1314
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1315
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1316
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1317
     * Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1318
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1319
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1320
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1321
     * @param nanos  the nanos to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1322
     * @return a {@code LocalDateTime} based on this date-time with the nanoseconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1323
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1324
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1325
    public LocalDateTime plusNanos(long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1326
        return plusWithOverflow(date, 0, 0, 0, nanos, 1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1327
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1328
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1329
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1330
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1331
     * Returns a copy of this date-time with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1332
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1333
     * This returns a {@code LocalDateTime}, based on this one, with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1334
     * The amount is typically {@link Period} or {@link Duration} but may be
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1335
     * any other type implementing the {@link TemporalAmount} interface.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1336
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1337
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1338
     * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1339
     * to implement the subtraction in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1340
     * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1341
     * of the amount implementation to determine if it can be successfully subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1342
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1343
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1344
     *
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1345
     * @param amountToSubtract  the amount to subtract, not null
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1346
     * @return a {@code LocalDateTime} based on this date-time with the subtraction made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1347
     * @throws DateTimeException if the subtraction cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1348
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1349
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1350
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1351
    public LocalDateTime minus(TemporalAmount amountToSubtract) {
20517
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1352
        if (amountToSubtract instanceof Period) {
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1353
            Period periodToSubtract = (Period) amountToSubtract;
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1354
            return with(date.minus(periodToSubtract), time);
1336a85b3d52 8023764: Optimize Period addition
rriggs
parents: 19841
diff changeset
  1355
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1356
        Objects.requireNonNull(amountToSubtract, "amountToSubtract");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1357
        return (LocalDateTime) amountToSubtract.subtractFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1358
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1359
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1360
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1361
     * Returns a copy of this date-time with the specified amount subtracted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1362
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1363
     * This returns a {@code LocalDateTime}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1364
     * in terms of the unit subtracted. If it is not possible to subtract the amount,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1365
     * because the unit is not supported or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1366
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1367
     * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1368
     * See that method for a full description of how addition, and thus subtraction, works.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1369
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1370
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1371
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1372
     * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1373
     * @param unit  the unit of the amount to subtract, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1374
     * @return a {@code LocalDateTime} based on this date-time with the specified amount subtracted, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1375
     * @throws DateTimeException if the subtraction cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1376
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1377
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1378
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1379
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1380
    public LocalDateTime minus(long amountToSubtract, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1381
        return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1382
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1383
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1384
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1385
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1386
     * Returns a copy of this {@code LocalDateTime} with the specified period in years subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1387
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1388
     * This method subtracts the specified amount from the years field in three steps:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1389
     * <ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1390
     * <li>Subtract the input years from the year field</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1391
     * <li>Check if the resulting date would be invalid</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1392
     * <li>Adjust the day-of-month to the last valid day if necessary</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1393
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1394
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1395
     * For example, 2008-02-29 (leap year) minus one year would result in the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1396
     * invalid date 2009-02-29 (standard year). Instead of returning an invalid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1397
     * result, the last valid day of the month, 2009-02-28, is selected instead.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1398
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1399
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1400
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1401
     * @param years  the years to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1402
     * @return a {@code LocalDateTime} based on this date-time with the years subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1403
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1404
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1405
    public LocalDateTime minusYears(long years) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1406
        return (years == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-years));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1407
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1408
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1409
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1410
     * Returns a copy of this {@code LocalDateTime} with the specified period in months subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1411
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1412
     * This method subtracts the specified amount from the months field in three steps:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1413
     * <ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1414
     * <li>Subtract the input months from the month-of-year field</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1415
     * <li>Check if the resulting date would be invalid</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1416
     * <li>Adjust the day-of-month to the last valid day if necessary</li>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1417
     * </ol>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1418
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1419
     * For example, 2007-03-31 minus one month would result in the invalid date
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1420
     * 2007-04-31. Instead of returning an invalid result, the last valid day
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1421
     * of the month, 2007-04-30, is selected instead.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1422
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1423
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1424
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1425
     * @param months  the months to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1426
     * @return a {@code LocalDateTime} based on this date-time with the months subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1427
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1428
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1429
    public LocalDateTime minusMonths(long months) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1430
        return (months == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-months));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1431
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1432
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1433
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1434
     * Returns a copy of this {@code LocalDateTime} with the specified period in weeks subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1435
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1436
     * This method subtracts the specified amount in weeks from the days field decrementing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1437
     * the month and year fields as necessary to ensure the result remains valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1438
     * The result is only invalid if the maximum/minimum year is exceeded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1439
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1440
     * For example, 2009-01-07 minus one week would result in 2008-12-31.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1441
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1442
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1443
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1444
     * @param weeks  the weeks to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1445
     * @return a {@code LocalDateTime} based on this date-time with the weeks subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1446
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1447
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1448
    public LocalDateTime minusWeeks(long weeks) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1449
        return (weeks == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeks));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1450
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1451
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1452
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1453
     * Returns a copy of this {@code LocalDateTime} with the specified period in days subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1454
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1455
     * This method subtracts the specified amount from the days field incrementing the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1456
     * month and year fields as necessary to ensure the result remains valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1457
     * The result is only invalid if the maximum/minimum year is exceeded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1458
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1459
     * For example, 2009-01-01 minus one day would result in 2008-12-31.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1460
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1461
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1462
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1463
     * @param days  the days to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1464
     * @return a {@code LocalDateTime} based on this date-time with the days subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1465
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1466
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1467
    public LocalDateTime minusDays(long days) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1468
        return (days == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-days));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1469
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1470
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1471
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1472
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1473
     * Returns a copy of this {@code LocalDateTime} with the specified period in hours subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1474
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1475
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1476
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1477
     * @param hours  the hours to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1478
     * @return a {@code LocalDateTime} based on this date-time with the hours subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1479
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1480
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1481
    public LocalDateTime minusHours(long hours) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1482
        return plusWithOverflow(date, hours, 0, 0, 0, -1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1483
   }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1484
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1485
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1486
     * Returns a copy of this {@code LocalDateTime} with the specified period in minutes subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1487
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1488
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1489
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1490
     * @param minutes  the minutes to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1491
     * @return a {@code LocalDateTime} based on this date-time with the minutes subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1492
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1493
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1494
    public LocalDateTime minusMinutes(long minutes) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1495
        return plusWithOverflow(date, 0, minutes, 0, 0, -1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1496
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1497
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1498
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1499
     * Returns a copy of this {@code LocalDateTime} with the specified period in seconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1500
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1501
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1502
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1503
     * @param seconds  the seconds to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1504
     * @return a {@code LocalDateTime} based on this date-time with the seconds subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1505
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1506
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1507
    public LocalDateTime minusSeconds(long seconds) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1508
        return plusWithOverflow(date, 0, 0, seconds, 0, -1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1509
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1510
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1511
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1512
     * Returns a copy of this {@code LocalDateTime} with the specified period in nanoseconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1513
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1514
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1515
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1516
     * @param nanos  the nanos to subtract, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1517
     * @return a {@code LocalDateTime} based on this date-time with the nanoseconds subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1518
     * @throws DateTimeException if the result exceeds the supported date range
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1519
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1520
    public LocalDateTime minusNanos(long nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1521
        return plusWithOverflow(date, 0, 0, 0, nanos, -1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1522
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1523
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1524
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1525
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1526
     * Returns a copy of this {@code LocalDateTime} with the specified period added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1527
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1528
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1529
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1530
     * @param newDate  the new date to base the calculation on, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1531
     * @param hours  the hours to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1532
     * @param minutes the minutes to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1533
     * @param seconds the seconds to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1534
     * @param nanos the nanos to add, may be negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1535
     * @param sign  the sign to determine add or subtract
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1536
     * @return the combined result, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1537
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1538
    private LocalDateTime plusWithOverflow(LocalDate newDate, long hours, long minutes, long seconds, long nanos, int sign) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1539
        // 9223372036854775808 long, 2147483648 int
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1540
        if ((hours | minutes | seconds | nanos) == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1541
            return with(newDate, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1542
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1543
        long totDays = nanos / NANOS_PER_DAY +             //   max/24*60*60*1B
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1544
                seconds / SECONDS_PER_DAY +                //   max/24*60*60
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1545
                minutes / MINUTES_PER_DAY +                //   max/24*60
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1546
                hours / HOURS_PER_DAY;                     //   max/24
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1547
        totDays *= sign;                                   // total max*0.4237...
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1548
        long totNanos = nanos % NANOS_PER_DAY +                    //   max  86400000000000
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1549
                (seconds % SECONDS_PER_DAY) * NANOS_PER_SECOND +   //   max  86400000000000
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1550
                (minutes % MINUTES_PER_DAY) * NANOS_PER_MINUTE +   //   max  86400000000000
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1551
                (hours % HOURS_PER_DAY) * NANOS_PER_HOUR;          //   max  86400000000000
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1552
        long curNoD = time.toNanoOfDay();                       //   max  86400000000000
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1553
        totNanos = totNanos * sign + curNoD;                    // total 432000000000000
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1554
        totDays += Math.floorDiv(totNanos, NANOS_PER_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1555
        long newNoD = Math.floorMod(totNanos, NANOS_PER_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1556
        LocalTime newTime = (newNoD == curNoD ? time : LocalTime.ofNanoOfDay(newNoD));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1557
        return with(newDate.plusDays(totDays), newTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1558
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1559
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1560
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1561
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1562
     * Queries this date-time using the specified query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1563
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1564
     * This queries this date-time using the specified query strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1565
     * The {@code TemporalQuery} object defines the logic to be used to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1566
     * obtain the result. Read the documentation of the query to understand
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1567
     * what the result of this method will be.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1568
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1569
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1570
     * {@link java.time.temporal.TemporalQuery#queryFrom(TemporalAccessor)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1571
     * specified query passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1572
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1573
     * @param <R> the type of the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1574
     * @param query  the query to invoke, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1575
     * @return the query result, null may be returned (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1576
     * @throws DateTimeException if unable to query (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1577
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1578
     */
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1579
    @SuppressWarnings("unchecked")
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1580
    @Override  // override for Javadoc
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1581
    public <R> R query(TemporalQuery<R> query) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
  1582
        if (query == TemporalQueries.localDate()) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1583
            return (R) date;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1584
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1585
        return ChronoLocalDateTime.super.query(query);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1586
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1587
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1588
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1589
     * Adjusts the specified temporal object to have the same date and time as this object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1590
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1591
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1592
     * with the date and time changed to be the same as this.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1593
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1594
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1595
     * twice, passing {@link ChronoField#EPOCH_DAY} and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1596
     * {@link ChronoField#NANO_OF_DAY} as the fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1597
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1598
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1599
     * {@link Temporal#with(TemporalAdjuster)}:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1600
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1601
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1602
     *   temporal = thisLocalDateTime.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1603
     *   temporal = temporal.with(thisLocalDateTime);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1604
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1605
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1606
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1607
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1608
     * @param temporal  the target object to be adjusted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1609
     * @return the adjusted object, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1610
     * @throws DateTimeException if unable to make the adjustment
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1611
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1612
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1613
    @Override  // override for Javadoc
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1614
    public Temporal adjustInto(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1615
        return ChronoLocalDateTime.super.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1616
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1617
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1618
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1619
     * Calculates the amount of time until another date-time in terms of the specified unit.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1620
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1621
     * This calculates the amount of time between two {@code LocalDateTime}
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1622
     * objects in terms of a single {@code TemporalUnit}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1623
     * The start and end points are {@code this} and the specified date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1624
     * The result will be negative if the end is before the start.
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1625
     * The {@code Temporal} passed to this method is converted to a
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1626
     * {@code LocalDateTime} using {@link #from(TemporalAccessor)}.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1627
     * For example, the amount in days between two date-times can be calculated
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1628
     * using {@code startDateTime.until(endDateTime, DAYS)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1629
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1630
     * The calculation returns a whole number, representing the number of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1631
     * complete units between the two date-times.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1632
     * For example, the amount in months between 2012-06-15T00:00 and 2012-08-14T23:59
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1633
     * will only be one month as it is one minute short of two months.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1634
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1635
     * There are two equivalent ways of using this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1636
     * The first is to invoke this method.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1637
     * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1638
     * <pre>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1639
     *   // these two lines are equivalent
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1640
     *   amount = start.until(end, MONTHS);
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1641
     *   amount = MONTHS.between(start, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1642
     * </pre>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1643
     * The choice should be made based on which makes the code more readable.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1644
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1645
     * The calculation is implemented in this method for {@link ChronoUnit}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1646
     * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1647
     * {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS}, {@code DAYS},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1648
     * {@code WEEKS}, {@code MONTHS}, {@code YEARS}, {@code DECADES},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1649
     * {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS} are supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1650
     * Other {@code ChronoUnit} values will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1651
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1652
     * If the unit is not a {@code ChronoUnit}, then the result of this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1653
     * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1654
     * passing {@code this} as the first argument and the converted input temporal
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1655
     * as the second argument.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1656
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1657
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1658
     *
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1659
     * @param endExclusive  the end date, exclusive, which is converted to a {@code LocalDateTime}, not null
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1660
     * @param unit  the unit to measure the amount in, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1661
     * @return the amount of time between this date-time and the end date-time
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1662
     * @throws DateTimeException if the amount cannot be calculated, or the end
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1663
     *  temporal cannot be converted to a {@code LocalDateTime}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1664
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1665
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1666
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1667
    @Override
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1668
    public long until(Temporal endExclusive, TemporalUnit unit) {
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1669
        LocalDateTime end = LocalDateTime.from(endExclusive);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1670
        if (unit instanceof ChronoUnit) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1671
            if (unit.isTimeBased()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1672
                long amount = date.daysUntil(end.date);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1673
                if (amount == 0) {
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1674
                    return time.until(end.time, unit);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1675
                }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1676
                long timePart = end.time.toNanoOfDay() - time.toNanoOfDay();
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1677
                if (amount > 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1678
                    amount--;  // safe
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1679
                    timePart += NANOS_PER_DAY;  // safe
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1680
                } else {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1681
                    amount++;  // safe
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1682
                    timePart -= NANOS_PER_DAY;  // safe
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1683
                }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1684
                switch ((ChronoUnit) unit) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1685
                    case NANOS:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1686
                        amount = Math.multiplyExact(amount, NANOS_PER_DAY);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1687
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1688
                    case MICROS:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1689
                        amount = Math.multiplyExact(amount, MICROS_PER_DAY);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1690
                        timePart = timePart / 1000;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1691
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1692
                    case MILLIS:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1693
                        amount = Math.multiplyExact(amount, MILLIS_PER_DAY);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1694
                        timePart = timePart / 1_000_000;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1695
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1696
                    case SECONDS:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1697
                        amount = Math.multiplyExact(amount, SECONDS_PER_DAY);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1698
                        timePart = timePart / NANOS_PER_SECOND;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1699
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1700
                    case MINUTES:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1701
                        amount = Math.multiplyExact(amount, MINUTES_PER_DAY);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1702
                        timePart = timePart / NANOS_PER_MINUTE;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1703
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1704
                    case HOURS:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1705
                        amount = Math.multiplyExact(amount, HOURS_PER_DAY);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1706
                        timePart = timePart / NANOS_PER_HOUR;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1707
                        break;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1708
                    case HALF_DAYS:
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1709
                        amount = Math.multiplyExact(amount, 2);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1710
                        timePart = timePart / (NANOS_PER_HOUR * 12);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1711
                        break;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1712
                }
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1713
                return Math.addExact(amount, timePart);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1714
            }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1715
            LocalDate endDate = end.date;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1716
            if (endDate.isAfter(date) && end.time.isBefore(time)) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1717
                endDate = endDate.minusDays(1);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1718
            } else if (endDate.isBefore(date) && end.time.isAfter(time)) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1719
                endDate = endDate.plusDays(1);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1720
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1721
            return date.until(endDate, unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1722
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 20517
diff changeset
  1723
        return unit.between(this, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1724
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1725
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1726
    /**
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1727
     * Formats this date-time using the specified formatter.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1728
     * <p>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1729
     * This date-time will be passed to the formatter to produce a string.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1730
     *
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1731
     * @param formatter  the formatter to use, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1732
     * @return the formatted date-time string, not null
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1733
     * @throws DateTimeException if an error occurs during printing
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1734
     */
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1735
    @Override  // override for Javadoc and performance
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1736
    public String format(DateTimeFormatter formatter) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1737
        Objects.requireNonNull(formatter, "formatter");
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1738
        return formatter.format(this);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1739
    }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1740
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1741
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1742
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1743
     * Combines this date-time with an offset to create an {@code OffsetDateTime}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1744
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1745
     * This returns an {@code OffsetDateTime} formed from this date-time at the specified offset.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1746
     * All possible combinations of date-time and offset are valid.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1747
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1748
     * @param offset  the offset to combine with, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1749
     * @return the offset date-time formed from this date-time and the specified offset, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1750
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1751
    public OffsetDateTime atOffset(ZoneOffset offset) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1752
        return OffsetDateTime.of(this, offset);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1753
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1754
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1755
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1756
     * Combines this date-time with a time-zone to create a {@code ZonedDateTime}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1757
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1758
     * This returns a {@code ZonedDateTime} formed from this date-time at the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1759
     * specified time-zone. The result will match this date-time as closely as possible.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1760
     * Time-zone rules, such as daylight savings, mean that not every local date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1761
     * is valid for the specified zone, thus the local date-time may be adjusted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1762
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1763
     * The local date-time is resolved to a single instant on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1764
     * This is achieved by finding a valid offset from UTC/Greenwich for the local
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1765
     * date-time as defined by the {@link ZoneRules rules} of the zone ID.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1766
     *<p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1767
     * In most cases, there is only one valid offset for a local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1768
     * In the case of an overlap, where clocks are set back, there are two valid offsets.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1769
     * This method uses the earlier offset typically corresponding to "summer".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1770
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1771
     * In the case of a gap, where clocks jump forward, there is no valid offset.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1772
     * Instead, the local date-time is adjusted to be later by the length of the gap.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1773
     * For a typical one hour daylight savings change, the local date-time will be
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1774
     * moved one hour later into the offset typically corresponding to "summer".
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1775
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1776
     * To obtain the later offset during an overlap, call
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1777
     * {@link ZonedDateTime#withLaterOffsetAtOverlap()} on the result of this method.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1778
     * To throw an exception when there is a gap or overlap, use
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1779
     * {@link ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId)}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1780
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1781
     * @param zone  the time-zone to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1782
     * @return the zoned date-time formed from this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1783
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1784
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1785
    public ZonedDateTime atZone(ZoneId zone) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1786
        return ZonedDateTime.of(this, zone);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1787
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1788
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1789
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1790
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1791
     * Compares this date-time to another date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1792
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1793
     * The comparison is primarily based on the date-time, from earliest to latest.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1794
     * It is "consistent with equals", as defined by {@link Comparable}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1795
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1796
     * If all the date-times being compared are instances of {@code LocalDateTime},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1797
     * then the comparison will be entirely based on the date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1798
     * If some dates being compared are in different chronologies, then the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1799
     * chronology is also considered, see {@link ChronoLocalDateTime#compareTo}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1800
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1801
     * @param other  the other date-time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1802
     * @return the comparator value, negative if less, positive if greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1803
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1804
    @Override  // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1805
    public int compareTo(ChronoLocalDateTime<?> other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1806
        if (other instanceof LocalDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1807
            return compareTo0((LocalDateTime) other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1808
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1809
        return ChronoLocalDateTime.super.compareTo(other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1810
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1811
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1812
    private int compareTo0(LocalDateTime other) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1813
        int cmp = date.compareTo0(other.toLocalDate());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1814
        if (cmp == 0) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1815
            cmp = time.compareTo(other.toLocalTime());
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1816
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1817
        return cmp;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1818
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1819
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1820
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1821
     * Checks if this date-time is after the specified date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1822
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1823
     * This checks to see if this date-time represents a point on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1824
     * local time-line after the other date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1825
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1826
     *   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1827
     *   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1828
     *   a.isAfter(b) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1829
     *   a.isAfter(a) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1830
     *   b.isAfter(a) == true
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1831
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1832
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1833
     * This method only considers the position of the two date-times on the local time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1834
     * It does not take into account the chronology, or calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1835
     * This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1836
     * but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1837
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1838
     * @param other  the other date-time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1839
     * @return true if this date-time is after the specified date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1840
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1841
    @Override  // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1842
    public boolean isAfter(ChronoLocalDateTime<?> other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1843
        if (other instanceof LocalDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1844
            return compareTo0((LocalDateTime) other) > 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1845
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1846
        return ChronoLocalDateTime.super.isAfter(other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1847
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1848
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1849
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1850
     * Checks if this date-time is before the specified date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1851
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1852
     * This checks to see if this date-time represents a point on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1853
     * local time-line before the other date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1854
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1855
     *   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1856
     *   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1857
     *   a.isBefore(b) == true
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1858
     *   a.isBefore(a) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1859
     *   b.isBefore(a) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1860
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1861
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1862
     * This method only considers the position of the two date-times on the local time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1863
     * It does not take into account the chronology, or calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1864
     * This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1865
     * but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1866
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1867
     * @param other  the other date-time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1868
     * @return true if this date-time is before the specified date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1869
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1870
    @Override  // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1871
    public boolean isBefore(ChronoLocalDateTime<?> other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1872
        if (other instanceof LocalDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1873
            return compareTo0((LocalDateTime) other) < 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1874
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1875
        return ChronoLocalDateTime.super.isBefore(other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1876
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1877
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1878
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1879
     * Checks if this date-time is equal to the specified date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1880
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1881
     * This checks to see if this date-time represents the same point on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1882
     * local time-line as the other date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1883
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1884
     *   LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1885
     *   LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1886
     *   a.isEqual(b) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1887
     *   a.isEqual(a) == true
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1888
     *   b.isEqual(a) == false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1889
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1890
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1891
     * This method only considers the position of the two date-times on the local time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1892
     * It does not take into account the chronology, or calendar system.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1893
     * This is different from the comparison in {@link #compareTo(ChronoLocalDateTime)},
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1894
     * but is the same approach as {@link ChronoLocalDateTime#timeLineOrder()}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1895
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1896
     * @param other  the other date-time to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1897
     * @return true if this date-time is equal to the specified date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1898
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1899
    @Override  // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1900
    public boolean isEqual(ChronoLocalDateTime<?> other) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1901
        if (other instanceof LocalDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1902
            return compareTo0((LocalDateTime) other) == 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1903
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1904
        return ChronoLocalDateTime.super.isEqual(other);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1905
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1906
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1907
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1908
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1909
     * Checks if this date-time is equal to another date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1910
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1911
     * Compares this {@code LocalDateTime} with another ensuring that the date-time is the same.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1912
     * Only objects of type {@code LocalDateTime} are compared, other types return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1913
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1914
     * @param obj  the object to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1915
     * @return true if this is equal to the other date-time
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1916
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1917
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1918
    public boolean equals(Object obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1919
        if (this == obj) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1920
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1921
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1922
        if (obj instanceof LocalDateTime) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1923
            LocalDateTime other = (LocalDateTime) obj;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1924
            return date.equals(other.date) && time.equals(other.time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1925
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1926
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1927
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1928
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1929
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1930
     * A hash code for this date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1931
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1932
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1933
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1934
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1935
    public int hashCode() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1936
        return date.hashCode() ^ time.hashCode();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1937
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1938
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1939
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1940
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1941
     * Outputs this date-time as a {@code String}, such as {@code 2007-12-03T10:15:30}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1942
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1943
     * The output will be one of the following ISO-8601 formats:
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
  1944
     * <ul>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1945
     * <li>{@code uuuu-MM-dd'T'HH:mm}</li>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1946
     * <li>{@code uuuu-MM-dd'T'HH:mm:ss}</li>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1947
     * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSS}</li>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1948
     * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSS}</li>
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1949
     * <li>{@code uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS}</li>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
  1950
     * </ul>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1951
     * The format used will be the shortest that outputs the full value of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1952
     * the time where the omitted parts are implied to be zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1953
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1954
     * @return a string representation of this date-time, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1955
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1956
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1957
    public String toString() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1958
        return date.toString() + 'T' + time.toString();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1959
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1960
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1961
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1962
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1963
     * Writes the object using a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1964
     * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1965
     * @serialData
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1966
     * <pre>
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1967
     *  out.writeByte(5);  // identifies a LocalDateTime
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1968
     *  // the <a href="../../serialized-form.html#java.time.LocalDate">date</a> excluding the one byte header
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1969
     *  // the <a href="../../serialized-form.html#java.time.LocalTime">time</a> excluding the one byte header
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1970
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1971
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1972
     * @return the instance of {@code Ser}, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1973
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1974
    private Object writeReplace() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1975
        return new Ser(Ser.LOCAL_DATE_TIME_TYPE, this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1976
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1977
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1978
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1979
     * Defend against malicious streams.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1980
     * @return never
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1981
     * @throws InvalidObjectException always
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1982
     */
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1983
    private Object readResolve() throws InvalidObjectException {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1984
        throw new InvalidObjectException("Deserialization via serialization delegate");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1985
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1986
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1987
    void writeExternal(DataOutput out) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1988
        date.writeExternal(out);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1989
        time.writeExternal(out);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1990
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1991
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1992
    static LocalDateTime readExternal(DataInput in) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1993
        LocalDate date = LocalDate.readExternal(in);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1994
        LocalTime time = LocalTime.readExternal(in);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1995
        return LocalDateTime.of(date, time);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1996
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1997
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1998
}