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

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

问题描述

在 Ruby on Rails 中,有一项功能可以让您获取任何日期并打印出它是多久以前"的.

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

例如:

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

在 Java 中有没有一种简单的方法可以做到这一点?

Is there an easy way to do this in Java?

推荐答案

看看 PrettyTime 图书馆.

使用起来非常简单:

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"

如评论中所述,Android 将此功能内置于 android.text.format.DateUtils 类.

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

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

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