如何将ruby中的java.util.Date格式化为“ MM / dd / yyyy”格式的日期?

问题描述

| 我想将Java对象转换为以ruby格式化的对象。 我用下面的代码
def format_date(date,date_format)
    return Date::strptime(date,date_format)
end 
date是java.util.Date的实例 date_format = \“%d /%m /%Y \” 我在ruby中遇到以下错误
private method `sub!\' called for #<Java::JavaUtil::Date:0x150ea09>
    

解决方法

转换为JRuby Time并使用strftime像这样:
def format_date(date,date_format)
    t = Time.at(date.time/1000)
    return t.strftime(date_format)
end 
Java Date time Method给出自1970年1月1日以来的毫秒数,而Ruby Time自该时间以来的秒数。