1.整体分析

1.1.源代码如下,可以直接Copy。

public class TimeUtil {
private static final String TAG = "TimeUtil"; public static String computePastTime(String time) {
// Log.v(TAG, "computePastTime: " + time);
String result = "刚刚";
//2017-02-13T01:20:13.035+08:00
time = time.replace("T", " ");
time = time.substring(0, 22);
// Log.v(TAG, "computePastTime time: " + time);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.SIMPLIFIED_CHINESE);
try {
Date t = simpleDateFormat.parse(time);
Date now = new Date(System.currentTimeMillis());
long diff = (now.getTime() - t.getTime()) / 1000;
if (diff < 60) {
result = "刚刚";
} else if ((diff /= 60) < 60) {
result = diff + "分钟前";
} else if ((diff /= 60) < 24) {
result = diff + "小时前";
} else if ((diff /= 24) < 30) {
result = diff + "天前";
} else if ((diff /= 30) < 12) {
result = diff + "月前";
} else {
diff /= 12;
result = diff + "年前";
}
} catch (ParseException e) {
e.printStackTrace();
}
// Log.v(TAG, "computePastTime result: " + result);
return result;
} public static String formatTime(String time) {
// Log.v(TAG, "formatTime: " + time);
//2017-02-13T01:20:13.035+08:00
time = time.replace("T", " ");
time = time.substring(0, 16);
// Log.v(TAG, "formatTime result: " + time);
return time;
}
}

1.2.主要方法

  • computePastTime(String time)==>字符串转化为汉字的时间。
  • formatTime(String time)==>格式化字符串时间

1.3.参考其他时间类。

public class MyTimeUtils {  

    //获取时间戳
public static long getTime() {
Calendar calendar = Calendar.getInstance();// 获取当前日历对象
long unixTime = calendar.getTimeInMillis();// 获取当前时区下日期时间对应的时间戳
return unixTime;
} public static String getTimeString() {
return Long.toString(new Date().getTime());
} //获取标准时间
public static String getStandardTime() {
SimpleDateFormat formatter = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_one));
Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
return formatter.format(curDate);
} // 获取与现在时间的时间差(秒)
public static int getDurationSecond(String time) {
int durationSecond = 0;
SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
Date date;
try {
date = df.parse(time);
MyLog.i("TimeUtils getDurationSecond Date=" + new Date().toString());
durationSecond = (int) ((new Date().getTime() - date.getTime()) / 1000);
} catch (Exception e) {
MyLog.e("TimeUtils getDurationSecond error=" + e);
}
return durationSecond;
} // 获取时间差
public static String getDuration(String one, String two) {
String duration = "";
SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span><span style="font-family: SimHei;">);</span>
Date date1;
Date date2;
try {
date1 = df.parse(one);
date2 = df.parse(two);
int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);
if (l > 60) {
int hr = l / 60;
int min = l % 60;
duration = <span style="font-family: SimHei;">hr + "小时" + min + "分钟"</span>;
} else {
duration = <span style="font-family: SimHei;">l + "分钟";</span>
}
} catch (Exception e) {
e.printStackTrace();
} return duration;
} // 获取与当前时间差
public static String getcurDuration(String one) {
String duration = "";
SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
Date date1;
Date date2;
try {
date1 = df.parse(one);
date2 = new Date();
int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);
if (l > 60) {
int hr = l / 60;
int min = l % 60;
duration = <span style="font-family: SimHei;">hr + "小时" + min + "分钟"</span><span style="font-family: SimHei;">;</span>
} else {
duration =<span style="font-family: SimHei;"> l + "分钟";</span> }
} catch (Exception e) {
e.printStackTrace();
} return duration;
} /**
* @return格式化当前日期和时间为字符串
*/
public static String mCurrentTime() {
SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);
String currenttime = df.format(new Date());
return currenttime;
} public static String parseBangTime(long time) {
MyLog.out("time==>" + time);
String timeTemp = "";
if (time < 60) {
timeTemp = time + BaseApplication.getInstance().getString(R.string.seconds_before);
} else if (time < (60 * 60)) {
timeTemp = time / 60 + BaseApplication.getInstance().getString(R.string.minutes_before);
} else if (time < (3600 * 24)) {
timeTemp = time / 3600 + BaseApplication.getInstance().getString(R.string.hour_before);
} else if (time < (60 * 60 * 24 * 30)) {
timeTemp = time / (3600 * 24) + BaseApplication.getInstance().getString(R.string.today_before);
} else {
timeTemp = time / (3600 * 24 * 30) + BaseApplication.getInstance().getString(R.string.month_before);
}
return timeTemp;
} public static String getTimeStamp() {
SimpleDateFormat dateFormat = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_two));
String timeStamp = dateFormat.format(new Date());
MyLog.e("getTimeStamp=" + timeStamp);
return timeStamp;
} public static String getCurrentDate(){
SimpleDateFormat df = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show));
String currentDate = df.format(new Date());
return currentDate;
}
}

2.局部分析

2.1.字符串转化为汉字的时间

  

  服务器返回的时间一般是一个字符串,如:2017-02-13T01:20:13.035+08:00

  然后我要解析这段字符串,首先将T变成空格,然后截取前22个字符即可

  然后利用SimpleDataFormat转化一下想要的格式

  然后将字符串转化为Date

  然后比较Dta和当前的时间差

  从小到大排序:刚刚、分钟前、小时前、天前、月前、年前。

  

2.1.格式化字符串time

  

  这个函数的作用也是解析一段字符串:2017-02-13T01:20:13.035+08:00

  然后替换T为空格

  然后截取前16个字符即可。

3.案例

3.1.写一个测试函数

  

  当前时间为:2017-11-28 16:15

3.2.执行结果

  

  没问题,Over!

Android 时间计算工具 通用类TimeUtil的更多相关文章

  1. Android随笔之——Android时间、日期相关类和方法

    今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...

  2. Android 时间与日期操作类

    获取本地日期与时间 public String getCalendar() { @SuppressLint("SimpleDateFormat") SimpleDateFormat ...

  3. DataTable转任意类型对象List数组-----工具通用类(利用反射和泛型)

    public class ConvertHelper<T> where T : new() { /// <summary> /// 利用反射和泛型 /// </summa ...

  4. jdk8 时间日期工具类(转)

    package com.changfu.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import jav ...

  5. Java 高精度浮点数计算工具

    说起编程中的高精度数值,我第一反应就是double类型了.的确,double阶码11位,尾数52位,几乎能应对任何苛刻的要求......然而,当我天真地尝试用double来算泰勒展开式的函数值,离散代 ...

  6. 代码片段:基于 JDK 8 time包的时间工具类 TimeUtil

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “知识的工作者必须成为自己时间的首席执行官.” 前言 这次泥瓦匠带来的是一个好玩的基于 JDK ...

  7. 时间处理工具类TimeUtil

    转自:https://cnblogs.com/ityouknow/p/5662753.html 功能 Date与String之间的互相转换,以及一些特殊格式的时间字符串处理 代码 /** * 类名:T ...

  8. Java日期时间实用工具类

    Java日期时间实用工具类 1.Date (java.util.Date)    Date();        以当前时间构造一个Date对象    Date(long);        构造函数   ...

  9. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

随机推荐

  1. 如何解决ArcGIS Runtime SDK for Android中文标注无法显示的问题

    自10.2版本开始,我就一直被ArcGIS Runtime SDK for Android的中文标注无限困扰.无论是驻留于内存中的Graphic 的文本符号TextSymbol,还是新增的离线geod ...

  2. 浅谈SQL Server中的事务日志(一)----事务日志的物理和逻辑构架

    简介 SQL Server中的事务日志无疑是SQL Server中最重要的部分之一.因为SQL SERVER利用事务日志来确保持久性(Durability)和事务回滚(Rollback).从而还部分确 ...

  3. SPFieldLookupValue

    //得到查阅项的值SPWeb web = site.OpenWeb();SPList list = web.Lists["DemoList"];SPListItem item = ...

  4. 拼接sql语句时拼接空字符串报sql错误

    先上代码(php): $id_card=""; $sql = "select * from people where id_card=".$id_card; 看 ...

  5. 笨办法学Python(二)

    习题 2: 注释和井号 程序里的注释是很重要的.它们可以用自然语言告诉你某段代码的功能是什么.在你想要临时移除一段代码时,你还可以用注解的方式将这段代码临时禁用.接下来的练习将让你学会注释: #-- ...

  6. word文档快速转换为PPT演示文稿

    方法一: 访问http://t.im/pdftoppt,点击继续浏览(会跳转至:https://smallpdf.com/cn/pdf-to-ppt): 打开word文档,设置为“横向”,输出为PDF ...

  7. Oracle编程入门经典 第11章 过程、函数和程序包

    目录 11.1          优势和利益... 1 11.2          过程... 1 11.2.1       语法... 2 11.2.2       建立或者替换... 2 11.2 ...

  8. 既然红黑树那么好,为啥hashmap不直接采用红黑树,而是当大于8个的时候才转换红黑树?

    因为红黑树需要进行左旋,右旋操作, 而单链表不需要,以下都是单链表与红黑树结构对比.如果元素小于8个,查询成本高,新增成本低如果元素大于8个,查询成本低,新增成本高 https://bbs.csdn. ...

  9. 文本编辑器Vim技巧

    1.  导入文件内容  :r  文件名 2.  插入当前日期  :r  !date 3. :!which ls 4. :r !命令

  10. oracle: listener.ora 、sqlnet.ora 、tnsnames.ora的配置及例子

    1.解决问题:TNS或者数据库不能登录.      最简单有效方法:使用oracle系统提供的工具 netca 配置(把原来的删除掉重新配置)     $netca  2.然而,仍有疑问:如何指定'l ...