Java 8 Nashorn CLI use case: adding ISO 8601 instant to CSV with UNIX timestamp

By William Narmontas,

This page can be edited on GitHub.

Original article at Medium.

Nashorn is a JavaScript engine that comes with a Java 8 installation. It allows you to write scripts that have access to all of Java very easily, without needing any compilation steps. It also comes with its own Shell Scripting facilities and JSON handling (as you’d expect from a JavaScript engine).

There’s a command line utility called jjs which will execute a given script or provide you with a REPL where you can type in custom commands.

Here for demonstration purposes we’ll utilise the powerful java.time library:



We can get the time in a different time zone, such as in New York:


And if we wanted to go back a few years we can:


Notice the same local time and timezone but different offset due to New York’s DST.

Making a time-series CSV human-readable

Let’s suppose we had this CSV file come in from a vendor or a client:


Are you able to discern what date and time the timestamp actually refers to? Not unless you check by hand. But would you be able to visually scan through such a file if it had 40 million lines? That’s only doable with human readable timestamps.

With jjs On UNIX, you can make a script to effortlessly enrich some CSV input:


When you run it against sample data, you’ll see a new column in the front:


And if this were a 40 million line file, you can use less to scan through all the data very easily.

What about JSON?

You may also come across NDJSON, such as:


Now let’s say you are in New York and want to know attach the local time. You could do:



Many other use cases

Especially where you want to do some Java calls but don’t wish to set up an IDE and a Maven/SBT project…

And where you’d like to turn your scripts into proper libraries later on.

One nice use case of mine was to reconfigure a Scala/Play app at runtime:


I hope you found this useful and will investigate what else Nashorn has to offer. It’s a very solid tool that’s available where Java 8 is and can be utilised very easily. Explore :-)