Blame openjdk/nashorn/samples/datetime.js

Packit Service 46fadf
/*
Packit Service 46fadf
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
Packit Service 46fadf
 *
Packit Service 46fadf
 * Redistribution and use in source and binary forms, with or without
Packit Service 46fadf
 * modification, are permitted provided that the following conditions
Packit Service 46fadf
 * are met:
Packit Service 46fadf
 *
Packit Service 46fadf
 *   - Redistributions of source code must retain the above copyright
Packit Service 46fadf
 *     notice, this list of conditions and the following disclaimer.
Packit Service 46fadf
 *
Packit Service 46fadf
 *   - Redistributions in binary form must reproduce the above copyright
Packit Service 46fadf
 *     notice, this list of conditions and the following disclaimer in the
Packit Service 46fadf
 *     documentation and/or other materials provided with the distribution.
Packit Service 46fadf
 *
Packit Service 46fadf
 *   - Neither the name of Oracle nor the names of its
Packit Service 46fadf
 *     contributors may be used to endorse or promote products derived
Packit Service 46fadf
 *     from this software without specific prior written permission.
Packit Service 46fadf
 *
Packit Service 46fadf
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
Packit Service 46fadf
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
Packit Service 46fadf
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit Service 46fadf
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Packit Service 46fadf
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit Service 46fadf
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit Service 46fadf
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit Service 46fadf
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit Service 46fadf
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit Service 46fadf
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Packit Service 46fadf
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 46fadf
 */
Packit Service 46fadf
Packit Service 46fadf
// converting b/w JavaScript Date and Java LocalDateTime
Packit Service 46fadf
Packit Service 46fadf
// JavaScript Date with current time
Packit Service 46fadf
var d = new Date();
Packit Service 46fadf
print(d);
Packit Service 46fadf
 
Packit Service 46fadf
// Java 8 java.time classes used
Packit Service 46fadf
var Instant = java.time.Instant;
Packit Service 46fadf
var LocalDateTime = java.time.LocalDateTime;
Packit Service 46fadf
var ZoneId = java.time.ZoneId;
Packit Service 46fadf
 
Packit Service 46fadf
// Date.prototype.getTime
Packit Service 46fadf
Packit Service 46fadf
// getTime() method returns the numeric value corresponding to the time
Packit Service 46fadf
// for the specified date according to universal time. The value returned
Packit Service 46fadf
// by the getTime() method is the number of milliseconds since 1 January 1970 00:00:00 UTC.
Packit Service 46fadf
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
Packit Service 46fadf
 
Packit Service 46fadf
// Java Instant.ofEpochMilli to convert time in milliseconds to Instant object
Packit Service 46fadf
// https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#ofEpochMilli-long-
Packit Service 46fadf
 
Packit Service 46fadf
var instant = Instant.ofEpochMilli(d.getTime());
Packit Service 46fadf
 
Packit Service 46fadf
// Instant to LocalDateTime using LocalDateTime.ofInstant
Packit Service 46fadf
// https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#ofInstant-java.time.Instant-java.time.ZoneId-
Packit Service 46fadf
 
Packit Service 46fadf
var ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
Packit Service 46fadf
print(ldt);
Packit Service 46fadf
 
Packit Service 46fadf
// converting a LocalDateTime to JavaScript Date
Packit Service 46fadf
// convert LocalDateTime to Instant first
Packit Service 46fadf
// https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#atZone-java.time.ZoneId-
Packit Service 46fadf
 
Packit Service 46fadf
var instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
Packit Service 46fadf
 
Packit Service 46fadf
// instant to to epoch milliseconds
Packit Service 46fadf
// https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#toEpochMilli--
Packit Service 46fadf
// and then to JavaScript Date from time in milliseconds
Packit Service 46fadf
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date
Packit Service 46fadf
Packit Service 46fadf
var d1 = new Date(instant.toEpochMilli());
Packit Service 46fadf
print(d1);