Ian
Ian

Reputation: 3908

Gremlin - how to convert a string to a date?

For a given property, I'm trying to convert a String representation of a date to the actual type Date. The string representation follows the date format: yyyy-MM-dd HH:mm:ss.

For example:

g.V(1).outE().values('date') returns the following three Strings (one for each edge):

==>2019-05-02 00:00:00
==>2019-05-22 00:00:00
==>2019-05-06 00:00:00

I've tried adapting the solutions from here and here, but with no luck. E.g. the following code throws an error.

g.V(1).outE().values('date').map{new Date(it.get()).format('yyyy-MM-dd HH:mm:ss')}

Upvotes: 0

Views: 1265

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

Gremlin does not have explicit type conversion functions, so any form of casting you do will require some form of code specific to the programming language environment you're working in. It appears that you're working Java so use SimpleDateFormat:

gremlin> g.addV('person').property('dtob','2018-10-01 12:01:02')
==>v[0]
gremlin> format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
==>java.text.SimpleDateFormat@4f76f1a0
gremlin> g.V().values('dtob').map{format.parse(it.get())}
==>Mon Oct 01 12:01:02 EDT 2018

Note that you're using a lambda here to do this conversion which reduces the portability of your Gremlin a bit (not all graphs will support that).

Upvotes: 2

Related Questions