有很多版本的,先上过ruby版本。
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
case distance_in_minutes
when 0..1 then (distance_in_minutes==0) ? '几秒钟前'[] : '1 分钟前'[]
when 2..59 then "{minutes} 分钟前"[:minutes_ago, distance_in_minutes]
when 60..90 then "1 小时前"[]
when 90..1440 then "{hours} 小时前"[:hours_ago, (distance_in_minutes.to_f / 60.0).round]
when 1440..2160 then '1 天前'[] # 1 day to 1.5 days
when 2160..2880 then "{days} 天前"[:days_ago, (distance_in_minutes.to_f / 1440.0).round] # 1.5 days to 2 days
else from_time.strftime("%Y-%m-%d"[:datetime_format]) { |x| x.downcase }
end
end
下面是java版本
public static String distanceOfTimeInWords(long fromTime, long toTime, String format) {
return distanceOfTimeInWords(new Date(fromTime), new Date(toTime), format, 7);
}
public static String distanceOfTimeInWords(long fromTime, long toTime, String format, int days) {
return distanceOfTimeInWords(new Date(fromTime), new Date(toTime), format, days);
}
public static String distanceOfTimeInWords(long fromTime, long toTime, int days) {
return distanceOfTimeInWords(new Date(fromTime), new Date(toTime), "MM-dd HH:mm", days);
}
public static String distanceOfTimeInWords(long fromTime, long toTime) {
return distanceOfTimeInWords(new Date(fromTime), new Date(toTime), "MM-dd HH:mm", 7);
}
public static String distanceOfTimeInWords(Date fromTime, Date toTime, int days) {
return distanceOfTimeInWords(fromTime, toTime, "MM-dd HH:mm", days);
}
public static String distanceOfTimeInWords(Date fromTime, Date toTime, String format) {
return distanceOfTimeInWords(fromTime, toTime, format, 7);
}
public static String distanceOfTimeInWords(Date fromTime, Date toTime) {
return distanceOfTimeInWords(fromTime, toTime, "MM-dd HH:mm", 7);
}
/**
* 截止时间时间到起始时间间隔的时间描述
* @param fromTime 起始时间
* @param toTime 截止时间
* @param format 格式化
* @param days 超过此天数,将按format格式化显示实际时间
* @return
*/
public static String distanceOfTimeInWords(Date fromTime, Date toTime, String format, int days) {
long distanceInMinutes = (toTime.getTime() - fromTime.getTime()) / 60000;
String message = "";
if (distanceInMinutes == 0) {
message = "几秒钟前";
} else if (distanceInMinutes >= 1 && distanceInMinutes < 60) {
message = distanceInMinutes + "分钟前";
} else if (distanceInMinutes >= 60 && distanceInMinutes < 1400) {
message = (distanceInMinutes / 60) + "小时前";
} else if (distanceInMinutes >= 1440 && distanceInMinutes <= (1440 * days)) {
message = (distanceInMinutes / 1440) + "天前";
} else {
message = new SimpleDateFormat(format).format(fromTime);
}
return message;
}
分享到:
相关推荐
java读取某个文件夹下的所有文件 很不错可以看看 不要分
20个开发人员非常有用的Java功能代码
前段时间工作中需要用到中文分词的软件,于是上网找了找。 发现中科院提供的ictclas工具是比较不错的。 可惜没有官方正式的java版本,有一个网友sinbo自己改写了一个java版本。 但在使用过程中发现问题很多。其中...
netcdf即nc文件的读写实例,java版本的,不懂的地方可以问我,lishirongtech@126.com
用Java实现邮箱激活账号的功能,可以参考我的博客 博客地址:http://blog.csdn.net/jolingogo/article/details/8591197
java中,计算一个月总共有几天工作日,和都有哪天是工作日等问题。
java 使用Date类获取系统的当前时间 java 使用Date类获取系统的当前时间 java 使用Date类获取系统的当前时间
java jdk 1.6 Linux版本
java 将excel里面的数字时间转化为准确的时间格式;具体来源有点忘记了,实现是摘自了开源代码里面的一段。
java版的生成唯一编号的一个小程序
radius客户端源代码,用java实现,可监控radius服务器
Java解析apk/ipa读取包名、版本名、版本号等等信息源码,自己总结,希望对大家有所帮助。
搜索功能设计java实现能够完成功能搜索,支持用户业务上的搜索操作搜索功能设计java实现能够完成功能搜索,支持用户业务上的搜索操作
java文件夹排序,按时间、大小等排序。。。。。。
简单java代码获取apk文件版本号,用于比较判断apk版本更新!
时间序列预测法是一种定量分析方法,它是在时间序列变量分析的基础上,运用一定的数学方法建立预测模型,使时间趋势向外延伸,从而预测未来市场的发展变化趋势,确定变量预测值。
实际上,SUN公司也一直致力于Java打印功能的完善,而Java2平台则终于有了一个健壮的打印模式的开端,该打印模式与Java2D图形包充分结合成一体。更令人鼓舞的是,新发布的jdk1.4则提供了一套完整的"Java 打印服务 API...
Java实现Web报表打印功能
网盘源码(JAVA)-实现部分功能,其他的功能需要自己做了,现在没时间
Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...