0

I've just upgraded to java 8. Previously I was using the nashorn script engine and the following would work:

engine.eval("format('%04d-%02d-%02d', 1966, 07, 18);"); 

Now I get:

<eval>:1 ReferenceError: "format" is not defined

I've googled around for over a day now, but I just can't find an example of this. Is this still supported?

Very grateful for any help.

BackSlash
  • 21,927
  • 22
  • 96
  • 136

1 Answers1

0

It doesn't make sense. Open chrome, for example, and see that format is not a globally available function and you'd get the exact same exception.

Take a look at this one:

Where can I find documentation on formatting a date in JavaScript?

You'd probably have to define the prototype of the format function if you want to use it like that.

And here is some sample code to get you started:

engine.eval(""
                + "var options = {year: \"numeric\", month: \"numeric\", day: \"numeric\"};"
                + "var res = new Date(1966, 07, 18).toLocaleDateString('en', options);"
                + "print(res);"); 

Also, some rather intriguing .md file where you can see the global stuff exposed in nashorn:

https://gist.github.com/WebReflection/9627010

Community
  • 1
  • 1
Nikola Yovchev
  • 9,498
  • 4
  • 46
  • 72