Method: Java::JavaUtil::Date.from_ruby_date

Defined in:
lib/jinx/import/java.rb

.from_ruby_date(date) ⇒ Date

Converts a Ruby Date or DateTime to a Java Date.

Parameters:

  • date (::Date, DateTime)

    the Ruby date

Returns:

  • (Date)

    the Java date



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/jinx/import/java.rb', line 159

def self.from_ruby_date(date)
  return if date.nil?
  # DateTime has time attributes, Date doesn't
  if DateTime === date then
    hour, min, sec = date.hour, date.min, date.sec
  else
    hour = min = sec = 0
  end
  # the Ruby time
  rtime = Time.local(sec, min, hour, date.day, date.mon, date.year, nil, nil, nil, nil)
  # millis since epoch
  millis = (rtime.to_f * 1000).truncate
  # the Java date factory
  calendar = java.util.Calendar.instance
  calendar.setTimeInMillis(millis)
  jtime = calendar.getTime
  # the daylight time flag
  isdt = calendar.timeZone.inDaylightTime(jtime)
  return jtime unless isdt
  # adjust the Ruby time for DST
  rtime = Time.local(sec, min, hour, date.day, date.mon, date.year, nil, nil, isdt, nil)
  millis = (rtime.to_f * 1000).truncate
  calendar.setTimeInMillis(millis)
  calendar.getTime
end