调用这个方法 传入一个时间 让该时间与当前系统时间作对比

/*** 设置时间** @return*/
private String getTime(long mTime) {oldTime = System.currentTimeMillis();//DateUtils  时间换算工具类String format = DateUtils.format(mTime, "MM月dd日 HH:mm");String format1 = DateUtils.format(mTime, "mm 分钟前");String format2 = "刚刚";if (oldTime - mTime >= 30 * 60 * 1000) {// 如果超过30分钟,显示时间mTime = oldTime;return format;}if (oldTime - mTime >= 5 * 60 * 1000) {// 如果超过5分钟,显示几分钟前mTime = oldTime;return format1;}if (oldTime - mTime >= 3 * 60 * 1000) {// 如果超过3分钟,显示刚刚mTime = oldTime;return format2;} else {return "";}
}

** 下面附上 DateUtils 时间换算工具类**

@SuppressLint("SimpleDateFormat")
public final class DateUtils {public static String format(Date date, String format) {if (date == null || TextUtils.isEmpty(format)) {return null;}SimpleDateFormat sdf = new SimpleDateFormat(format);return sdf.format(date);
}public static String format(long timeMillion, String format){if(timeMillion < 31507200000L){timeMillion *= 1000;}return format(new Date(timeMillion), format);
}/*** * @param date*            long 20140507* @param format* @return*/
public static String format(long date, long time, String format) {try {Date d = getDateFromNumber(date,time);return format(d, format);} catch (Exception e) {}return null;
}/*** 处理符合时间格式字符串* @param dateStr* @param format* @return*/
public static Date parser(String dateStr, String format) {if (TextUtils.isEmpty(dateStr) || TextUtils.isEmpty(format)) {return null;}SimpleDateFormat sdf = new SimpleDateFormat(format);try {return sdf.parse(dateStr);} catch (ParseException e) {// TODO Auto-generated catch blockreturn null;}
}public static String getAmonthAgo(String format) {Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());calendar.add(Calendar.MONTH, -1);return format(calendar.getTime(), format);
}public static String getAmonthAgo(Date endDate, String format) {Calendar calendar = Calendar.getInstance();calendar.setTime(endDate);calendar.add(Calendar.MONTH, -1);return format(calendar.getTime(), format);
}/*** 处理非时间格式字符串如:date=20141010 time=163022* @param date* @param time* @return*/
public static Date getDateFromNumber(long date, long time) {long year = date / 10000;long month = (date % 10000) / 100;long day = date % 100;long hour = time / 10000;long minute = (time % 10000) / 100;long second = time % 100;Calendar calendar = Calendar.getInstance();calendar.set(Calendar.YEAR, (int) year);calendar.set(Calendar.MONTH, (int) month - 1);calendar.set(Calendar.DAY_OF_MONTH, (int) day);calendar.set(Calendar.HOUR_OF_DAY, (int) hour);calendar.set(Calendar.MINUTE, (int) minute);calendar.set(Calendar.SECOND, (int) second);return calendar.getTime();
}public static String getDateFromNumber(long date, long time, String format) {return DateUtils.format(getDateFromNumber(date, time), format);}public static boolean isInOneMonth(String startDateStr, String endDateStr) {Date start = parser(startDateStr, "yyyyMMdd");if (start == null) {return false;}Date end = parser(endDateStr, "yyyyMMdd");if (end == null) {return false;}Calendar calendar = Calendar.getInstance();calendar.setTime(end);calendar.add(Calendar.MONTH, -1);Date monthAgo = calendar.getTime();if (start.compareTo(monthAgo) >= 0) {return true;}return false;
}public static boolean isInOneMonth(Date startDate, Date endDate) {if (startDate == null || endDate == null) {return false;}Calendar calendar = Calendar.getInstance();calendar.setTime(endDate);calendar.add(Calendar.MONTH, -1);Date monthAgo = calendar.getTime();if (startDate.compareTo(monthAgo) >= 0) {return true;}return false;
}
/*** * @return null 时间格式错误*/
public static String getTimeAgoString(String dateStr,String paramFormat){Date date = parser(dateStr, paramFormat);if(date == null){return dateStr;}long timeMillion = date.getTime();String result = getTimeAgoString(timeMillion,paramFormat);if(TextUtils.isEmpty(result)){return dateStr;}return result;
}/*** * @return null 时间格式错误*/
public static String getTimeAgoString(long timeMillion,String resultFormat){if(timeMillion < 31507200000L){timeMillion *= 1000;}long currTime = System.currentTimeMillis();long timeSub = currTime - timeMillion;if(timeSub < 0){return "刚刚";}if(timeSub < (1 * 60 * 1000)){return "刚刚";}if(timeSub < (60 * 60 * 1000)){long t = timeSub / (60 * 1000);return t+"分钟前";}//        if(timeSub < (60 * 60 * 1000)){
//          return "半小时前";
//      }if(timeSub < (24 * 60 * 60 * 1000)){long t = timeSub / (60 * 60 * 1000);return t+"小时前";}Calendar yesterday = Calendar.getInstance();yesterday.setTimeInMillis(System.currentTimeMillis());yesterday.add(Calendar.DAY_OF_MONTH, -1);yesterday.set(Calendar.HOUR, 0);yesterday.set(Calendar.MINUTE, 0);yesterday.set(Calendar.SECOND, 0);yesterday.set(Calendar.MILLISECOND, 0);if(timeMillion > yesterday.getTimeInMillis()){return "昨天"+format(timeMillion,"HH:mm");}Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(timeMillion);int defYear = calendar.get(Calendar.YEAR);Calendar currCalendar = Calendar.getInstance();currCalendar.setTimeInMillis(System.currentTimeMillis());int currYear = currCalendar.get(Calendar.YEAR);if(defYear == currYear){return format(timeMillion,"MM-dd HH:mm");}else{return format(timeMillion,"yyyy-MM-dd");}}public static Date getDateByMillis(long time){if(time<=0){return null;}Date dat=new Date(time); return dat;
}
public static String getStringDate(Date date){if(date==null){return "";}SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");return format.format(date);  }
public static String getStringDateByMillis(long time ){return getStringDate(getDateByMillis(time));}public static boolean isToday(long time){Calendar calendar = Calendar.getInstance();return true;}/*** 知我*/private static final SimpleDateFormat FORMATTER_INPUT = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
private static final SimpleDateFormat FORMATTER_OUTPUT = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
private static final SimpleDateFormat FORMATTER_INPUT2 = new SimpleDateFormat("yyyyMMddHHmmss");
/*** 格式化日期* @param date* @return*/
public static String paserDate(String date) {try {Date myDate = FORMATTER_INPUT2.parse(date);Date curDate = new Date();long dif = curDate.getTime() - myDate.getTime();long temp = 0l;if (isToday(myDate)) {if (dif < 0) {return "1秒前";}temp = dif / (60L * 60L * 1000L);if (temp > 0) {return temp + "小时前";}temp = dif / (60 * 1000L);if (temp > 0) {return temp + "分钟前";}temp = dif / (1000L);if (temp > 0) {return temp + "秒前";}}return FORMATTER_OUTPUT.format(myDate);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;
}/*** 判断传入的日期是否是今天* @param mDate* @return*/
public static boolean isToday(Date mDate) {Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("dd");String todayStr = format.format(date);String mStr = format.format(mDate);SimpleDateFormat mm = new SimpleDateFormat("MM");SimpleDateFormat yy = new SimpleDateFormat("yyyy");if (!(yy.format(date).equals(yy.format(mDate)))) {return false;} else {if (!(mm.format(date).equals(mm.format(mDate)))) {return false;} else {if (todayStr.equals(mStr)) {return true;} else {return false;}}}
}/*** 获取当前时间* @return*/
public static String getCurrentTime() {Date date = new Date();return paserDate(FORMATTER_INPUT2.format(date));
}/*** 由手机时间判断是否是周末* */
public static boolean isWeekEnd() {int week = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);// Log.d("Time", "week="+week);if (week == Calendar.SATURDAY || week == Calendar.SUNDAY)return true;elsereturn false;}
/*** 判断当前日期是星期几** @param** @return dayForWeek 判断结果* @Exception 发生异常*/
public static String dayForWeek(Calendar c) {int dayForWeek = 0;if (c.get(Calendar.DAY_OF_WEEK) == 1) {dayForWeek = 7;} else {dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;}switch (dayForWeek) {case 1:return "周一";case 2:return "周二";case 3:return "周三";case 4:return "周四";case 5:return "周五";case 6:return "周六";case 7:return "周日";default:return "";}
}

时间单位换算 与不同格式显示相关推荐

  1. utc时间 单位换算_日期时间

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 使用time模块显示当前日期和时间 开发一个以多种格式显示当前时间和日期的程序.转换秒数为GMT时间.你要 ...

  2. utc时间 单位换算_数学基础知识点总结,常用单位换算长度、时间、面积等分类...

    在所有的课程中间,数学贯穿了整个学习生涯,对于学生学习数学知识,要培养学生对数学应用价值的意识,能解决简单的实际问题.数学有助于学生理解现实生活中的数的意义,引导学生培养估算能力.下面就讲一下在实际教 ...

  3. 计算机系统中时间周期的单位,时间单位换算 (全),计算机单位换算大全

    常用单位换算 长度单位换算 1千米=1000米 1米=10分米 1分米=10厘米 1米=100厘米 1厘米=10毫米 面积单位换算 1平方千米=100公顷 1公顷=10000平方米 1平方米=100平 ...

  4. ms、s、min、h时间单位换算

    一.背景 有一个延迟时间的字段数据,后端默认返回的是ms,但是返回的时候有可能数据量较大,所以需要前端根据后端返回的值去换算单位. 二.代码版本更迭 1. 第一版 思路: 根据后端返回的值和换算的临界 ...

  5. utc时间 单位换算_c++ 时间类型详解 time_t

    Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00 ...

  6. 微秒, 纳秒,毫秒, 时间单位换算

    秒也是一个的时间单位,0.000 000 001 毫秒 = 1皮秒,0.000 001 毫秒 = 1纳秒,0.001 毫秒 = 1微秒,1毫秒等于0.001秒,60秒等于1分钟,60分钟等于1小时,2 ...

  7. utc时间 单位换算_一些时间的概念与区分(UTC、GMT、LT、TAI等)

    UT - 世界时 Universal Time 世界时是最早的时间标准.在1884年,国际上将1s确定为全年内每日平均长度的1/8.64×104.以此标准形成的时间系统,称为世界时,即 UT1. 19 ...

  8. java计算程序运行时间及时间单位换算

    long startTime=System.currentTimeMillis(); for(int i=0;i<100;i++){ System.out.println("当前是:& ...

  9. 日期时间类,按特定格式显示日期时间

    1.Date类 Date date = new Date(); // 创建现在的日期 long value = date.getTime(); // 获得1970年1月1日00:00:00GMT到现在 ...

最新文章

  1. pyqt tcp通信_实验十 基于PyQt界面的TCP通信程序(一).doc_学小易找答案
  2. Eigen: C++开源矩阵计算工具——Eigen的简单用法
  3. 哲学家就餐问题linux源代码,Linux下实现哲学家就餐问题
  4. android system window,Android之属性fitsSystemWindows
  5. 时间戳服务器显示invalid,signtool签名时间戳失败的解决方法
  6. 管理对象空间——管理存储参数
  7. 我打败了妈妈 - 张朔
  8. Android ADB命令?这一次我再也不死记了!【简单说】
  9. Microsoft AJAX Library的beta2版发布
  10. 解决方案:Android开发基于rtmp视频直播
  11. 基于SSM的ERP管理系统和仓库管理系统
  12. gps坐标转成火星坐标
  13. Microsoft Excel 教程:如何在 Excel 中隐藏或显示行或列?
  14. 路由在电话网和计算机网中的区别,计算机猫和路由器之间有什么区别
  15. 笔记本电脑设置WiFi共享
  16. TCP/IP协议及常见状态码(SYN,FIN,ACK,PSH,RST)
  17. python渗透攻击
  18. 怎么用wifi进e站_科普微e站 | 注意!路由器千万别再这样用,当心信号越来越差!...
  19. 【STM32标准库】【基础知识】时钟系统
  20. 命令行启动burp报错:-Xbootclasspath/p is no longer a supported option.解决方法(Mac )

热门文章

  1. Python项目:爬虫实战
  2. 奇酷学院-Navicat 安装
  3. Win10无法正常启动,出现错误代码:0xc0000001
  4. 【Java知识点总结】Java 常量
  5. suse linux修改系统时间,怎么修改Suse Linux的时间
  6. 【深度学习】Weight Normalization: 一种简单的加速深度网络训练的重参数方法
  7. RSA算法原理(附带例子)
  8. 几分钟快速学会Linux开机启动服务
  9. 科技巨头的交通争夺战
  10. java实现对mysql数据库的操作_java 对mysql数据库的基本操作