Class: javajava::util::Date

Inherits:
Object show all
Includes:
JactiveSupport::Localize
Defined in:
lib/jactive_support/java_ext/date/calculations.rb,
lib/jactive_support/java_ext/date/conversions.rb,
lib/jactive_support/java_ext/date/formatters.rb,
lib/jactive_support/java_ext/date/localize.rb,
lib/jactive_support/core_ext/to_java_date.rb

Constant Summary collapse

CALCULATION_FIELDS =
{
  years: java::util::Calendar::YEAR,
  months: java::util::Calendar::MONTH,
  weeks: java::util::Calendar::WEEK_OF_YEAR,
  days: java::util::Calendar::DAY_OF_MONTH,
  hours: java::util::Calendar::HOUR_OF_DAY,
  minutes: java::util::Calendar::MINUTE,
  seconds: java::util::Calendar::SECOND,
  millis: java::util::Calendar::MILLISECOND
}
DATE_FORMATS =
{
  :db           => "yyyy-MM-dd HH:mm:ss.SSS",
  :i18n         => lambda { |clazz, locale| clazz.i18n_formatter(locale: locale) },
  :number       => "yyyyMMddHHmmssSSS",
  :time         => "HH:mm:ss",
  :full         => lambda { |clazz, locale| clazz.date_time_instance(:full, :full, locale) },
  :long         => lambda { |clazz, locale| clazz.date_time_instance(:long, :long, locale) },
  :medium       => lambda { |clazz, locale| clazz.date_time_instance(:medium, :medium, locale) },
  :short        => lambda { |clazz, locale| clazz.date_time_instance(:short, :short, locale) },
  :default      => lambda { |clazz, locale| clazz.date_time_instance(:default, :default, locale) },
  :rfc822       => "EEE, dd MMM yyyy HH:mm:ss Z",
  :httpdate     => lambda { |clazz, locale|
    fmt = clazz.pattern_formatter("EEE, dd MMM yyyy HH:mm:ss z", locale || 'EN')
    fmt.time_zone = 'GMT'.to_java_time_zone
    fmt
  }
}
FULL_STYLE =
java::text::DateFormat::FULL
LONG_STYLE =
java::text::DateFormat::LONG
MEDIUM_STYLE =
java::text::DateFormat::MEDIUM
SHORT_STYLE =
java::text::DateFormat::SHORT
DEFAULT_STYLE =
java::text::DateFormat::DEFAULT
STYLE =
{
  :full => FULL_STYLE,
  :long => LONG_STYLE,
  :medium => MEDIUM_STYLE,
  :short => SHORT_STYLE,
  :default => DEFAULT_STYLE
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JactiveSupport::Localize

#localize

Class Method Details

.date_instance(date_style = :default, locale = nil) ⇒ Object



30
31
32
# File 'lib/jactive_support/java_ext/date/formatters.rb', line 30

def self.date_instance(date_style = :default, locale = nil)
  java::text::DateFormat.getDateInstance(STYLE[date_style], locale.to_locale)
end

.date_time_instance(date_style = :default, time_style = :default, locale = nil) ⇒ Object



22
23
24
# File 'lib/jactive_support/java_ext/date/formatters.rb', line 22

def self.date_time_instance(date_style = :default, time_style = :default, locale = nil)
  java::text::DateFormat.getDateTimeInstance(STYLE[date_style], STYLE[time_style], locale.to_locale)
end

.default_formatter(locale) ⇒ Object



18
19
20
# File 'lib/jactive_support/java_ext/date/formatters.rb', line 18

def self.default_formatter(locale)
  date_time_instance(:default, :default, locale)
end

.format(format = :i18n, options = {}) ⇒ Object



42
43
44
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 42

def self.format(format = :i18n, options = {})
  formatter(format, options).to_pattern
end

.formatter(format = :i18n, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 23

def self.formatter(format = :i18n, options = {})
  formatter = self::DATE_FORMATS[format] || formatter(:default, options)
  if formatter.respond_to?(:call)
    if formatter.arity == 2
      formatter = formatter.call(self, options[:locale])
    else
      formatter = formatter.call(self)
    end
  elsif formatter.is_a?(String)
    formatter = self.pattern_formatter(formatter, options[:locale])
  end

  if options[:time_zone] && formatter.respond_to?(:time_zone)
    formatter.time_zone = options[:time_zone].to_java_time_zone
  end

  formatter
end

.i18n_scopeObject



9
10
11
# File 'lib/jactive_support/java_ext/date/localize.rb', line 9

def self.i18n_scope
  :java_date
end

.pattern_formatter(pattern, locale = nil) ⇒ Object



34
35
36
# File 'lib/jactive_support/java_ext/date/formatters.rb', line 34

def self.pattern_formatter(pattern, locale=nil)
  java::text::SimpleDateFormat.new(pattern, locale.to_locale)
end

.time_instance(time_style = :default, locale = nil) ⇒ Object



26
27
28
# File 'lib/jactive_support/java_ext/date/formatters.rb', line 26

def self.time_instance(time_style = :default, locale = nil)
  java::text::DateFormat.getTimeInstance(STYLE[time_style], locale.to_locale)
end

Instance Method Details

#acts_like_time?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 125

def acts_like_time?
  true
end

#advance(options) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/jactive_support/java_ext/date/calculations.rb', line 13

def advance(options)
  cal = java::util::Calendar.instance
  cal.time = self
  options.each do |field, value|
    cal.add(CALCULATION_FIELDS[field], value)
  end
  cal.time
end

#age(since = Date.new) ⇒ Object



22
23
24
# File 'lib/jactive_support/java_ext/date/calculations.rb', line 22

def age(since = Date.new)
  difference(:years, since)
end

#difference(field = :years, since = Date.new) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jactive_support/java_ext/date/calculations.rb', line 26

def difference(field = :years, since = Date.new)
  local_date = org::joda::time::LocalDate.fromDateFields(self)
  since_date = org::joda::time::LocalDate.fromDateFields(since)
  if field == :days
    return org::joda::time::Days.daysBetween(local_date, since_date).days
  elsif field == :months
    return org::joda::time::Months.monthsBetween(local_date, since_date).months
  else
    return org::joda::time::Years.yearsBetween(local_date, since_date).years
  end
end

#httpdateObject



121
122
123
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 121

def httpdate
  to_formatted_s(:httpdate)
end

#strformat(pattern, options = {}) ⇒ Object

Converts a java.util.Date object to a string using the given format pattern. The pattern syntax is from java.text.SimpleDateFormat

my_time = java.util.Date.new  # => Mon Nov 12 22:59:51 -0500 2007
my_time.strformat('yyyy MMMMM') # => "2007 November"


81
82
83
84
85
86
87
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 81

def strformat(pattern, options = {})
  formatter = self.class.pattern_formatter(pattern, options[:locale])
  if options[:time_zone] && formatter.respond_to?(:time_zone)
    formatter.time_zone = options[:time_zone].to_java_time_zone
  end
  formatter.format(self)
end

#to_dateObject

Converts a java.util.Date object to a ruby Date, dropping hour, minute, and second precision.

my_time = java.util.Date.new  # => Mon Nov 12 22:59:51 -0500 2007
my_time.to_date     # => Mon, 12 Nov 2007


93
94
95
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 93

def to_date
  to_time.to_date
end

#to_datetimeObject

Converts a java.util.Date instance to a Ruby DateTime instance, preserving UTC offset.

my_time = Time.now    # => Mon Nov 12 23:04:21 -0500 2007
my_time.to_datetime   # => Mon, 12 Nov 2007 23:04:21 -0500

your_time = Time.parse("1/13/2009 1:13:03 P.M.")  # => Tue Jan 13 13:13:03 -0500 2009
your_time.to_datetime                             # => Tue, 13 Jan 2009 13:13:03 -0500


113
114
115
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 113

def to_datetime
  to_time.to_datetime
end

#to_formatted_s(format = :i18n, options = {}) ⇒ Object Also known as: to_s

Converts to a formatted string. See DATE_FORMATS for builtin formats.

This method is aliased to to_s.

time = java::util::Date.new         # => Thu Jan 18 06:10:17 CST 2007

time.to_formatted_s(:time)          # => "06:10:17"
time.to_s(:time)                    # => "06:10:17"

time.to_formatted_s(:db)            # => "2007-01-18 06:10:17.100"
time.to_formatted_s(:number)        # => "20070118061017100"
time.to_formatted_s(:short)         # => "18 Jan 06:10"
time.to_formatted_s(:long)          # => "January 18, 2007 06:10"
time.to_formatted_s(:rfc822)        # => "Thu, 18 Jan 2007 06:10:17 -0600"

Adding your own time formats to to_formatted_s

You can add your own formats to the Time::DATE_FORMATS hash. Use the format name as the hash key and either a strftime string or Proc instance that takes a time argument as the value.

# config/initializers/time_formats.rb
Time::DATE_FORMATS[:month_and_year] = "%B %Y"
Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }


69
70
71
72
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 69

def to_formatted_s(format = :i18n, options = {})
  return to_default_s unless self.class::DATE_FORMATS[format]
  self.class.formatter(format, options).format(self)
end

#to_java_dateObject



17
18
19
# File 'lib/jactive_support/core_ext/to_java_date.rb', line 17

def to_java_date
  self
end

#to_java_sqldateObject



117
118
119
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 117

def to_java_sqldate
  java::sql::Date.new(time)
end

#to_timeObject

Converts a java.util.Date instance to a Time.

Examples

date = java.util.Date.new  # => Mon Nov 12 22:59:51 -0500 2007
date.to_time               # => Mon Nov 12 22:59:51 -0500 2007


102
103
104
# File 'lib/jactive_support/java_ext/date/conversions.rb', line 102

def to_time
  ::Time.at(self.getTime/1000)
end