< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1215      *
1216      * @param date  the date to combine with, not null
1217      * @return the offset date-time formed from this time and the specified date, not null
1218      */
1219     public OffsetDateTime atDate(LocalDate date) {
1220         return OffsetDateTime.of(date, time, offset);
1221     }
1222 
1223     //-----------------------------------------------------------------------
1224     /**
1225      * Converts this time to epoch nanos based on 1970-01-01Z.
1226      *
1227      * @return the epoch nanos value
1228      */
1229     private long toEpochNano() {
1230         long nod = time.toNanoOfDay();
1231         long offsetNanos = offset.getTotalSeconds() * NANOS_PER_SECOND;
1232         return nod - offsetNanos;
1233     }
1234 














1235     //-----------------------------------------------------------------------
1236     /**
1237      * Compares this {@code OffsetTime} to another time.
1238      * <p>
1239      * The comparison is based first on the UTC equivalent instant, then on the local time.
1240      * It is "consistent with equals", as defined by {@link Comparable}.
1241      * <p>
1242      * For example, the following is the comparator order:
1243      * <ol>
1244      * <li>{@code 10:30+01:00}</li>
1245      * <li>{@code 11:00+01:00}</li>
1246      * <li>{@code 12:00+02:00}</li>
1247      * <li>{@code 11:30+01:00}</li>
1248      * <li>{@code 12:00+01:00}</li>
1249      * <li>{@code 12:30+01:00}</li>
1250      * </ol>
1251      * Values #2 and #3 represent the same instant on the time-line.
1252      * When two values represent the same instant, the local time is compared
1253      * to distinguish them. This step is needed to make the ordering
1254      * consistent with {@code equals()}.


   1 /*
   2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1215      *
1216      * @param date  the date to combine with, not null
1217      * @return the offset date-time formed from this time and the specified date, not null
1218      */
1219     public OffsetDateTime atDate(LocalDate date) {
1220         return OffsetDateTime.of(date, time, offset);
1221     }
1222 
1223     //-----------------------------------------------------------------------
1224     /**
1225      * Converts this time to epoch nanos based on 1970-01-01Z.
1226      *
1227      * @return the epoch nanos value
1228      */
1229     private long toEpochNano() {
1230         long nod = time.toNanoOfDay();
1231         long offsetNanos = offset.getTotalSeconds() * NANOS_PER_SECOND;
1232         return nod - offsetNanos;
1233     }
1234 
1235     /**
1236      * Converts this {@code OffsetTime} to the number of seconds since the epoch
1237      * of 1970-01-01T00:00:00Z.
1238      * <p>
1239      * This returns the number of seconds from the epoch for this {@link OffsetTime}.
1240      *
1241      * @return the number of seconds since the epoch of 1970-01-01T00:00:00Z, may be negative
1242      */
1243     public int toEpochSecond() {
1244         int secs = time.toSecondOfDay();
1245         secs -= offset.getTotalSeconds();
1246         return secs;
1247     }
1248 
1249     //-----------------------------------------------------------------------
1250     /**
1251      * Compares this {@code OffsetTime} to another time.
1252      * <p>
1253      * The comparison is based first on the UTC equivalent instant, then on the local time.
1254      * It is "consistent with equals", as defined by {@link Comparable}.
1255      * <p>
1256      * For example, the following is the comparator order:
1257      * <ol>
1258      * <li>{@code 10:30+01:00}</li>
1259      * <li>{@code 11:00+01:00}</li>
1260      * <li>{@code 12:00+02:00}</li>
1261      * <li>{@code 11:30+01:00}</li>
1262      * <li>{@code 12:00+01:00}</li>
1263      * <li>{@code 12:30+01:00}</li>
1264      * </ol>
1265      * Values #2 and #3 represent the same instant on the time-line.
1266      * When two values represent the same instant, the local time is compared
1267      * to distinguish them. This step is needed to make the ordering
1268      * consistent with {@code equals()}.


< prev index next >