Class: javajava::util::Date
- 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
- .date_instance(date_style = :default, locale = nil) ⇒ Object
- .date_time_instance(date_style = :default, time_style = :default, locale = nil) ⇒ Object
- .default_formatter(locale) ⇒ Object
- .format(format = :i18n, options = {}) ⇒ Object
- .formatter(format = :i18n, options = {}) ⇒ Object
- .i18n_scope ⇒ Object
- .pattern_formatter(pattern, locale = nil) ⇒ Object
- .time_instance(time_style = :default, locale = nil) ⇒ Object
Instance Method Summary collapse
- #acts_like_time? ⇒ Boolean
- #advance(options) ⇒ Object
- #age(since = Date.new) ⇒ Object
- #difference(field = :years, since = Date.new) ⇒ Object
- #httpdate ⇒ Object
-
#strformat(pattern, options = {}) ⇒ Object
Converts a java.util.Date object to a string using the given format pattern.
-
#to_date ⇒ Object
Converts a java.util.Date object to a ruby Date, dropping hour, minute, and second precision.
-
#to_datetime ⇒ Object
Converts a java.util.Date instance to a Ruby DateTime instance, preserving UTC offset.
-
#to_formatted_s(format = :i18n, options = {}) ⇒ Object
(also: #to_s)
Converts to a formatted string.
- #to_java_date ⇒ Object
- #to_java_sqldate ⇒ Object
-
#to_time ⇒ Object
Converts a java.util.Date instance to a Time.
Methods included from JactiveSupport::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, = {}) formatter(format, ).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, = {}) formatter = self::DATE_FORMATS[format] || formatter(:default, ) if formatter.respond_to?(:call) if formatter.arity == 2 formatter = formatter.call(self, [:locale]) else formatter = formatter.call(self) end elsif formatter.is_a?(String) formatter = self.pattern_formatter(formatter, [:locale]) end if [:time_zone] && formatter.respond_to?(:time_zone) formatter.time_zone = [:time_zone].to_java_time_zone end formatter end |
.i18n_scope ⇒ Object
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 |
Instance Method Details
#acts_like_time? ⇒ 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() cal = java::util::Calendar.instance cal.time = self .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 |
#httpdate ⇒ Object
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, = {}) formatter = self.class.pattern_formatter(pattern, [:locale]) if [:time_zone] && formatter.respond_to?(:time_zone) formatter.time_zone = [:time_zone].to_java_time_zone end formatter.format(self) end |
#to_date ⇒ Object
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_datetime ⇒ Object
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, = {}) return to_default_s unless self.class::DATE_FORMATS[format] self.class.formatter(format, ).format(self) end |
#to_java_date ⇒ Object
17 18 19 |
# File 'lib/jactive_support/core_ext/to_java_date.rb', line 17 def to_java_date self end |
#to_java_sqldate ⇒ Object
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_time ⇒ Object
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 |