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