Converting time / DataTime / data to java.util.Calendar?

Is it possible to easily convert JRuby Time / DataTime / Data to java.util.Calendar including timezone?

In #jruby I was presented with code like this cal.set_time_in_millis(time.to_i)

, but I lost my timezone information in between. So the more specific question is how to convert the timezone, but I prefer to ask a broader search in case there is an easier way.

+1


a source to share


2 answers


You can use #to_java method to convert Ruby time object to java.util.Date:

require 'java'
Time.now.to_java

      



Note that this alignment occurs automatically when you pass Ruby objects to Java methods.

+2


a source


I am aware that Time does not store the timezone, so the one returned Time.now.zone

is the local timezone.

So just convert to java.util.Data

:



data = java.util.Date.new(date.to_i*1000)

      

+1


a source







All Articles