如何计算“时间之前”在Java中? [英] How to calculate "time ago" in Java?

查看:125
本文介绍了如何计算“时间之前”在Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如:在Ruby on Rails中,有一个功能允许您采取任何日期并打印出很久以前 p>

  8分钟前
8小时前
8天前
8个月前
8年前

在Java中有一个简单的方法吗?

解决方案

查看 PrettyTime < a>图书馆。



使用起来很简单:

  import org.ocpsoft.prettytime.PrettyTime; 

PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));
//打印之前

您还可以传入国际化邮件的区域设置:

  PrettyTime p = new PrettyTime(new Locale(fr)); 
System.out.println(p.format(new Date()));
//打印àl'instant

如评论中所述,Android有此功能内置于 android.text.format.DateUtils 课程。


In Ruby on Rails, there is a feature that allows you to take any Date and print out how "long ago" it was.

For example:

8 minutes ago
8 hours ago
8 days ago
8 months ago
8 years ago

Is there an easy way to do this in Java?

解决方案

Take a look at the PrettyTime library.

It's quite simple to use:

import org.ocpsoft.prettytime.PrettyTime;

PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));
// prints "moments ago"

You can also pass in a locale for internationalized messages:

PrettyTime p = new PrettyTime(new Locale("fr"));
System.out.println(p.format(new Date()));
// prints "à l'instant"

As noted in the comments, Android has this functionality built into the android.text.format.DateUtils class.

这篇关于如何计算“时间之前”在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆