java数字格式化, 时间换算工具方法
2015-07-30 11:28
567 查看
java数字格式化, 时间换算工具方法
Java格式化数字——右对齐,左补0
方法一DecimialFormat format = new DecimialFormat("00"); int number = 9; System.out.println(format.format(number)); // 应该输出 09
方法二
String str2 = String.format("%04d", number2);
时间差换算成字符串方法
最开始没有考虑到integer的大小范围,出现了没法判断到周的情况,这个方法解决了这个问题/** * 计算两个日期型的时间相差多少时间 * * @return */ public static String getTimeDiff(Date postDate) { Date currentDate = new Date(); if (postDate == null || currentDate == null) { return null; } long timeLong = currentDate.getTime() - postDate.getTime(); long secondScope = 60*1000; long minuteScope = secondScope*60; long hourScope = minuteScope*24; long dayScope = hourScope*7; long weekScope = dayScope*4; long monthScope = weekScope*12; if (timeLong < secondScope) // 小于60秒 return timeLong / 1000 + "秒前"; else if (timeLong < minuteScope) { // 小于60分钟 timeLong = timeLong / secondScope; return timeLong + "分钟前"; } else if (timeLong < hourScope) { // 小于24小时 timeLong = timeLong / minuteScope; return timeLong + "小时前"; } else if (timeLong < dayScope) { // 小于7天 timeLong = timeLong / hourScope; return timeLong + "天前"; } else if (timeLong < weekScope) { // 小于4周 timeLong = timeLong / dayScope; return timeLong + "周前"; } else if(timeLong < monthScope){ timeLong = timeLong / weekScope; return timeLong + "月前"; } else { SimpleDateFormat sdf = new SimpleDateFormat(Constant.DATE_PATTERN2); return sdf.format(postDate); } }
相关文章推荐
- 认识java中的堆和栈
- java词法分析器简单实现
- java中的内存空间 堆和栈
- java--导入包中的类
- Spring JDBC 常用批量操作及插入操作
- Spring中什么时候用@Resource,什么时候用@service
- spring中bean的作用域
- hdu5316 线段树 by java
- java 读取Properties 几种方法
- 在eclipse中打开文件所在的目录
- Spring MVC-ContentNegotiatingViewResolver 多视图返回
- Spring mvc 返回json格式
- eclipse快捷键大全
- Ubuntu 配置Eclipse 导入工程完整设置 嵌入式工具链的安装
- 使用eclipse遇到问题:the-package-collides-with-a-type
- Java中对List进行排序
- Java ConcurrentModificationException 异常分析与解决方案
- javaweb学习总结(三十一)——国际化(i18n)
- Mybatis包分页查询java公共类
- 详解spring 每个jar的作用