Mon, 17 Apr 2017 11:46:11 -0700
Added tag jdk8u141-b05 for changeset f7fc5fc0e2cb
sundar@1401 | 1 | /* |
sundar@1401 | 2 | * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
sundar@1401 | 3 | * |
sundar@1401 | 4 | * Redistribution and use in source and binary forms, with or without |
sundar@1401 | 5 | * modification, are permitted provided that the following conditions |
sundar@1401 | 6 | * are met: |
sundar@1401 | 7 | * |
sundar@1401 | 8 | * - Redistributions of source code must retain the above copyright |
sundar@1401 | 9 | * notice, this list of conditions and the following disclaimer. |
sundar@1401 | 10 | * |
sundar@1401 | 11 | * - Redistributions in binary form must reproduce the above copyright |
sundar@1401 | 12 | * notice, this list of conditions and the following disclaimer in the |
sundar@1401 | 13 | * documentation and/or other materials provided with the distribution. |
sundar@1401 | 14 | * |
sundar@1401 | 15 | * - Neither the name of Oracle nor the names of its |
sundar@1401 | 16 | * contributors may be used to endorse or promote products derived |
sundar@1401 | 17 | * from this software without specific prior written permission. |
sundar@1401 | 18 | * |
sundar@1401 | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
sundar@1401 | 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
sundar@1401 | 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
sundar@1401 | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
sundar@1401 | 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
sundar@1401 | 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
sundar@1401 | 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
sundar@1401 | 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
sundar@1401 | 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
sundar@1401 | 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
sundar@1401 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
sundar@1401 | 30 | */ |
sundar@1401 | 31 | |
sundar@1401 | 32 | // Converting between #javascript Date and #java8 LocalDateTime with #nashorn |
sundar@1401 | 33 | |
sundar@1401 | 34 | // JavaScript Date with current time |
sundar@1401 | 35 | var d = new Date(); |
sundar@1401 | 36 | print(d); |
sundar@1401 | 37 | |
sundar@1401 | 38 | // Java 8 java.time classes used |
sundar@1401 | 39 | var Instant = java.time.Instant; |
sundar@1401 | 40 | var LocalDateTime = java.time.LocalDateTime; |
sundar@1401 | 41 | var ZoneId = java.time.ZoneId; |
sundar@1401 | 42 | |
sundar@1401 | 43 | // Date.prototype.getTime |
sundar@1401 | 44 | |
sundar@1401 | 45 | // getTime() method returns the numeric value corresponding to the time |
sundar@1401 | 46 | // for the specified date according to universal time. The value returned |
sundar@1401 | 47 | // by the getTime() method is the number of milliseconds since 1 January 1970 00:00:00 UTC. |
sundar@1401 | 48 | // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime |
sundar@1401 | 49 | |
sundar@1401 | 50 | // Java Instant.ofEpochMilli to convert time in milliseconds to Instant object |
sundar@1401 | 51 | // https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#ofEpochMilli-long- |
sundar@1401 | 52 | |
sundar@1401 | 53 | var instant = Instant.ofEpochMilli(d.getTime()); |
sundar@1401 | 54 | |
sundar@1401 | 55 | // Instant to LocalDateTime using LocalDateTime.ofInstant |
sundar@1401 | 56 | // https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#ofInstant-java.time.Instant-java.time.ZoneId- |
sundar@1401 | 57 | |
sundar@1401 | 58 | var ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); |
sundar@1401 | 59 | print(ldt); |
sundar@1401 | 60 | |
sundar@1401 | 61 | // converting a LocalDateTime to JavaScript Date |
sundar@1401 | 62 | // convert LocalDateTime to Instant first |
sundar@1401 | 63 | // https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#atZone-java.time.ZoneId- |
sundar@1401 | 64 | |
sundar@1401 | 65 | var instant = ldt.atZone(ZoneId.systemDefault()).toInstant(); |
sundar@1401 | 66 | |
sundar@1401 | 67 | // instant to to epoch milliseconds |
sundar@1401 | 68 | // https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#toEpochMilli-- |
sundar@1401 | 69 | // and then to JavaScript Date from time in milliseconds |
sundar@1401 | 70 | // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date |
sundar@1401 | 71 | |
sundar@1401 | 72 | var d1 = new Date(instant.toEpochMilli()); |
sundar@1401 | 73 | print(d1); |