开发者

How to format java.util.Date to a date in "MM/dd/yyyy" format in ruby?

I want to convert a Java object to be formatted in ruby.

I used the following code

def format_date(date,date_format)
    return Date::strptime(date, date_format)
end 

date is an instance of java.util.Date date_format = "%d/%m/%Y"

I am getting the following error in ruby

private method `sub!' called for #&开发者_StackOverflow中文版lt;Java::JavaUtil::Date:0x150ea09>


convert to JRuby Time and use strftime like this:

def format_date(date, date_format)
    t = Time.at(date.time/1000)
    return t.strftime(date_format)
end 

Java Date time Method gives the number of milliseconds since January 1, 1970, Ruby Time number of seconds since that time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜