距离当前时间的时间间隔

比如有一时间string = “2009年12月21日 17时03分45秒”
想得到此时间距离当前有几天?最好精确到秒

我想的方案:把当前时间和string都转换成2000年1月1号0时0分0秒多少秒,然后,执行相减,然后换算成天

大家有没有更好的方法??

x =~ /.*(\d\d)年(\d\d)月(\d\d)日.*(\d\d)时(\d\d)分/

expireDate = "#{$1}#{$2}#{$3}"

expireTime = "#{$4}#{$5}"

t = Time.now

dateNow = t.strftime("%y%m%d") # 090825

timeNow = t.strftime("%H%M")

[code="java"]
String strDate = "2009年12月21日 17时03分45秒";
Date date1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").parse(strDate);
Date date2 = new Date();
long millSeconds = (date1.getTime() - date2.getTime()); //此处得到毫秒
System.out.println(millSeconds);
long seconds = millSeconds / 1000; //此处为秒数
System.out.println(seconds);
long days = seconds / (3600 * 24); //此处为天数
System.out.println(days);
[/code]

用 SimpleDateFormat 类可以方便的将字符串转化为 Date:
[code="java"]
Date then = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").parse(str);
[/code]

有一个ActionView helper:

<%= time_ago_in_words(time)%> ago

这样的功能一般就够了。