Java Reference
In-Depth Information
// Get a ZonedDateTime for the system default zone id
ZonedDateTime zdt =
ZonedDateTime.ofInstant(instant, ZoneId.
systemDefault());
System.out.println("Java ZonedDateTime: " + zdt);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
JavaScript Date: Fri Oct 31 2014 18:17:02 GMT-0500 (CDT)
Java ZonedDateTime: 2014-10-31T18:17:02.489-05:00[America/Chicago]
Summary
The Java APIs for Nashorn provide Java classes and interfaces that let you deal with
the Nashorn scripting engine and Nashorn objects directly in Java code. All classes for
Java APIs for Nashorn are in the
jdk.nashorn.api.scripting
package. Notice that the
Nashorn scripting engine internally uses a lot of other classes in other packages. However,
you are not supposed to use them in your application directly, except for classes from the
jdk.nashorn.api.scripting
package.
A Nashorn script can use different types of values and objects such as Number,
String, Boolean types of primitive values, and Object, String, Date, and custom objects.
You can also create Java objects in scripts. The script values of type Number and
Boolean are represented in Java as
java.lang.Number
and
java.lang.Boolean
. The
Null type in a script is represented as
null
in Java. The String primitive type in a script is
represented as
java.lang.String
in Java. A script object is represented as an object of
the
ScriptObjectMirror
class in Java.
The
NashornScriptEngineFactory
and
ScriptEngine
classes in the
jdk.nashorn.
api.scripting
package represent the Nashorn script engine factory and script engine,
respectively. You will need to work with these classes directly if you want to pass
options to the Nashorn engine. You can also pass options to the Nashorn engine using
-Dnashorn.args="<options>"
on the command-line. If you pass the
--globals-per-
engine
, to a Nashorn engine, all script contexts will share the globals. By default, script
contexts do not share globals.
The
ScriptObjectMirror
class contains several methods to add, update, and delete
properties from the script object. The class implements the
JSObject
and
Bindings
interfaces. The
JSObject
interface lets you treat a script object as a simple Java class,
whereas the
Bindings
interface lets you treat a script object as a map. You can use
getMember()
,
getSlot()
, and
get()
methods to get the value of the property of a script
object. You can use
setMember()
,
setSlot()
, and
put()
methods to add or update the
value of a property. You can use the
callMember()
method to call a method of the script
object. The
call()
method lets you call a script function, allowing you to pass the
this
value for the function invocation. The
ScriptObjectMirror
class contains several other
methods that let you work with script objects in Java programs.
Search WWH ::
Custom Search